Skip to content

fix(test_tools): Asserting unlabeled metrics in assert_metric results in AttributeError#224

Merged
khvn26 merged 1 commit into
mainfrom
fix/assert-metric-unlabeled
May 11, 2026
Merged

fix(test_tools): Asserting unlabeled metrics in assert_metric results in AttributeError#224
khvn26 merged 1 commit into
mainfrom
fix/assert-metric-unlabeled

Conversation

@khvn26
Copy link
Copy Markdown
Member

@khvn26 khvn26 commented May 9, 2026

MetricWrapperBase.clear() only works on parent metrics (those declared with labels). Calling it on an unlabeled metric raises AttributeError because _lock is only initialised on parents.

assert_metric_impl iterates every collector in the registry and calls clear() to reset state between tests, so any unlabeled metric registered in the test session would crash the fixture for downstream consumers.

This PR resets unlabeled metrics' observable state via _metric_init instead, which recreates _value (Counter) / _buckets+_sum (Histogram) etc. fresh.

Test plan

  • New regression test test_assert_metrics__unlabeled_metric_incremented__asserts_expected exercises an unlabeled counter through assert_metric. Verified failing on main and passing on this branch.
  • Existing test_assert_metrics__metric_incremented__asserts_expected and test_assert_metrics__after_registry_reset__raises_assertion still pass.

`MetricWrapperBase.clear()` only works on parent metrics — the ones
declared with labels. Calling it on an unlabeled metric raises
AttributeError because `_lock` is only set on parents.

`assert_metric_impl` iterates every collector in the registry and calls
`clear()`, so any unlabeled metric registered in the test session would
crash the fixture. Reset unlabeled metrics' observable state via
`_metric_init` instead, which recreates `_value` (Counter) /
`_buckets`+`_sum` (Histogram) / etc. fresh.

beep boop
@khvn26 khvn26 requested a review from a team as a code owner May 9, 2026 21:35
@khvn26 khvn26 requested review from gagantrivedi and removed request for a team May 9, 2026 21:35
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.38%. Comparing base (1c32dcf) to head (b15979d).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #224   +/-   ##
=======================================
  Coverage   97.38%   97.38%           
=======================================
  Files         105      105           
  Lines        4586     4595    +9     
=======================================
+ Hits         4466     4475    +9     
  Misses        120      120           

☔ View full report in Codecov by Sentry.
📢 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.

@khvn26 khvn26 changed the title fix(test_tools): Reset unlabeled metrics in assert_metric fix(test_tools): Asserting unlabeled metrics in assert_metric results in AttributeError May 9, 2026
@khvn26 khvn26 self-assigned this May 11, 2026
@khvn26 khvn26 merged commit 8a53e67 into main May 11, 2026
4 checks passed
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.

4 participants