Skip to content

fix(aiohttp-server): resolve tracer dynamically for legacy middleware - #5256

Open
VimalN2005 wants to merge 3 commits into
open-telemetry:mainfrom
VimalN2005:fix/aiohttp-server-legacy-middleware-tracer
Open

VimalN2005 wants to merge 3 commits into
open-telemetry:mainfrom
VimalN2005:fix/aiohttp-server-legacy-middleware-tracer

Conversation

@VimalN2005

Copy link
Copy Markdown

Description

The backwards-compatible middleware exported by opentelemetry.instrumentation.aiohttp_server is instantiated at import time and captures _tracer = None because AioHttpServerInstrumentor().instrument() has not yet executed. Later requests routed through this middleware fail with AttributeError: 'NoneType' object has no attribute 'start_as_current_span' and return HTTP 500.

Resolve the tracer dynamically at request time when _tracer was captured as None, falling back to the module tracer (or global tracer), while preserving explicit tracer provider bindings configured via create_aiohttp_middleware(tracer_provider=...).

Fixes #5164

Type of change

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

Checklist:

  • I am a human and not a bot
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Added changelog entry in .changelog/5164.fixed

@VimalN2005
VimalN2005 requested a review from a team as a code owner October 29, 2026 09:21
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-10-30 18:35 UTC

Respond to 1 review item (e.g. link a commit, explain why not, ask a follow-up):

  • Top-level threads: 1
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.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

@dipesh-svg

Copy link
Copy Markdown

Hey @VimalN2005, took a quick look. The 3 red checks are all small things:

  1. changelog: the fragment needs to be named after the PR number, not the issue, so .changelog/5164.fixed → .changelog/5256.fixed.
  2. precommit: ruff flags I001 in test_aiohttp_server_integration.py. There's an extra blank line after from opentelemetry import trace. Running pre-commit run --all-files (or ruff check --fix) fixes it.
  3. CI / check only fails because misc failed.

Since misc failed, the aiohttp-server tests got skipped, so they haven't actually run in CI yet. The branch is also behind main, so it's worth updating it too. Once it's green it'll probably get reviewed faster.

@VimalN2005

Copy link
Copy Markdown
Author

Hi @dipesh-cvg,

Thank you for the quick and helpful feedback! I have addressed all points:

  1. Renamed .changelog/5164.fixed to .changelog/5256.fixed.
  2. Fixed the extra blank line / Ruff formatting (I001) in test_aiohttp_server_integration.py.
  3. Merged the latest main into this branch.

Everything is pushed now and ready for review. Thanks again!

@dipesh-svg

Copy link
Copy Markdown

Nice, thanks for the quick update @VimalN2005! Just a heads-up: the CI workflows on the new commit are waiting for a maintainer to approve them (they show "action_required"), so the checks haven't run yet. Might be worth mentioning that in the Slack thread so someone can approve the run. Good luck with the review!

This branch has not been deployed

No deployments
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.

aiohttp-server: backwards-compatible middleware returns HTTP 500 after instrumentation

2 participants