fix(observability): idempotent metric registration (v26.06.97)#127
Merged
Conversation
…oss apps); release v26.06.97
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.
Problem
MetricsRegistrycached its Prometheus collectors in per-instance dicts(
self._counters/self._histograms/self._gaugescreated in__init__), butprometheus_clientregisters everyCounter/Histogram/Gaugeon theprocess-global default
REGISTRYkeyed by metric name.So a second
MetricsRegistry— e.g. a secondcreate_app()in one pytest process —re-created an already-registered collector and prometheus raised:
Downstream
flymgmtservices worked around this with a per-repoconftestfixture thatreset the registry between apps. This release fixes it at the source so that workaround
can be retired.
Fix
Move the collector caches to module level (
_COUNTERS/_HISTOGRAMS/_GAUGES),making get-or-create idempotent across every
MetricsRegistryin the process — matchingprometheus's one-collector-per-name model.
MetricsRegistry.__init__keeps the_HAS_PROMETHEUSguard, drops the per-instance dicts.counter()/histogram()/gauge()read+write the module-level caches./metricsexposition — production behaviorunchanged; this only dedupes process-wide (no per-instance
CollectorRegistry).dict[str, Counter]etc.) somypy --strictstays clean.Regression test
tests/observability/test_metrics_idempotent.py— twoMetricsRegistryinstances calling.counter()/.histogram()/.gauge()with the same name do not raise and return thesame (
is) collector object (the exact scenario that used to raise "Duplicated timeseries").Two existing tests that poked the removed
_countersdict now use the public.counter()accessor (which returns the same cached collector).
Gates (all green locally)
ruff check src/ tests/— passruff format --check src/ tests/— passmypy src/pyfly --strict— Success: no issues found in 673 source filespytest tests/observability -q— 31 passedpytest tests/container tests/context tests/observability -q— 377 passed