@@ -20,6 +20,7 @@ import (
20
20
"fmt"
21
21
"strings"
22
22
23
+ "github.com/pkg/errors"
23
24
"github.com/prometheus/client_golang/prometheus"
24
25
"github.com/sirupsen/logrus"
25
26
"go.mongodb.org/mongo-driver/bson"
@@ -37,7 +38,7 @@ type shardsCollector struct {
37
38
func newShardsCollector (ctx context.Context , client * mongo.Client , logger * logrus.Logger , compatibleMode bool ) * shardsCollector {
38
39
return & shardsCollector {
39
40
ctx : ctx ,
40
- base : newBaseCollector (client , logger ),
41
+ base : newBaseCollector (client , logger . WithFields (logrus. Fields { "collector" : "shards" }) ),
41
42
compatible : compatibleMode ,
42
43
}
43
44
}
@@ -56,6 +57,26 @@ func (d *shardsCollector) collect(ch chan<- prometheus.Metric) {
56
57
client := d .base .client
57
58
logger := d .base .logger
58
59
prefix := "shards collection chunks"
60
+ ctx := d .ctx
61
+
62
+ metrics := make ([]prometheus.Metric , 0 )
63
+ metric , err := chunksTotal (ctx , client )
64
+ if err != nil {
65
+ logger .Warnf ("cannot create metric for chunks total: %s" , err )
66
+ } else {
67
+ metrics = append (metrics , metric )
68
+ }
69
+
70
+ ms , err := chunksTotalPerShard (ctx , client )
71
+ if err != nil {
72
+ logger .Warnf ("cannot create metric for chunks total per shard: %s" , err )
73
+ } else {
74
+ metrics = append (metrics , ms ... )
75
+ }
76
+
77
+ for _ , metric := range metrics {
78
+ ch <- metric
79
+ }
59
80
60
81
databaseNames , err := client .ListDatabaseNames (d .ctx , bson.D {})
61
82
if err != nil {
@@ -186,4 +207,59 @@ func (d *shardsCollector) getChunksForCollection(row primitive.M) []bson.M {
186
207
return chunks
187
208
}
188
209
210
+ func chunksTotal (ctx context.Context , client * mongo.Client ) (prometheus.Metric , error ) { //nolint:ireturn
211
+ n , err := client .Database ("config" ).Collection ("chunks" ).CountDocuments (ctx , bson.M {})
212
+ if err != nil {
213
+ return nil , errors .Wrap (err , "cannot get total number of chunks" )
214
+ }
215
+
216
+ name := "mongodb_mongos_sharding_chunks_total"
217
+ help := "Total number of chunks"
218
+
219
+ d := prometheus .NewDesc (name , help , nil , nil )
220
+ return prometheus .NewConstMetric (d , prometheus .GaugeValue , float64 (n ))
221
+ }
222
+
223
+ func chunksTotalPerShard (ctx context.Context , client * mongo.Client ) ([]prometheus.Metric , error ) {
224
+ aggregation := bson.D {
225
+ {Key : "$group" , Value : bson.M {"_id" : "$shard" , "count" : bson.M {"$sum" : 1 }}},
226
+ }
227
+
228
+ cursor , err := client .Database ("config" ).Collection ("chunks" ).Aggregate (ctx , mongo.Pipeline {aggregation })
229
+ if err != nil {
230
+ return nil , errors .Wrap (err , "cannot get $shards cursor for collection config.chunks" )
231
+ }
232
+
233
+ var shards []bson.M
234
+ if err = cursor .All (ctx , & shards ); err != nil {
235
+ return nil , errors .Wrap (err , "cannot get $shards for collection config.chunks" )
236
+ }
237
+
238
+ metrics := make ([]prometheus.Metric , 0 , len (shards ))
239
+
240
+ for _ , shard := range shards {
241
+ help := "Total number of chunks per shard"
242
+ id , ok := shard ["_id" ].(string )
243
+ if ! ok {
244
+ continue
245
+ }
246
+ labels := map [string ]string {"shard" : id }
247
+
248
+ d := prometheus .NewDesc ("mongodb_mongos_sharding_shard_chunks_total" , help , nil , labels )
249
+ val , ok := shard ["count" ].(int32 )
250
+ if ! ok {
251
+ continue
252
+ }
253
+
254
+ metric , err := prometheus .NewConstMetric (d , prometheus .GaugeValue , float64 (val ))
255
+ if err != nil {
256
+ continue
257
+ }
258
+
259
+ metrics = append (metrics , metric )
260
+ }
261
+
262
+ return metrics , nil
263
+ }
264
+
189
265
var _ prometheus.Collector = (* shardsCollector )(nil )
0 commit comments