forked from jacksontj/mongodb_exporter
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtop_counters.go
117 lines (97 loc) · 3.82 KB
/
top_counters.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
package collector
import (
"reflect"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
var (
readOps = map[string]bool{"Queries": true, "GetMore": true, "Commands": true}
writeOps = map[string]bool{"Insert": true, "Remove": true, "Update": true}
)
var (
topTimeSecondsTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Name: "top_time_seconds_total",
Help: "The top command provides operation time, in seconds, for each database collection",
}, []string{"type", "database", "collection"})
topCountTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Name: "top_count_total",
Help: "The top command provides operation count for each database collection",
}, []string{"type", "database", "collection"})
topTimeSecondsAggregateTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Name: "top_time_seconds_aggregate_total",
Help: "An aggregate counter for top time seconds for read/write (does not include locks)",
}, []string{"type"})
topCountAggregateTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: Namespace,
Name: "top_count_aggregate_total",
Help: "An aggregate counter for top operations for read/write (does not include locks)",
}, []string{"type"})
)
// TopStatsMap is a map of top stats
type TopStatsMap map[string]TopStats
// TopcountersStats topcounters stats
type TopcounterStats struct {
Time float64 `bson:"time"`
Count float64 `bson:"count"`
}
// TopCollectionStats top collection stats
type TopStats struct {
Total TopcounterStats `bson:"total"`
ReadLock TopcounterStats `bson:"readLock"`
WriteLock TopcounterStats `bson:"writeLock"`
Queries TopcounterStats `bson:"queries"`
GetMore TopcounterStats `bson:"getmore"`
Insert TopcounterStats `bson:"insert"`
Update TopcounterStats `bson:"update"`
Remove TopcounterStats `bson:"remove"`
Commands TopcounterStats `bson:"commands"`
}
// Export exports the data to prometheus.
func (topStats TopStatsMap) Export(ch chan<- prometheus.Metric) {
totalReadSeconds := float64(0)
totalReadOps := float64(0)
totalWriteSeconds := float64(0)
totalWriteOps := float64(0)
for collectionNamespace, topStat := range topStats {
namespace := strings.Split(collectionNamespace, ".")
database := namespace[0]
collection := strings.Join(namespace[1:], ".")
topStatTypes := reflect.TypeOf(topStat)
topStatValues := reflect.ValueOf(topStat)
for i := 0; i < topStatValues.NumField(); i++ {
metric_type := topStatTypes.Field(i).Name
op_count := topStatValues.Field(i).Field(1).Float()
op_time_microsecond := topStatValues.Field(i).Field(0).Float()
op_time_second := float64(op_time_microsecond / 1e6)
if _, ok := readOps[metric_type]; ok {
totalReadSeconds += op_time_second
totalReadOps += op_count
}
if _, ok := writeOps[metric_type]; ok {
totalWriteSeconds += op_time_second
totalWriteOps += op_count
}
topTimeSecondsTotal.WithLabelValues(metric_type, database, collection).Set(op_time_second)
topCountTotal.WithLabelValues(metric_type, database, collection).Set(op_count)
}
}
// Set aggregate metrics
topTimeSecondsAggregateTotal.WithLabelValues("Read").Set(totalReadSeconds)
topTimeSecondsAggregateTotal.WithLabelValues("Write").Set(totalWriteSeconds)
topCountAggregateTotal.WithLabelValues("Read").Set(totalReadOps)
topCountAggregateTotal.WithLabelValues("Write").Set(totalWriteOps)
topTimeSecondsTotal.Collect(ch)
topCountTotal.Collect(ch)
topTimeSecondsAggregateTotal.Collect(ch)
topCountAggregateTotal.Collect(ch)
}
// Describe describes the metrics for prometheus
func (tops TopStatsMap) Describe(ch chan<- *prometheus.Desc) {
topTimeSecondsTotal.Describe(ch)
topCountTotal.Describe(ch)
topTimeSecondsAggregateTotal.Describe(ch)
topCountAggregateTotal.Describe(ch)
}