@@ -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 ))
@@ -1053,7 +1052,11 @@ func mongosMetrics(ctx context.Context, client *mongo.Client, l *logrus.Logger)
10531052 metrics = append (metrics , metric )
10541053 }
10551054
1056- metrics = append (metrics , balancerEnabled (ctx , client ))
1055+ if metric , err := balancerEnabled (ctx , client ); err != nil {
1056+ l .Debugf ("cannot create metric for balancer is enabled: %s" , err )
1057+ } else {
1058+ metrics = append (metrics , metric )
1059+ }
10571060
10581061 metric , err := chunksTotal (ctx , client )
10591062 if err != nil {
@@ -1164,17 +1167,19 @@ func chunksBalanced(ctx context.Context, client *mongo.Client) (prometheus.Metri
11641167 return prometheus .NewConstMetric (d , prometheus .GaugeValue , value )
11651168}
11661169
1167- func balancerEnabled (ctx context.Context , client * mongo.Client ) prometheus.Metric {
1170+ func balancerEnabled (ctx context.Context , client * mongo.Client ) ( prometheus.Metric , error ) {
11681171 type bss struct {
1169- stopped bool `bson:"stopped"`
1172+ Stopped bool `bson:"stopped"`
11701173 }
11711174 var bs bss
11721175 enabled := 0
11731176
11741177 err := client .Database ("config" ).Collection ("settings" ).FindOne (ctx , bson.M {"_id" : "balancer" }).Decode (& bs )
11751178 if err != nil {
1176- enabled = 1
1177- } else if ! bs .stopped {
1179+ return nil , err
1180+ }
1181+
1182+ if ! bs .Stopped {
11781183 enabled = 1
11791184 }
11801185
@@ -1184,7 +1189,7 @@ func balancerEnabled(ctx context.Context, client *mongo.Client) prometheus.Metri
11841189 d := prometheus .NewDesc (name , help , nil , nil )
11851190 metric , _ := prometheus .NewConstMetric (d , prometheus .GaugeValue , float64 (enabled ))
11861191
1187- return metric
1192+ return metric , nil
11881193}
11891194
11901195func chunksTotal (ctx context.Context , client * mongo.Client ) (prometheus.Metric , error ) {
@@ -1348,6 +1353,7 @@ type rawStatus struct {
13481353}
13491354
13501355type buildInfo struct {
1356+ Version string
13511357 Edition string
13521358 Vendor string
13531359}
0 commit comments