/
collections_status.go
149 lines (141 loc) · 4.96 KB
/
collections_status.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
137
138
139
140
141
142
143
144
145
146
147
148
149
package mongod
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var (
collectionSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "db_coll",
Name: "size",
Help: "The total size in memory of all records in a collection",
}, []string{"db", "coll"})
collectionObjectCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "db_coll",
Name: "count",
Help: "The number of objects or documents in this collection",
}, []string{"db", "coll"})
collectionAvgObjSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "db_coll",
Name: "avgobjsize",
Help: "The average size of an object in the collection (plus any padding)",
}, []string{"db", "coll"})
collectionStorageSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "db_coll",
Name: "storage_size",
Help: "The total amount of storage allocated to this collection for document storage",
}, []string{"db", "coll"})
collectionIndexes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "db_coll",
Name: "indexes",
Help: "The number of indexes on the collection",
}, []string{"db", "coll"})
collectionIndexesSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "db_coll",
Name: "indexes_size",
Help: "The total size of all indexes",
}, []string{"db", "coll"})
collectionIndexSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: "db_coll",
Name: "index_size",
Help: "The individual index size",
}, []string{"db", "coll", "index"})
)
// CollectionStatList contains stats from all collections
type CollectionStatList struct {
Members []CollectionStatus
}
// CollectionStatus represents stats about a collection in database (mongod and raw from mongos)
type CollectionStatus struct {
Database string
Name string
Size int `bson:"size,omitempty"`
Count int `bson:"count,omitempty"`
AvgObjSize int `bson:"avgObjSize,omitempty"`
StorageSize int `bson:"storageSize,omitempty"`
IndexesSize int `bson:"totalIndexSize,omitempty"`
IndexSizes map[string]float64 `bson:"indexSizes,omitempty"`
}
// Export exports database stats to prometheus
func (collStatList *CollectionStatList) Export(ch chan<- prometheus.Metric) {
// reset previously collected values
collectionSize.Reset()
collectionObjectCount.Reset()
collectionAvgObjSize.Reset()
collectionStorageSize.Reset()
collectionIndexes.Reset()
collectionIndexesSize.Reset()
collectionIndexSize.Reset()
for _, member := range collStatList.Members {
ls := prometheus.Labels{
"db": member.Database,
"coll": member.Name,
}
collectionSize.With(ls).Set(float64(member.Size))
collectionObjectCount.With(ls).Set(float64(member.Count))
collectionAvgObjSize.With(ls).Set(float64(member.AvgObjSize))
collectionStorageSize.With(ls).Set(float64(member.StorageSize))
collectionIndexes.With(ls).Set(float64(len(member.IndexSizes)))
collectionIndexesSize.With(ls).Set(float64(member.IndexesSize))
for indexName, size := range member.IndexSizes {
ls = prometheus.Labels{
"db": member.Database,
"coll": member.Name,
"index": indexName,
}
collectionIndexSize.With(ls).Set(size)
}
}
collectionSize.Collect(ch)
collectionObjectCount.Collect(ch)
collectionAvgObjSize.Collect(ch)
collectionStorageSize.Collect(ch)
collectionIndexes.Collect(ch)
collectionIndexesSize.Collect(ch)
collectionIndexSize.Collect(ch)
}
// Describe describes database stats for prometheus
func (collStatList *CollectionStatList) Describe(ch chan<- *prometheus.Desc) {
collectionSize.Describe(ch)
collectionObjectCount.Describe(ch)
collectionAvgObjSize.Describe(ch)
collectionStorageSize.Describe(ch)
collectionIndexes.Describe(ch)
collectionIndexesSize.Describe(ch)
}
// GetDatabaseStatus returns stats for a given database
func GetCollectionStatList(session *mgo.Session) *CollectionStatList {
collectionStatList := &CollectionStatList{}
database_names, err := session.DatabaseNames()
if err != nil {
log.Error("Failed to get database names")
return nil
}
for _, db := range database_names {
collection_names, err := session.DB(db).CollectionNames()
if err != nil {
log.Error("Failed to get collection names for db=" + db)
return nil
}
for _, collection_name := range collection_names {
collStatus := CollectionStatus{}
err := session.DB(db).Run(bson.D{{"collStats", collection_name}, {"scale", 1}}, &collStatus)
collStatus.Database = db
collStatus.Name = collection_name
if err != nil {
log.Error("Failed to get collection status.")
return nil
}
collectionStatList.Members = append(collectionStatList.Members, collStatus)
}
}
return collectionStatList
}