Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions exporter/v1_compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"
Expand Down
64 changes: 63 additions & 1 deletion exporter/v1_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expected computation mirrors the function under test exactly (chunksBalancerRunning applies the same if inBalancerRound { 1 } mapping), so the assertion is tautological. It does guard against a re-introduced ! inversion, but on an idle test cluster inBalancerRound is effectively always false, so only the → 0 path is ever exercised — the → 1 path (the behavior this PR fixes) is never covered. Consider extracting the bool→value mapping into a pure function and unit-testing both inputs, or at minimum add a comment noting this limitation.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. I’ve now extracted the bool->value mapping into a pure function and added a unit test covering both true and false cases. The integration test is kept only to verify the MongoDB response->metric wiring.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. One final fix - let's run make format, commit and push - it should make the linter happy :)

@nisarg14 nisarg14 Jun 19, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, i've ran make format and pushed the code

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gentle reminder @ademidoff i've ran the command can you please review

expected = 1
}
assert.Equal(t, float64(expected), m.GetGauge().GetValue()) //nolint
})
}

// myState should always return a metric. If there is no connection, the value
Expand Down