Skip to content

Commit f9e7fb7

Browse files
PMM-11137 fetch version from buildInfo (#604)
1 parent 692438d commit f9e7fb7

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

exporter/diagnostic_data_collector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func TestDisconnectedDiagnosticDataCollector(t *testing.T) {
292292
mongodb_mongod_storage_engine{engine="Engine is unavailable"} 1
293293
# HELP mongodb_version_info The server version
294294
# TYPE mongodb_version_info gauge
295-
mongodb_version_info{edition="",mongodb="server version is unavailable",vendor=""} 1` + "\n")
295+
mongodb_version_info{edition="",mongodb="",vendor=""} 1` + "\n")
296296
// Filter metrics for 2 reasons:
297297
// 1. The result is huge
298298
// 2. We need to check against know values. Don't use metrics that return counters like uptime

exporter/v1_compatibility.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ func specialMetrics(ctx context.Context, client *mongo.Client, m bson.M, l *logr
799799
}
800800

801801
metrics = append(metrics, storageEngine(m))
802-
metrics = append(metrics, serverVersion(m, buildInfo))
802+
metrics = append(metrics, serverVersion(buildInfo))
803803
metrics = append(metrics, myState(ctx, client))
804804

805805
if mm := replSetMetrics(m); mm != nil {
@@ -825,7 +825,6 @@ func retrieveMongoDBBuildInfo(ctx context.Context, client *mongo.Client, l *logr
825825
return buildInfo{}, errors.Wrap(err, "Failed to run buildInfo command")
826826
}
827827

828-
var edition string
829828
modules, ok := buildInfoDoc["modules"].(bson.A)
830829
if !ok {
831830
return buildInfo{}, errors.Wrap(err, "Failed to cast module information variable")
@@ -837,7 +836,7 @@ func retrieveMongoDBBuildInfo(ctx context.Context, client *mongo.Client, l *logr
837836
} else {
838837
bi.Edition = CommunityEdition
839838
}
840-
l.Debug("MongoDB edition: ", edition)
839+
l.Debug("MongoDB edition: ", bi.Edition)
841840

842841
_, ok = buildInfoDoc["psmdbVersion"]
843842
if ok {
@@ -846,6 +845,11 @@ func retrieveMongoDBBuildInfo(ctx context.Context, client *mongo.Client, l *logr
846845
bi.Vendor = MongoDBVendor
847846
}
848847

848+
bi.Version, ok = buildInfoDoc["version"].(string)
849+
if !ok {
850+
return buildInfo{}, errors.Wrap(err, "Failed to cast version information variable")
851+
}
852+
849853
return bi, nil
850854
}
851855

@@ -866,16 +870,11 @@ func storageEngine(m bson.M) prometheus.Metric {
866870
return metric
867871
}
868872

869-
func serverVersion(m bson.M, bi buildInfo) prometheus.Metric { //nolint:ireturn
870-
v := walkTo(m, []string{"serverStatus", "version"})
873+
func serverVersion(bi buildInfo) prometheus.Metric { //nolint:ireturn
871874
name := "mongodb_version_info"
872875
help := "The server version"
873876

874-
serverVersion, ok := v.(string)
875-
if !ok {
876-
serverVersion = "server version is unavailable"
877-
}
878-
labels := map[string]string{"mongodb": serverVersion, "edition": bi.Edition, "vendor": bi.Vendor}
877+
labels := map[string]string{"mongodb": bi.Version, "edition": bi.Edition, "vendor": bi.Vendor}
879878

880879
d := prometheus.NewDesc(name, help, nil, labels)
881880
metric, _ := prometheus.NewConstMetric(d, prometheus.GaugeValue, float64(1))
@@ -1354,6 +1353,7 @@ type rawStatus struct {
13541353
}
13551354

13561355
type buildInfo struct {
1356+
Version string
13571357
Edition string
13581358
Vendor string
13591359
}

0 commit comments

Comments
 (0)