Skip to content

Fix inverted mongodb_mongos_sharding_chunks_is_balancer_running gauge#1295

Open
nisarg14 wants to merge 7 commits into
percona:mainfrom
nisarg14:fix/in-balancer-round-metric
Open

Fix inverted mongodb_mongos_sharding_chunks_is_balancer_running gauge#1295
nisarg14 wants to merge 7 commits into
percona:mainfrom
nisarg14:fix/in-balancer-round-metric

Conversation

@nisarg14

Copy link
Copy Markdown

Fix inverted mongodb_mongos_sharding_chunks_is_balancer_running gauge

Summary

  • Fixes chunksBalancerRunning() so mongodb_mongos_sharding_chunks_is_balancer_running is 1 when MongoDB balancerStatus.inBalancerRound is true, and 0 otherwise.

  • Previously the condition was negated (!m.InBalancerRound), so the gauge reported the opposite of the metric name and help text (“Shard balancer is in a balancing round”).

  • Renames the existing TestMongosMetrics subtest to match what it actually tests (balancer_enabled via mode).

  • Adds a subtest that exercises chunksBalancerRunning() against inBalancerRound from balancerStatus.

@nisarg14 nisarg14 requested a review from a team as a code owner May 28, 2026 18:03
@nisarg14 nisarg14 requested review from JiriCtvrtka and ademidoff and removed request for a team May 28, 2026 18:03
@nisarg14 nisarg14 changed the title Fix invertedmongodb_mongos_sharding_chunks_is_balancer_running gauge Fix inverted mongodb_mongos_sharding_chunks_is_balancer_running gauge May 28, 2026
@nisarg14

nisarg14 commented May 28, 2026

Copy link
Copy Markdown
Author

Please add pmm-review-exporters as reviewers per CONTRIBUTING.md

@codecov

codecov Bot commented Jun 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.82%. Comparing base (dc46ed5) to head (9d53c82).
⚠️ Report is 233 commits behind head on main.

❗ There is a different number of reports uploaded between BASE (dc46ed5) and HEAD (9d53c82). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (dc46ed5) HEAD (9d53c82)
agent 10 8
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1295      +/-   ##
==========================================
- Coverage   70.88%   65.82%   -5.07%     
==========================================
  Files          28       29       +1     
  Lines        3569     2481    -1088     
==========================================
- Hits         2530     1633     -897     
+ Misses        904      702     -202     
- Partials      135      146      +11     
Flag Coverage Δ
agent 65.82% <100.00%> (-5.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ademidoff ademidoff left a comment

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.

Confirmed the production fix is correct. The inversion is a regression from PMM-12962 (15688e3), which renamed chunksBalanced/is_balanced ("Shards are balanced", correctly !inBalancerRound) to chunksBalancerRunning/is_balancer_running but kept the !m.InBalancerRound logic, inverting the new semantics. This restores the intended behavior (the metric is only emitted under --compatible-mode). One note on the new test below.

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

@ademidoff

Copy link
Copy Markdown
Member

Please add pmm-review-exporters as reviewers per CONTRIBUTING.md

That group was removed, so we are good - CONTRIBUTING.md is updated and we have reviewers assigned :)

Comment thread exporter/v1_compatibility_test.go Outdated
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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants