refactor(prometheus): simplify _CustomCollector by replacing deque with Optional[MetricsData]#5258
Open
sridhar-3009 wants to merge 1 commit into
Conversation
…th single MetricsData field The callback → _receive_metrics → add_metrics_data → deque → yield chain always held at most one MetricsData per collect cycle (the callback triggers exactly one _receive_metrics call before collect drains the queue). Replace the deque[MetricsData] with an Optional[MetricsData] field and simplify collect() to consume it directly, removing the while-loop, popleft(), len() check, and the now-unused `deque` import. This fixes a latent bug where multiple items in the deque would have caused collect() to yield duplicate Prometheus metric families. Fixes open-telemetry#2500
|
|
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.
Summary
Fixes #2500 (referenced in PR #2321 discussion).
The internal
_CustomCollectorused adeque[MetricsData]to buffer metric data between the_receive_metrics → add_metrics_datacall and the Prometheuscollect()scrape. In practice the deque holds at most one item per scrape cycle because_callback()triggers exactly one_receive_metricsinvocation beforecollect()drains the queue.This PR simplifies the flow:
deque[MetricsData]withMetricsData | Noneadd_metrics_dataassigns directly instead of appendingcollect()reads and clears the single field, replacing thewhile … popleft()loopfrom collections import dequeimportThis also fixes a latent correctness issue: with the deque, if
add_metrics_datawere called multiple times between scrapes,collect()would yield duplicate metric family entries (becausemetric_family_id_metric_familyaccumulated across the loop but was yielded inside the loop).Before
After
Test plan