/
profile_status_collector.go
91 lines (76 loc) · 2.55 KB
/
profile_status_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
// 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"
"time"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type profileCollector struct {
ctx context.Context
base *baseCollector
compatibleMode bool
topologyInfo labelsGetter
profiletimets int
}
// newProfileCollector creates a collector for being processed queries.
func newProfileCollector(ctx context.Context, client *mongo.Client,
compatible bool, topology labelsGetter, profileTimeTS int,
) *profileCollector {
return &profileCollector{
ctx: ctx,
base: newBaseCollector(client),
compatibleMode: compatible,
topologyInfo: topology,
profiletimets: profileTimeTS,
}
}
func (d *profileCollector) Describe(ch chan<- *prometheus.Desc) {
d.base.Describe(d.ctx, ch, d.collect)
}
func (d *profileCollector) Collect(ch chan<- prometheus.Metric) {
d.base.Collect(ch)
}
func (d *profileCollector) collect(ch chan<- prometheus.Metric) {
defer measureCollectTime(ch, "mongodb", "profile")()
client := d.base.client
timeScrape := d.profiletimets
databases, err := databases(d.ctx, client, nil, nil)
if err != nil {
errors.Wrap(err, "cannot get the database names list")
return
}
// Now time + '--collector.profile-time-ts'
ts := primitive.NewDateTimeFromTime(time.Now().Add(-time.Duration(time.Second * time.Duration(timeScrape))))
labels := d.topologyInfo.baseLabels()
// Get all slow queries from all databases
cmd := bson.M{"ts": bson.M{"$gte": ts}}
for _, db := range databases {
res, err := client.Database(db).Collection("system.profile").CountDocuments(d.ctx, cmd)
if err != nil {
errors.Wrapf(err, "cannot read system.profile")
break
}
labels["database"] = db
m := primitive.M{"count": res}
for _, metric := range makeMetrics("profile_slow_query", m, labels, d.compatibleMode) {
ch <- metric
}
}
}