|
| 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 | + |
| 21 | + "github.com/pkg/errors" |
| 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 replSetGetConfigCollector struct { |
| 29 | + ctx context.Context |
| 30 | + base *baseCollector |
| 31 | + |
| 32 | + compatibleMode bool |
| 33 | + topologyInfo labelsGetter |
| 34 | +} |
| 35 | + |
| 36 | +// newReplicationSetConfigCollector creates a collector for configuration of replication set. |
| 37 | +func newReplicationSetConfigCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, compatible bool, topology labelsGetter) *replSetGetConfigCollector { |
| 38 | + return &replSetGetConfigCollector{ |
| 39 | + ctx: ctx, |
| 40 | + base: newBaseCollector(client, logger.WithFields(logrus.Fields{"collector": "replset_config"})), |
| 41 | + |
| 42 | + compatibleMode: compatible, |
| 43 | + topologyInfo: topology, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (d *replSetGetConfigCollector) Describe(ch chan<- *prometheus.Desc) { |
| 48 | + d.base.Describe(d.ctx, ch, d.collect) |
| 49 | +} |
| 50 | + |
| 51 | +func (d *replSetGetConfigCollector) Collect(ch chan<- prometheus.Metric) { |
| 52 | + d.base.Collect(ch) |
| 53 | +} |
| 54 | + |
| 55 | +func (d *replSetGetConfigCollector) collect(ch chan<- prometheus.Metric) { |
| 56 | + defer measureCollectTime(ch, "mongodb", "replset_config")() |
| 57 | + |
| 58 | + logger := d.base.logger |
| 59 | + client := d.base.client |
| 60 | + |
| 61 | + cmd := bson.D{{Key: "replSetGetConfig", Value: "1"}} |
| 62 | + res := client.Database("admin").RunCommand(d.ctx, cmd) |
| 63 | + |
| 64 | + var m bson.M |
| 65 | + |
| 66 | + if err := res.Decode(&m); err != nil { |
| 67 | + if e, ok := err.(mongo.CommandError); ok { //nolint // https://github.com/percona/mongodb_exporter/pull/295#issuecomment-922874632 |
| 68 | + if e.Code == replicationNotYetInitialized || e.Code == replicationNotEnabled { |
| 69 | + return |
| 70 | + } |
| 71 | + } |
| 72 | + logger.Errorf("cannot get replSetGetConfig: %s", err) |
| 73 | + |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + config, ok := m["config"].(bson.M) |
| 78 | + if !ok { |
| 79 | + err := errors.Wrapf(errUnexpectedDataType, "%T for data field", m["config"]) |
| 80 | + logger.Errorf("cannot decode getDiagnosticData: %s", err) |
| 81 | + |
| 82 | + return |
| 83 | + } |
| 84 | + m = config |
| 85 | + |
| 86 | + logger.Debug("replSetGetConfig result:") |
| 87 | + debugResult(logger, m) |
| 88 | + |
| 89 | + for _, metric := range makeMetrics("rs_cfg", m, d.topologyInfo.baseLabels(), d.compatibleMode) { |
| 90 | + ch <- metric |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +var _ prometheus.Collector = (*replSetGetConfigCollector)(nil) |
0 commit comments