From ed07046aafee2c95eef1cccf0818eb55f3996edd Mon Sep 17 00:00:00 2001 From: nisarg14 Date: Thu, 28 May 2026 23:22:50 +0530 Subject: [PATCH 1/5] Fix invertedmongodb_mongos_sharding_chunks_is_balancer_running gauge --- exporter/v1_compatibility.go | 2 +- exporter/v1_compatibility_test.go | 35 ++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/exporter/v1_compatibility.go b/exporter/v1_compatibility.go index a7eb97731..be17baa91 100644 --- a/exporter/v1_compatibility.go +++ b/exporter/v1_compatibility.go @@ -1178,7 +1178,7 @@ func chunksBalancerRunning(ctx context.Context, client *mongo.Client) (prometheu } value := float64(0) - if !m.InBalancerRound { + if m.InBalancerRound { value = 1 } diff --git a/exporter/v1_compatibility_test.go b/exporter/v1_compatibility_test.go index ad73c72af..fd3a6c1aa 100644 --- a/exporter/v1_compatibility_test.go +++ b/exporter/v1_compatibility_test.go @@ -213,7 +213,7 @@ func TestCreateOldMetricFromNew(t *testing.T) { func TestMongosMetrics(t *testing.T) { t.Parallel() - t.Run("test mongodb_mongos_sharding_chunks_is_balancer_running metric", func(t *testing.T) { + t.Run("test mongodb_mongos_sharding_balancer_enabled metric", func(t *testing.T) { type bss struct { Mode string `bson:"mode"` } @@ -245,6 +245,39 @@ func TestMongosMetrics(t *testing.T) { } assert.Equal(t, float64(expected), m.GetGauge().GetValue()) //nolint }) + + t.Run("test mongodb_mongos_sharding_chunks_is_balancer_running metric", func(t *testing.T) { + type bss struct { + InBalancerRound bool `bson:"inBalancerRound"` + } + + t.Parallel() + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + port, err := tu.PortForContainer("mongos") + require.NoError(t, err) + client := tu.TestClient(ctx, port, t) + + var bs bss + cmd := bson.D{{Key: "balancerStatus", Value: "1"}} + err = client.Database("admin").RunCommand(ctx, cmd).Decode(&bs) + require.NoError(t, err) + + var metric prometheus.Metric + var m dto.Metric + metric, err = chunksBalancerRunning(ctx, client) + assert.NoError(t, err) + + err = metric.Write(&m) + assert.NoError(t, err) + + expected := 0 + if bs.InBalancerRound { + expected = 1 + } + assert.Equal(t, float64(expected), m.GetGauge().GetValue()) //nolint + }) } // myState should always return a metric. If there is no connection, the value From 933863bfafff546939479be060937768cefbcf1e Mon Sep 17 00:00:00 2001 From: Nisarg Thoriya Date: Wed, 3 Jun 2026 00:05:58 +0530 Subject: [PATCH 2/5] extracted bool->value mapping in pure function and added unit test for the same --- exporter/v1_compatibility.go | 13 +++++++++---- exporter/v1_compatibility_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/exporter/v1_compatibility.go b/exporter/v1_compatibility.go index be17baa91..cddf8fd66 100644 --- a/exporter/v1_compatibility.go +++ b/exporter/v1_compatibility.go @@ -1165,6 +1165,13 @@ func shardedCollectionsTotal(ctx context.Context, client *mongo.Client) (prometh return prometheus.NewConstMetric(d, prometheus.GaugeValue, float64(collCount)) } +func balancerRunningValue(inBalancerRound bool) float64 { + if inBalancerRound { + return 1 + } + return 0 +} + func chunksBalancerRunning(ctx context.Context, client *mongo.Client) (prometheus.Metric, error) { var m struct { InBalancerRound bool `bson:"inBalancerRound"` @@ -1177,10 +1184,8 @@ func chunksBalancerRunning(ctx context.Context, client *mongo.Client) (prometheu return nil, err } - value := float64(0) - if m.InBalancerRound { - value = 1 - } + value := balancerRunningValue(m.inBalancerRound) + name := "mongodb_mongos_sharding_chunks_is_balancer_running" help := "Shard balancer is in a balancing round" diff --git a/exporter/v1_compatibility_test.go b/exporter/v1_compatibility_test.go index fd3a6c1aa..afc844554 100644 --- a/exporter/v1_compatibility_test.go +++ b/exporter/v1_compatibility_test.go @@ -211,6 +211,36 @@ func TestCreateOldMetricFromNew(t *testing.T) { assert.Equal(t, want, nm) } +func TestBalancerRunningValue(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + inBalancerRound bool + expected float64 + }{ + { + name: "balancer running", + inBalancerRound: true, + expected: 1, + }, + { + name: "balancer not running", + inBalancerRound: false, + expected: 0, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + got := balancerRunningValue(tt.inBalancerRound) + assert.Equal(t, tt.expected, got) + }) + } +} + func TestMongosMetrics(t *testing.T) { t.Parallel() t.Run("test mongodb_mongos_sharding_balancer_enabled metric", func(t *testing.T) { From fd2a010bf291c57132891f5caf93b199a3616623 Mon Sep 17 00:00:00 2001 From: Nisarg Thoriya Date: Sat, 6 Jun 2026 17:46:45 +0530 Subject: [PATCH 3/5] Fix field reference casing in chunksBalancerRunning --- exporter/v1_compatibility.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/v1_compatibility.go b/exporter/v1_compatibility.go index cddf8fd66..4c2e1e842 100644 --- a/exporter/v1_compatibility.go +++ b/exporter/v1_compatibility.go @@ -1184,7 +1184,7 @@ func chunksBalancerRunning(ctx context.Context, client *mongo.Client) (prometheu return nil, err } - value := balancerRunningValue(m.inBalancerRound) + value := balancerRunningValue(m.InBalancerRound) name := "mongodb_mongos_sharding_chunks_is_balancer_running" From 9d53c8244ebf58eef15a743905919def07f7ae08 Mon Sep 17 00:00:00 2001 From: Alex Demidoff Date: Tue, 16 Jun 2026 23:09:46 +0300 Subject: [PATCH 4/5] Update exporter/v1_compatibility_test.go --- exporter/v1_compatibility_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/exporter/v1_compatibility_test.go b/exporter/v1_compatibility_test.go index afc844554..7fd20ee2b 100644 --- a/exporter/v1_compatibility_test.go +++ b/exporter/v1_compatibility_test.go @@ -232,7 +232,6 @@ func TestBalancerRunningValue(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() got := balancerRunningValue(tt.inBalancerRound) From 16320bfe14e25dce19add40f48d19896b9d87cdb Mon Sep 17 00:00:00 2001 From: Nisarg Thoriya Date: Thu, 18 Jun 2026 22:22:04 +0530 Subject: [PATCH 5/5] format source code for Fix inverted mongodb_mongos_sharding_chunks_is_balancer_running gauge --- exporter/v1_compatibility.go | 1 - exporter/v1_compatibility_test.go | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/exporter/v1_compatibility.go b/exporter/v1_compatibility.go index 4c2e1e842..f09f1a6bb 100644 --- a/exporter/v1_compatibility.go +++ b/exporter/v1_compatibility.go @@ -1186,7 +1186,6 @@ func chunksBalancerRunning(ctx context.Context, client *mongo.Client) (prometheu value := balancerRunningValue(m.InBalancerRound) - name := "mongodb_mongos_sharding_chunks_is_balancer_running" help := "Shard balancer is in a balancing round" diff --git a/exporter/v1_compatibility_test.go b/exporter/v1_compatibility_test.go index 7fd20ee2b..5ad036a2a 100644 --- a/exporter/v1_compatibility_test.go +++ b/exporter/v1_compatibility_test.go @@ -215,9 +215,9 @@ func TestBalancerRunningValue(t *testing.T) { t.Parallel() tests := []struct { - name string - inBalancerRound bool - expected float64 + name string + inBalancerRound bool + expected float64 }{ { name: "balancer running",