-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcollection_stats.go
131 lines (105 loc) · 3.34 KB
/
collection_stats.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
package main
import (
"context"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"go.mongodb.org/mongo-driver/mongo"
"gopkg.in/mgo.v2/bson"
)
var (
docCount = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "mongo_collection_doc_count",
Help: "Collection document count",
}, []string{
"db",
"collection",
})
collDocumentsSize = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "mongo_collection_uncompressed_bytes",
Help: "Collection document sizes uncompressed in bytes",
}, []string{
"db",
"collection",
})
collTotalStorage = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "mongo_collection_disk_bytes",
Help: "Collection total disk storage size (documents + indexes) in bytes",
}, []string{
"db",
"collection",
})
collIndexesStorage = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "mongo_collection_indexes_bytes",
Help: "Collection total index size in bytes",
}, []string{
"db",
"collection",
})
collShardDocCount = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "mongo_collection_shard_doc_count",
Help: "Collection document count per shard",
}, []string{
"db",
"collection",
"shard",
})
collShardTotalStorage = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "mongo_collection_shard_disk_bytes",
Help: "Collection total storage per shard",
}, []string{
"db",
"collection",
"shard",
})
)
func processCollectionStats(mc *mongo.Client) error {
dbs, err := mc.ListDatabaseNames(context.TODO(), bson.M{})
if err != nil {
return fmt.Errorf("Could not list database names. err=%s", err)
}
//for each database, get collection stats
for _, dbname := range dbs {
if dbname == "config" || dbname == "admin" {
continue
}
cns, err := mc.Database(dbname).ListCollectionNames(context.TODO(), bson.M{})
if err != nil {
return fmt.Errorf("Could not list collection names. err=%s", err)
}
//get stats for each collection
for _, collname := range cns {
logrus.Debugf("Generating stats for collection %s.%s", dbname, collname)
r := mc.Database(dbname).RunCommand(context.TODO(),
bson.M{"collStats": collname},
)
br, err := r.DecodeBytes()
if err != nil {
return err
}
result := br.String()
if getFloatValue(gjson.Get(result, "ok")) != 1.0 {
return fmt.Errorf("Couldn't execute collStats for %s", collname)
}
docc := getFloatValue(gjson.Get(result, "count"))
docCount.WithLabelValues(dbname, collname).Set(docc)
uncompressedSize := getFloatValue(gjson.Get(result, "size"))
collDocumentsSize.WithLabelValues(dbname, collname).Set(uncompressedSize)
storageSize := getFloatValue(gjson.Get(result, "totalSize"))
collTotalStorage.WithLabelValues(dbname, collname).Set(storageSize)
indexSize := getFloatValue(gjson.Get(result, "totalIndexSize"))
collIndexesStorage.WithLabelValues(dbname, collname).Set(indexSize)
shards := gjson.Get(result, "shards").Map()
for shardname, v := range shards {
svs := v.String()
scount := getFloatValue(gjson.Get(svs, "count"))
collShardDocCount.WithLabelValues(dbname, collname, shardname).Set(scount)
sstorage := getFloatValue(gjson.Get(svs, "totalSize"))
collShardTotalStorage.WithLabelValues(dbname, collname, shardname).Set(sstorage)
}
}
}
return nil
}