Skip to content

Fix metrics collection timeout so slow observable callbacks raise MetricsTimeoutError - #5616

Open
henry3260 wants to merge 2 commits into
open-telemetry:mainfrom
henry3260:fix-metrics-collect-timeout
Open

Fix metrics collection timeout so slow observable callbacks raise MetricsTimeoutError#5616
henry3260 wants to merge 2 commits into
open-telemetry:mainfrom
henry3260:fix-metrics-collect-timeout

Conversation

@henry3260

Copy link
Copy Markdown
Contributor

Description

_Asynchronous.callback is a generator function, so calling it returns immediately without executing any user callback code. The deadline check in SynchronousMeasurementConsumer.collect ran right after that call, before the loop that actually drives the callbacks, so it always observed ~0 elapsed time and timeout_millis could never raise MetricsTimeoutError.

Move the deadline check after the measurement iteration, where the elapsed time is actually observable. A single slow callback now raises MetricsTimeoutError once it finishes, and remaining instruments are not invoked after the deadline passes. The existing test_collect_timeout only passed because its Mock callback slept at call time; it now uses a generator to match the real instrument's lazy behavior (it fails without the fix).

Fixes # (issue)

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Updated test_collect_timeout to use a generator callback like the real _Asynchronous.callback; it fails without the fix and passes with it.
  • pytest opentelemetry-sdk/tests/metrics/test_measurement_consumer.py (9 passed)

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

…ricsTimeoutError

_Asynchronous.callback is a generator function, so the previous deadline
check ran before any user callback code executed and could never fire.
Move the check after the measurement iteration, where the elapsed time
is actually observable, and make the unit test's mock callback a
generator to match the real instrument's lazy behavior.
@henry3260
henry3260 requested a review from a team as a code owner September 2, 2026 17:10
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Sep 2, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-09-05 10:19 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Sep 3, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: henry3260 / name: henry3260 (f8e87ab)

@henry3260
henry3260 force-pushed the fix-metrics-collect-timeout branch from 880ad20 to f8e87ab Compare September 3, 2026 16:21

@ocelotl ocelotl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only a few comments

Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/measurement_consumer.py Outdated
Comment thread opentelemetry-sdk/tests/metrics/test_measurement_consumer.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants