@@ -22,51 +22,61 @@ import (
22
22
23
23
// BucketCollector collects metrics about all buckets.
24
24
type BucketCollector struct {
25
- logger log.Logger
26
- errors * prometheus.CounterVec
27
- client * scw.Client
28
- region * scw.Region
29
- s3Client * s3.S3
30
- timeout time.Duration
25
+ logger log.Logger
26
+ errors * prometheus.CounterVec
27
+ endpoints []Endpoint
28
+ timeout time.Duration
31
29
32
30
ObjectCount * prometheus.Desc
33
31
Bandwidth * prometheus.Desc
34
32
StorageUsageStandard * prometheus.Desc
35
33
StorageUsageGlacier * prometheus.Desc
36
34
}
37
35
36
+ type Endpoint struct {
37
+ client * scw.Client
38
+ region scw.Region
39
+ s3Client * s3.S3
40
+ }
41
+
38
42
// NewBucketCollector returns a new BucketCollector.
39
- func NewBucketCollector (logger log.Logger , errors * prometheus.CounterVec , client * scw.Client , timeout time.Duration ) * BucketCollector {
43
+ func NewBucketCollector (logger log.Logger , errors * prometheus.CounterVec , client * scw.Client , timeout time.Duration , regions []scw. Region ) * BucketCollector {
40
44
errors .WithLabelValues ("bucket" ).Add (0 )
41
45
42
- region , _ := client .GetDefaultRegion ()
43
-
44
46
accessKey , _ := client .GetAccessKey ()
45
47
46
48
secretKey , _ := client .GetSecretKey ()
47
49
48
- newSession , err := session .NewSession (& aws.Config {
49
- Credentials : credentials .NewStaticCredentials (accessKey , secretKey , "" ),
50
- Region : aws .String (fmt .Sprint (region )),
51
- })
50
+ endpoints := make ([]Endpoint , len (regions ))
52
51
53
- if err != nil {
54
- _ = level .Error (logger ).Log ("msg" , "can't create a S3 client" , "err" , err )
55
- os .Exit (1 )
56
- }
52
+ for i , region := range regions {
57
53
58
- s3Client := s3 . New ( newSession , & aws.Config {
59
- Endpoint : aws . String ( "https://s3." + fmt . Sprint ( region ) + ".scw.cloud " ),
60
- S3ForcePathStyle : aws .Bool ( true ),
61
- })
54
+ newSession , err := session . NewSession ( & aws.Config {
55
+ Credentials : credentials . NewStaticCredentials ( accessKey , secretKey , " " ),
56
+ Region : aws .String ( fmt . Sprint ( region ) ),
57
+ })
62
58
59
+ if err != nil {
60
+ _ = level .Error (logger ).Log ("msg" , "can't create a S3 client" , "err" , err )
61
+ os .Exit (1 )
62
+ }
63
+
64
+ s3Client := s3 .New (newSession , & aws.Config {
65
+ Endpoint : aws .String ("https://s3." + fmt .Sprint (region ) + ".scw.cloud" ),
66
+ S3ForcePathStyle : aws .Bool (true ),
67
+ })
68
+
69
+ endpoints [i ] = Endpoint {
70
+ client : client ,
71
+ s3Client : s3Client ,
72
+ region : region ,
73
+ }
74
+ }
63
75
return & BucketCollector {
64
- region : & region ,
65
- logger : logger ,
66
- errors : errors ,
67
- client : client ,
68
- s3Client : s3Client ,
69
- timeout : timeout ,
76
+ logger : logger ,
77
+ errors : errors ,
78
+ endpoints : endpoints ,
79
+ timeout : timeout ,
70
80
71
81
ObjectCount : prometheus .NewDesc (
72
82
"scaleway_s3_object_total" ,
@@ -142,13 +152,15 @@ type HandleSimpleMetricOptions struct {
142
152
MetricName MetricName
143
153
Desc * prometheus.Desc
144
154
labels []string
155
+ Endpoint Endpoint
145
156
}
146
157
147
158
type HandleMultiMetricsOptions struct {
148
159
Bucket string
149
160
MetricName MetricName
150
161
DescMatrix map [string ]* prometheus.Desc
151
162
labels []string
163
+ Endpoint Endpoint
152
164
}
153
165
154
166
// Collect is called by the Prometheus registry when collecting metrics.
@@ -157,69 +169,72 @@ func (c *BucketCollector) Collect(ch chan<- prometheus.Metric) {
157
169
_ , cancel := context .WithTimeout (context .Background (), c .timeout )
158
170
defer cancel ()
159
171
160
- buckets , err := c . s3Client . ListBuckets ( & s3. ListBucketsInput {})
172
+ for _ , endpoint := range c . endpoints {
161
173
162
- if err != nil {
163
- c .errors .WithLabelValues ("bucket" ).Add (1 )
164
- _ = level .Warn (c .logger ).Log ("msg" , "can't fetch the list of buckets" , "err" , err )
174
+ buckets , err := endpoint .s3Client .ListBuckets (& s3.ListBucketsInput {})
165
175
166
- return
167
- }
176
+ if err != nil {
177
+ c .errors .WithLabelValues ("bucket" ).Add (1 )
178
+ _ = level .Warn (c .logger ).Log ("msg" , "can't fetch the list of buckets" , "err" , err )
168
179
169
- scwReq := & scw.ScalewayRequest {
170
- Method : "POST" ,
171
- Path : "/object-private/v1/regions/" + fmt .Sprint (c .region ) + "/buckets-info/" ,
172
- }
180
+ return
181
+ }
173
182
174
- var bucketNames []string
183
+ scwReq := & scw.ScalewayRequest {
184
+ Method : "POST" ,
185
+ Path : "/object-private/v1/regions/" + fmt .Sprint (endpoint .region ) + "/buckets-info/" ,
186
+ }
175
187
176
- for _ , bucket := range buckets . Buckets {
188
+ var bucketNames [] string
177
189
178
- bucketNames = append (bucketNames , * bucket .Name )
179
- }
190
+ for _ , bucket := range buckets .Buckets {
180
191
181
- projectId := strings .Split (* buckets .Owner .ID , ":" )[0 ]
192
+ bucketNames = append (bucketNames , * bucket .Name )
193
+ }
182
194
183
- _ = level . Debug ( c . logger ). Log ( "msg" , fmt . Sprintf ( "found %d buckets under projectID %s : %s" , len ( bucketNames ), projectId , bucketNames ))
195
+ projectId := strings . Split ( * buckets . Owner . ID , ":" )[ 0 ]
184
196
185
- err = scwReq . SetBody ( & BucketInfoRequestBody { ProjectId : projectId , BucketsName : bucketNames } )
197
+ _ = level . Debug ( c . logger ). Log ( "msg" , fmt . Sprintf ( "found %d buckets under projectID %s : %s" , len ( bucketNames ), projectId , bucketNames ) )
186
198
187
- if err != nil {
188
- c .errors .WithLabelValues ("bucket" ).Add (1 )
189
- _ = level .Warn (c .logger ).Log ("msg" , "can't fetch details of buckets" , "err" , err )
199
+ err = scwReq .SetBody (& BucketInfoRequestBody {ProjectId : projectId , BucketsName : bucketNames })
190
200
191
- return
192
- }
201
+ if err != nil {
202
+ c .errors .WithLabelValues ("bucket" ).Add (1 )
203
+ _ = level .Warn (c .logger ).Log ("msg" , "can't fetch details of buckets" , "err" , err )
193
204
194
- var response BucketInfoList
205
+ return
206
+ }
195
207
196
- err = c . client . Do ( scwReq , & response )
208
+ var response BucketInfoList
197
209
198
- if err != nil {
199
- c .errors .WithLabelValues ("bucket" ).Add (1 )
200
- _ = level .Warn (c .logger ).Log ("msg" , "can't fetch details of buckets" , "err" , err )
210
+ err = endpoint .client .Do (scwReq , & response )
201
211
202
- return
203
- }
212
+ if err != nil {
213
+ c .errors .WithLabelValues ("bucket" ).Add (1 )
214
+ _ = level .Warn (c .logger ).Log ("msg" , "can't fetch details of buckets" , "err" , err )
204
215
205
- var wg sync. WaitGroup
206
- defer wg . Wait ()
216
+ return
217
+ }
207
218
208
- for name , bucket := range response .Buckets {
219
+ var wg sync.WaitGroup
220
+ defer wg .Wait ()
209
221
210
- wg . Add ( 1 )
222
+ for name , bucket := range response . Buckets {
211
223
212
- _ = level . Debug ( c . logger ). Log ( "msg" , fmt . Sprintf ( "Fetching metrics for bucket : %s" , name ) )
224
+ wg . Add ( 1 )
213
225
214
- go c .FetchMetricsForBucket (& wg , ch , name , bucket )
226
+ _ = level .Debug (c .logger ).Log ("msg" , fmt .Sprintf ("Fetching metrics for bucket : %s" , name ))
227
+
228
+ go c .FetchMetricsForBucket (& wg , ch , name , bucket , endpoint )
229
+ }
215
230
}
216
231
}
217
232
218
- func (c * BucketCollector ) FetchMetricsForBucket (parentWg * sync.WaitGroup , ch chan <- prometheus.Metric , name string , bucket BucketInfo ) {
233
+ func (c * BucketCollector ) FetchMetricsForBucket (parentWg * sync.WaitGroup , ch chan <- prometheus.Metric , name string , bucket BucketInfo , endpoint Endpoint ) {
219
234
220
235
defer parentWg .Done ()
221
236
222
- labels := []string {name , fmt .Sprint (c .region ), fmt .Sprint (bucket .IsPublic )}
237
+ labels := []string {name , fmt .Sprint (endpoint .region ), fmt .Sprint (bucket .IsPublic )}
223
238
224
239
// TODO check if it is possible to add bucket tag as labels
225
240
//for _, tags := range instance.Tags {
@@ -236,20 +251,23 @@ func (c *BucketCollector) FetchMetricsForBucket(parentWg *sync.WaitGroup, ch cha
236
251
MetricName : ObjectCount ,
237
252
labels : labels ,
238
253
Desc : c .ObjectCount ,
254
+ Endpoint : endpoint ,
239
255
})
240
256
241
257
go c .HandleSimpleMetric (& wg , ch , & HandleSimpleMetricOptions {
242
258
Bucket : name ,
243
259
MetricName : BytesSent ,
244
260
labels : labels ,
245
261
Desc : c .Bandwidth ,
262
+ Endpoint : endpoint ,
246
263
})
247
264
248
265
go c .HandleMultiMetrics (& wg , ch , & HandleMultiMetricsOptions {
249
266
Bucket : name ,
250
267
MetricName : StorageUsage ,
251
268
labels : labels ,
252
269
DescMatrix : map [string ]* prometheus.Desc {"STANDARD" : c .StorageUsageStandard , "GLACIER" : c .StorageUsageGlacier },
270
+ Endpoint : endpoint ,
253
271
})
254
272
}
255
273
@@ -259,7 +277,7 @@ func (c *BucketCollector) HandleSimpleMetric(parentWg *sync.WaitGroup, ch chan<-
259
277
260
278
var response Metric
261
279
262
- err := c .FetchMetric (options .Bucket , options .MetricName , & response )
280
+ err := c .FetchMetric (options .Bucket , options .MetricName , & response , options . Endpoint )
263
281
264
282
if err != nil {
265
283
@@ -304,7 +322,7 @@ func (c *BucketCollector) HandleMultiMetrics(parentWg *sync.WaitGroup, ch chan<-
304
322
305
323
var response Metric
306
324
307
- err := c .FetchMetric (options .Bucket , options .MetricName , & response )
325
+ err := c .FetchMetric (options .Bucket , options .MetricName , & response , options . Endpoint )
308
326
309
327
if err != nil {
310
328
@@ -355,7 +373,7 @@ func (c *BucketCollector) HandleMultiMetrics(parentWg *sync.WaitGroup, ch chan<-
355
373
}
356
374
}
357
375
358
- func (c * BucketCollector ) FetchMetric (Bucket string , MetricName MetricName , response * Metric ) error {
376
+ func (c * BucketCollector ) FetchMetric (Bucket string , MetricName MetricName , response * Metric , endpoint Endpoint ) error {
359
377
360
378
query := url.Values {}
361
379
@@ -365,11 +383,11 @@ func (c *BucketCollector) FetchMetric(Bucket string, MetricName MetricName, resp
365
383
366
384
scwReq := & scw.ScalewayRequest {
367
385
Method : "GET" ,
368
- Path : "/object-private/v1/regions/" + fmt .Sprint (c .region ) + "/buckets/" + Bucket + "/metrics" ,
386
+ Path : "/object-private/v1/regions/" + fmt .Sprint (endpoint .region ) + "/buckets/" + Bucket + "/metrics" ,
369
387
Query : query ,
370
388
}
371
389
372
- err := c .client .Do (scwReq , & response )
390
+ err := endpoint .client .Do (scwReq , & response )
373
391
374
392
if err != nil {
375
393
0 commit comments