|
| 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 | + "errors" |
| 21 | + "strconv" |
| 22 | + |
| 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 currentopCollector struct { |
| 31 | + ctx context.Context |
| 32 | + base *baseCollector |
| 33 | + compatibleMode bool |
| 34 | + topologyInfo labelsGetter |
| 35 | +} |
| 36 | + |
| 37 | +var ErrInvalidOrMissingInprogEntry = errors.New("invalid or missing inprog entry in currentop results") |
| 38 | + |
| 39 | +// newCurrentopCollector creates a collector for being processed queries. |
| 40 | +func newCurrentopCollector(ctx context.Context, client *mongo.Client, logger *logrus.Logger, |
| 41 | + compatible bool, topology labelsGetter, |
| 42 | +) *currentopCollector { |
| 43 | + return ¤topCollector{ |
| 44 | + ctx: ctx, |
| 45 | + base: newBaseCollector(client, logger), |
| 46 | + compatibleMode: compatible, |
| 47 | + topologyInfo: topology, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (d *currentopCollector) Describe(ch chan<- *prometheus.Desc) { |
| 52 | + d.base.Describe(d.ctx, ch, d.collect) |
| 53 | +} |
| 54 | + |
| 55 | +func (d *currentopCollector) Collect(ch chan<- prometheus.Metric) { |
| 56 | + d.base.Collect(ch) |
| 57 | +} |
| 58 | + |
| 59 | +func (d *currentopCollector) collect(ch chan<- prometheus.Metric) { |
| 60 | + defer measureCollectTime(ch, "mongodb", "currentop")() |
| 61 | + |
| 62 | + logger := d.base.logger |
| 63 | + client := d.base.client |
| 64 | + |
| 65 | + // Get all requests that are being processed except system requests (admin and local). |
| 66 | + cmd := bson.D{ |
| 67 | + {Key: "currentOp", Value: true}, |
| 68 | + {Key: "active", Value: true}, |
| 69 | + {Key: "microsecs_running", Value: bson.D{ |
| 70 | + {Key: "$exists", Value: true}, |
| 71 | + }}, |
| 72 | + {Key: "op", Value: bson.D{{Key: "$ne", Value: ""}}}, |
| 73 | + {Key: "ns", Value: bson.D{ |
| 74 | + {Key: "$ne", Value: ""}, |
| 75 | + {Key: "$not", Value: bson.D{{Key: "$regex", Value: "^admin.*|^local.*"}}}, |
| 76 | + }}, |
| 77 | + } |
| 78 | + res := client.Database("admin").RunCommand(d.ctx, cmd) |
| 79 | + |
| 80 | + var r primitive.M |
| 81 | + if err := res.Decode(&r); err != nil { |
| 82 | + ch <- prometheus.NewInvalidMetric(prometheus.NewInvalidDesc(err), err) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + logger.Debug("currentop response from MongoDB:") |
| 87 | + debugResult(logger, r) |
| 88 | + |
| 89 | + inprog, ok := r["inprog"].(primitive.A) |
| 90 | + |
| 91 | + if !ok { |
| 92 | + ch <- prometheus.NewInvalidMetric(prometheus.NewInvalidDesc(ErrInvalidOrMissingInprogEntry), |
| 93 | + ErrInvalidOrMissingInprogEntry) |
| 94 | + } |
| 95 | + |
| 96 | + for _, bsonMap := range inprog { |
| 97 | + |
| 98 | + bsonMapElement, ok := bsonMap.(primitive.M) |
| 99 | + if !ok { |
| 100 | + logger.Errorf("Invalid type primitive.M assertion for bsonMap: %T", bsonMapElement) |
| 101 | + continue |
| 102 | + } |
| 103 | + opid, ok := bsonMapElement["opid"].(int32) |
| 104 | + if !ok { |
| 105 | + logger.Errorf("Invalid type int32 assertion for 'opid': %T", bsonMapElement) |
| 106 | + continue |
| 107 | + } |
| 108 | + namespace, ok := bsonMapElement["ns"].(string) |
| 109 | + if !ok { |
| 110 | + logger.Errorf("Invalid type string assertion for 'ns': %T", bsonMapElement) |
| 111 | + continue |
| 112 | + } |
| 113 | + db, collection := splitNamespace(namespace) |
| 114 | + op, ok := bsonMapElement["op"].(string) |
| 115 | + if !ok { |
| 116 | + logger.Errorf("Invalid type string assertion for 'op': %T", bsonMapElement) |
| 117 | + continue |
| 118 | + } |
| 119 | + desc, ok := bsonMapElement["desc"].(string) |
| 120 | + if !ok { |
| 121 | + logger.Errorf("Invalid type string assertion for 'desc': %T", bsonMapElement) |
| 122 | + continue |
| 123 | + } |
| 124 | + microsecs_running, ok := bsonMapElement["microsecs_running"].(int64) |
| 125 | + if !ok { |
| 126 | + logger.Errorf("Invalid type int64 assertion for 'microsecs_running': %T", bsonMapElement) |
| 127 | + continue |
| 128 | + } |
| 129 | + |
| 130 | + labels := d.topologyInfo.baseLabels() |
| 131 | + labels["opid"] = strconv.Itoa(int(opid)) |
| 132 | + labels["op"] = op |
| 133 | + labels["desc"] = desc |
| 134 | + labels["database"] = db |
| 135 | + labels["collection"] = collection |
| 136 | + labels["ns"] = namespace |
| 137 | + |
| 138 | + m := primitive.M{"uptime": microsecs_running} |
| 139 | + |
| 140 | + for _, metric := range makeMetrics("currentop_query", m, labels, d.compatibleMode) { |
| 141 | + ch <- metric |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments