diff --git a/exporter/v1_compatibility.go b/exporter/v1_compatibility.go index a7eb97731..f09f1a6bb 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,7 @@ 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 ad73c72af..5ad036a2a 100644 --- a/exporter/v1_compatibility_test.go +++ b/exporter/v1_compatibility_test.go @@ -211,9 +211,38 @@ 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 { + 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_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 +274,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