Skip to content

Commit c4132a6

Browse files
authored
PMM-11247: Do not create a metric mongodb_mongod_storage_engine if it's not possible to retrieve (#612)
* PMM-11247: Do not create a metric mongodb_mongod_storage_engine if it's not possible to retrieve For example, we can't get the type of engine for mongos instance. So in this case we can skip creating this metric. * PMM-11247: Ignore lint issue
1 parent 917800b commit c4132a6

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

exporter/diagnostic_data_collector_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ func TestDiagnosticDataCollectorWithCompatibleMode(t *testing.T) {
9696

9797
// The last \n at the end of this string is important
9898
expected := strings.NewReader(fmt.Sprintf(`
99+
# HELP mongodb_mongod_storage_engine The storage engine used by the MongoDB instance
100+
# TYPE mongodb_mongod_storage_engine gauge
101+
mongodb_mongod_storage_engine{engine="wiredTiger"} 1
99102
# HELP mongodb_version_info The server version
100103
# TYPE mongodb_version_info gauge
101104
mongodb_version_info{edition="Community",mongodb="%s",vendor="%s"} 1`, version, vendor) + "\n")
@@ -105,6 +108,7 @@ func TestDiagnosticDataCollectorWithCompatibleMode(t *testing.T) {
105108
// 2. We need to check against know values. Don't use metrics that return counters like uptime
106109
// or counters like the number of transactions because they won't return a known value to compare
107110
filter := []string{
111+
"mongodb_mongod_storage_engine",
108112
"mongodb_version_info",
109113
}
110114

@@ -287,9 +291,6 @@ func TestDisconnectedDiagnosticDataCollector(t *testing.T) {
287291
# HELP mongodb_mongod_replset_my_state An integer between 0 and 10 that represents the replica state of the current member
288292
# TYPE mongodb_mongod_replset_my_state gauge
289293
mongodb_mongod_replset_my_state{set=""} 6
290-
# HELP mongodb_mongod_storage_engine The storage engine used by the MongoDB instance
291-
# TYPE mongodb_mongod_storage_engine gauge
292-
mongodb_mongod_storage_engine{engine="Engine is unavailable"} 1
293294
# HELP mongodb_version_info The server version
294295
# TYPE mongodb_version_info gauge
295296
mongodb_version_info{edition="",mongodb="",vendor=""} 1` + "\n")

exporter/v1_compatibility.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,11 @@ func specialMetrics(ctx context.Context, client *mongo.Client, m bson.M, l *logr
798798
l.Errorf("cannot retrieve MongoDB buildInfo: %s", err)
799799
}
800800

801-
metrics = append(metrics, storageEngine(m))
801+
if engine, err := storageEngine(m); err != nil {
802+
l.Errorf("cannot retrieve engine type: %s", err)
803+
} else {
804+
metrics = append(metrics, engine)
805+
}
802806
metrics = append(metrics, serverVersion(buildInfo))
803807
metrics = append(metrics, myState(ctx, client))
804808

@@ -853,21 +857,23 @@ func retrieveMongoDBBuildInfo(ctx context.Context, client *mongo.Client, l *logr
853857
return bi, nil
854858
}
855859

856-
func storageEngine(m bson.M) prometheus.Metric {
860+
func storageEngine(m bson.M) (prometheus.Metric, error) { //nolint:ireturn
857861
v := walkTo(m, []string{"serverStatus", "storageEngine", "name"})
858862
name := "mongodb_mongod_storage_engine"
859863
help := "The storage engine used by the MongoDB instance"
860864

861865
engine, ok := v.(string)
862866
if !ok {
863-
engine = "Engine is unavailable"
867+
return nil, errors.New("Engine is unavailable")
864868
}
865869
labels := map[string]string{"engine": engine}
866870

867871
d := prometheus.NewDesc(name, help, nil, labels)
868-
metric, _ := prometheus.NewConstMetric(d, prometheus.GaugeValue, float64(1))
869-
870-
return metric
872+
metric, err := prometheus.NewConstMetric(d, prometheus.GaugeValue, float64(1))
873+
if err != nil {
874+
return nil, errors.Wrap(err, "cannot create metric for engine type")
875+
}
876+
return metric, nil
871877
}
872878

873879
func serverVersion(bi buildInfo) prometheus.Metric { //nolint:ireturn

0 commit comments

Comments
 (0)