-
Notifications
You must be signed in to change notification settings - Fork 163
/
s3.go
136 lines (117 loc) · 4.09 KB
/
s3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package collectors
import (
"context"
"fmt"
"strings"
"github.com/minio/minio-go/v7"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
type s3Collector struct {
ObjectCount *prometheus.Desc
ObjectLastModifiedDate *prometheus.Desc
EmptyObjectCount *prometheus.Desc
QuerySuccess *prometheus.Desc
client ctrlruntimeclient.Reader
bucket string
minioClient *minio.Client
logger *zap.SugaredLogger
}
// MustRegisterS3Collector registers the S3 collector.
func MustRegisterS3Collector(minioClient *minio.Client, client ctrlruntimeclient.Reader, bucket string, logger *zap.SugaredLogger) {
collector := s3Collector{}
collector.minioClient = minioClient
collector.client = client
collector.bucket = bucket
collector.logger = logger
collector.ObjectCount = prometheus.NewDesc(
"kubermatic_s3_object_count",
"The amount of objects partitioned by cluster",
[]string{"cluster"}, nil)
collector.ObjectLastModifiedDate = prometheus.NewDesc(
"kubermatic_s3_object_last_modified_time_seconds",
"Modification time of the last modified object",
[]string{"cluster"}, nil)
collector.EmptyObjectCount = prometheus.NewDesc(
"kubermatic_s3_empty_object_count",
"The amount of empty objects (size=0) partitioned by cluster",
[]string{"cluster"}, nil)
collector.QuerySuccess = prometheus.NewDesc(
"kubermatic_s3_query_success",
"Whether querying the S3 was successful",
nil, nil)
prometheus.MustRegister(&collector)
}
func (e *s3Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- e.ObjectCount
ch <- e.ObjectLastModifiedDate
ch <- e.EmptyObjectCount
ch <- e.QuerySuccess
}
func (e *s3Collector) Collect(ch chan<- prometheus.Metric) {
clusterList := &kubermaticv1.ClusterList{}
if err := e.client.List(context.Background(), clusterList); err != nil {
e.logger.Errorw("Failed to list clusters", zap.Error(err))
ch <- prometheus.MustNewConstMetric(
e.QuerySuccess,
prometheus.GaugeValue,
float64(1))
return
}
logger := e.logger.With("bucket", e.bucket)
listOpts := minio.ListObjectsOptions{
Recursive: true,
}
var objects []minio.ObjectInfo
for listerObject := range e.minioClient.ListObjects(context.Background(), e.bucket, listOpts) {
if listerObject.Err != nil {
logger.Errorw("Error on object", "object", listerObject.Key, zap.Error(listerObject.Err))
ch <- prometheus.MustNewConstMetric(
e.QuerySuccess,
prometheus.GaugeValue,
float64(1))
return
}
objects = append(objects, listerObject)
}
for _, cluster := range clusterList.Items {
e.setMetricsForCluster(ch, objects, cluster.Name)
}
}
func (e *s3Collector) setMetricsForCluster(ch chan<- prometheus.Metric, allObjects []minio.ObjectInfo, clusterName string) {
var clusterObjects []minio.ObjectInfo
for _, object := range allObjects {
if strings.HasPrefix(object.Key, fmt.Sprintf("%s-", clusterName)) {
clusterObjects = append(clusterObjects, object)
}
}
ch <- prometheus.MustNewConstMetric(
e.ObjectCount,
prometheus.GaugeValue,
float64(len(clusterObjects)),
clusterName)
ch <- prometheus.MustNewConstMetric(
e.ObjectLastModifiedDate,
prometheus.GaugeValue,
float64(getLastModifiedTimestamp(clusterObjects).UnixNano()),
clusterName)
ch <- prometheus.MustNewConstMetric(
e.EmptyObjectCount,
prometheus.GaugeValue,
float64(getEmptyObjectCount(clusterObjects)),
clusterName)
}