|
| 1 | +// mongodb_exporter |
| 2 | +// Copyright (C) 2017 Percona LLC |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package exporter |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/pkg/errors" |
| 23 | + "github.com/prometheus/client_golang/prometheus" |
| 24 | + "github.com/sirupsen/logrus" |
| 25 | + "go.mongodb.org/mongo-driver/bson" |
| 26 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 27 | + "go.mongodb.org/mongo-driver/mongo" |
| 28 | +) |
| 29 | + |
| 30 | +type profileCollector struct { |
| 31 | + ctx context.Context |
| 32 | + base *baseCollector |
| 33 | + compatibleMode bool |
| 34 | + topologyInfo labelsGetter |
| 35 | + profiletimets int |
| 36 | +} |
| 37 | + |
| 38 | +// newProfileCollector creates a collector for being processed queries. |
| 39 | +func newProfileCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, |
| 40 | + compatible bool, topology labelsGetter, profileTimeTS int, |
| 41 | +) *profileCollector { |
| 42 | + return &profileCollector{ |
| 43 | + ctx: ctx, |
| 44 | + base: newBaseCollector(client, logger), |
| 45 | + compatibleMode: compatible, |
| 46 | + topologyInfo: topology, |
| 47 | + profiletimets: profileTimeTS, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (d *profileCollector) Describe(ch chan<- *prometheus.Desc) { |
| 52 | + d.base.Describe(d.ctx, ch, d.collect) |
| 53 | +} |
| 54 | + |
| 55 | +func (d *profileCollector) Collect(ch chan<- prometheus.Metric) { |
| 56 | + d.base.Collect(ch) |
| 57 | +} |
| 58 | + |
| 59 | +func (d *profileCollector) collect(ch chan<- prometheus.Metric) { |
| 60 | + defer measureCollectTime(ch, "mongodb", "profile")() |
| 61 | + |
| 62 | + logger := d.base.logger |
| 63 | + client := d.base.client |
| 64 | + timeScrape := d.profiletimets |
| 65 | + |
| 66 | + databases, err := databases(d.ctx, client, nil, nil) |
| 67 | + if err != nil { |
| 68 | + errors.Wrap(err, "cannot get the database names list") |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + // Now time + '--collector.profile-time-ts' |
| 73 | + ts := primitive.NewDateTimeFromTime(time.Now().Add(-time.Duration(time.Second * time.Duration(timeScrape)))) |
| 74 | + |
| 75 | + labels := d.topologyInfo.baseLabels() |
| 76 | + |
| 77 | + // Get all slow queries from all databases |
| 78 | + cmd := bson.M{"ts": bson.M{"$gte": ts}} |
| 79 | + for _, db := range databases { |
| 80 | + res, err := client.Database(db).Collection("system.profile").CountDocuments(d.ctx, cmd) |
| 81 | + if err != nil { |
| 82 | + errors.Wrapf(err, "cannot read system.profile") |
| 83 | + break |
| 84 | + } |
| 85 | + labels["database"] = db |
| 86 | + |
| 87 | + m := primitive.M{"count": res} |
| 88 | + |
| 89 | + logger.Debug("profile response from MongoDB:") |
| 90 | + debugResult(logger, primitive.M{db: m}) |
| 91 | + |
| 92 | + for _, metric := range makeMetrics("profile_slow_query", m, labels, d.compatibleMode) { |
| 93 | + ch <- metric |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments