/
indexstats_collector.go
158 lines (124 loc) · 4.1 KB
/
indexstats_collector.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
150
151
152
153
154
155
156
157
158
// mongodb_exporter
// Copyright (C) 2017 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package exporter
import (
"context"
"fmt"
"strings"
"github.com/cprobe/cprobe/lib/logger"
"github.com/prometheus/client_golang/prometheus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type indexstatsCollector struct {
ctx context.Context
base *baseCollector
discoveringMode bool
overrideDescendingIndex bool
topologyInfo labelsGetter
collections []string
opts *Opts
}
// newIndexStatsCollector creates a collector for statistics on index usage.
func newIndexStatsCollector(ctx context.Context, client *mongo.Client, topology labelsGetter, opts *Opts) *indexstatsCollector {
return &indexstatsCollector{
ctx: ctx,
base: newBaseCollector(client),
discoveringMode: opts.DiscoveringMode,
topologyInfo: topology,
overrideDescendingIndex: opts.EnableOverrideDescendingIndex,
collections: opts.IndexStatsCollections,
opts: opts,
}
}
func (d *indexstatsCollector) Describe(ch chan<- *prometheus.Desc) {
d.base.Describe(d.ctx, ch, d.collect)
}
func (d *indexstatsCollector) Collect(ch chan<- prometheus.Metric) {
d.base.Collect(ch)
}
func (d *indexstatsCollector) collect(ch chan<- prometheus.Metric) {
defer measureCollectTime(ch, "mongodb", "indexstats")()
collections := d.collections
client := d.base.client
if d.discoveringMode {
namespaces, err := listAllCollections(d.ctx, client, d.collections, systemDBs)
if err != nil {
logger.Errorf("cannot auto discover databases and collections, uri: %v, err: %s", d.opts.URI, err)
return
}
collections = fromMapToSlice(namespaces)
}
for _, dbCollection := range collections {
parts := strings.Split(dbCollection, ".")
if len(parts) < 2 { //nolint:gomnd
continue
}
database := parts[0]
collection := strings.Join(parts[1:], ".")
aggregation := bson.D{
{Key: "$indexStats", Value: bson.M{}},
}
cursor, err := client.Database(database).Collection(collection).Aggregate(d.ctx, mongo.Pipeline{aggregation})
if err != nil {
logger.Errorf("cannot get $indexStats cursor for collection %s.%s: %s", database, collection, err)
continue
}
var stats []bson.M
if err = cursor.All(d.ctx, &stats); err != nil {
logger.Errorf("cannot get $indexStats for collection %s.%s: %s", database, collection, err)
continue
}
for _, metric := range stats {
indexName := fmt.Sprintf("%s", metric["name"])
// Override the label name
if d.overrideDescendingIndex {
indexName = strings.ReplaceAll(fmt.Sprintf("%s", metric["name"]), "-1", "DESC")
}
// prefix and labels are needed to avoid duplicated metric names since the metrics are the
// same, for different collections.
prefix := "indexstats"
labels := d.topologyInfo.baseLabels()
labels["database"] = database
labels["collection"] = collection
labels["key_name"] = indexName
metrics := sanitizeMetrics(metric)
for _, metric := range makeMetrics(prefix, metrics, labels, false) {
ch <- metric
}
}
}
}
// According to specs, we should expose only this 2 metrics. 'building' might not exist.
func sanitizeMetrics(m bson.M) bson.M {
ops := float64(0)
if val := walkTo(m, []string{"accesses", "ops"}); val != nil {
if f, err := asFloat64(val); err == nil {
ops = *f
}
}
filteredMetrics := bson.M{
"accesses": bson.M{
"ops": ops,
},
}
if val := walkTo(m, []string{"building"}); val != nil {
if f, err := asFloat64(val); err == nil {
filteredMetrics["building"] = *f
}
}
return filteredMetrics
}
var _ prometheus.Collector = (*indexstatsCollector)(nil)