Skip to content

Conversation

@tammy-baylis-swi
Copy link
Contributor

@tammy-baylis-swi tammy-baylis-swi commented Nov 1, 2025

Description

When exemplars are opted into via metrics SDK, this fixes FlaskInstrumentor generation of exemplars for http.server.request.duration and http.server.duration (new and old semconv respectively) so trace ID and span ID are no longer empty.

How? The metrics SDK implementation depends on its received OTel context being valid with a real span context inside it -- see ExemplarBucket.offer. Some metrics like these from FlaskInstrumentor don't have their values known until spans are complete and no longer in the current context, so metrics are recorded after spans ended. We now pass a new OTel context containing the Flask-stashed span.

Fixes #3913

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Added unit tests
  • Added new Flask docker test
  • Installed changes to instrumented app

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

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

@tammy-baylis-swi tammy-baylis-swi marked this pull request as ready for review November 1, 2025 01:59
@tammy-baylis-swi tammy-baylis-swi requested a review from a team as a code owner November 1, 2025 01:59
@xrmx xrmx moved this to Ready for review in @xrmx's Python PR digest Nov 3, 2025
# Get the span from wrapped_app_environ and re-create context manually
# to pass to histogram for exemplars generation
span = wrapped_app_environ.get(_ENVIRON_SPAN_KEY)
if span:
Copy link
Contributor

@xrmx xrmx Nov 12, 2025

Choose a reason for hiding this comment

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

What do you think of doing something like metrics_context = context.set_value(_SPAN_KEY, span) if span else None and reuse the same record call to keep the code more compact?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! Changed in 6e38a77

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready for review

Development

Successfully merging this pull request may close these issues.

FlaskInstrumentor http.server.request.duration exemplars are missing span context

3 participants