|
| 1 | +// mongodb_exporter |
| 2 | +// Copyright (C) 2017 Percona LLC |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package exporter |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + "github.com/prometheus/client_golang/prometheus" |
| 23 | + "github.com/sirupsen/logrus" |
| 24 | + "go.mongodb.org/mongo-driver/bson" |
| 25 | + "go.mongodb.org/mongo-driver/mongo" |
| 26 | +) |
| 27 | + |
| 28 | +type serverStatusCollector struct { |
| 29 | + ctx context.Context |
| 30 | + base *baseCollector |
| 31 | + |
| 32 | + compatibleMode bool |
| 33 | + topologyInfo labelsGetter |
| 34 | +} |
| 35 | + |
| 36 | +// newServerStatusCollector creates a collector for statistics on server status. |
| 37 | +func newServerStatusCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, compatible bool, topology labelsGetter) *serverStatusCollector { |
| 38 | + return &serverStatusCollector{ |
| 39 | + ctx: ctx, |
| 40 | + base: newBaseCollector(client, logger), |
| 41 | + compatibleMode: compatible, |
| 42 | + topologyInfo: topology, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (d *serverStatusCollector) Describe(ch chan<- *prometheus.Desc) { |
| 47 | + d.base.Describe(d.ctx, ch, d.collect) |
| 48 | +} |
| 49 | + |
| 50 | +func (d *serverStatusCollector) Collect(ch chan<- prometheus.Metric) { |
| 51 | + d.base.Collect(ch) |
| 52 | +} |
| 53 | + |
| 54 | +func (d *serverStatusCollector) collect(ch chan<- prometheus.Metric) { |
| 55 | + defer prometheus.MeasureCollectTime(ch, "mongodb", "serverstatus")() |
| 56 | + |
| 57 | + logger := d.base.logger |
| 58 | + client := d.base.client |
| 59 | + |
| 60 | + cmd := bson.D{{Key: "serverStatus", Value: "1"}} |
| 61 | + res := client.Database("admin").RunCommand(d.ctx, cmd) |
| 62 | + |
| 63 | + var m bson.M |
| 64 | + if err := res.Decode(&m); err != nil { |
| 65 | + ch <- prometheus.NewInvalidMetric(prometheus.NewInvalidDesc(err), err) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + logger.Debug("serverStatus result:") |
| 70 | + debugResult(logger, m) |
| 71 | + |
| 72 | + for _, metric := range makeMetrics("", m, d.topologyInfo.baseLabels(), d.compatibleMode) { |
| 73 | + ch <- metric |
| 74 | + } |
| 75 | +} |
0 commit comments