fix(metrics): shutdown periodic readers in tests and handle unmapped aggregations (#5157) - #5622
Open
dlowzzxx wants to merge 3 commits into
Conversation
…te double shutdown warning
Pull request dashboard statusWaiting on reviewers · refreshed 2026-09-06 15:21 UTC Review the latest changes. Status above doesn't look right?
|
Contributor
|
please sign the CLA |
Contributor
|
how does this PR fix #5157? |
Author
|
@ocelotl Thank you for reviewing! Regarding how this PR addresses #5157: The traceback captured in #5157 shows two symptoms occurring together:
These two failures share a single root cause:
This PR fixes both:
|
Author
|
/dashboard route:reviewers |
|
@dlowzzxx, this pull request was routed to reviewers. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #5157
Root Cause
opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/metric_reader_storage.py,MetricReaderStorage.collect()iterates through view instrument matches and assignsdatadepending on the aggregation instance. When an unmapped or mock aggregation was encountered, no branch was taken, leavingdataunassigned and resulting inUnboundLocalError: cannot access local variable 'data' where it is not associated with a valuewhen constructingMetric(...).opentelemetry-sdk/tests/metrics/test_periodic_exporting_metric_reader.py,test_exporter_temporality_preferenceandtest_exporter_aggregation_preferenceinitialized aPeriodicExportingMetricReaderwhich started a background daemon thread (_ticker), but never shut downpmr. Similarly, intest_metric_reader_metrics,mp.shutdown()was not guaranteed to execute in afinallyblock if an assertion failed. These leaking daemon threads continued ticking during subsequent tests (such astest_boundary_statisticsandtest_creates_view_instrument_matches) and triggered unhandled thread exceptions in the background.Changes
else:fallback in the aggregationisinstancechain that logs a warning (_logger.warning(...)) and skips the match (continue), preventing unassigneddatafallthrough. Removed# pylint: disable=possibly-used-before-assignment.try...finally: pmr.shutdown()fortest_exporter_temporality_preferenceandtest_exporter_aggregation_preference, and ensuredmp.shutdown()(andpmr.shutdown()if not yet stopped) runs inside afinallyblock intest_metric_reader_metrics.test_collect_skips_unsupported_aggregationtotest_metric_reader_storage.pyto assert that unsupported aggregations log a warning and are cleanly skipped without raisingUnboundLocalError..changelog/5622.fixed(matching towncrier and CI PR number requirements).Testing
uv run pytest opentelemetry-sdk/tests/metrics/test_metric_reader_storage.pypassed (18 passed).uv run pytest opentelemetry-sdk/tests/metrics/test_periodic_exporting_metric_reader.pypassed (13 passed, 1 skipped for fork).uv run pytest opentelemetry-sdk/tests/metrics/passed (322 passed, 0 failures).uv run towncrier check --compare-with origin/mainpassed cleanly.uv run ruff checkanduv run ruff format --checkpassed with zero violations.