open-telemetry / open-telemetry/opentelemetry-python-contrib

FastAPI `uninstrument` disallows adding more middlewares with `fastapi@>=0.91` and `starlette@>=0.24`

Open
#2,042 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
1.1k
Forks
1.1k
Avg merge
4d 15h
Merged PRs (30d)
16

Description

Uninstrument is breaking subsequent calls to add_middleware due to recent changes in starlette https://github.com/encode/starlette/pull/2017 and latest version of fastapi@>=0.91.

Call to build_middleware_stack makes an instance of FastAPI and starelette to think that application is already running

Describe your environment

Python 3.9.9

opentelemetry-distro = "^0.41b0"
opentelemetry-exporter-otlp = "^1.20.0"
opentelemetry-instrumentation-aws-lambda = "^0.41b0"
opentelemetry-instrumentation-dbapi = "^0.41b0"
opentelemetry-instrumentation-logging = "^0.41b0"
opentelemetry-instrumentation-sqlite3 = "^0.41b0"
opentelemetry-instrumentation-urllib = "^0.41b0"
opentelemetry-instrumentation-wsgi = "^0.41b0"
opentelemetry-instrumentation-asgi = "^0.41b0"
opentelemetry-instrumentation-fastapi = "^0.41b0"
opentelemetry-instrumentation-grpc = "^0.41b0"
opentelemetry-instrumentation-httpx = "^0.41b0"
opentelemetry-instrumentation-requests = "^0.41b0"
opentelemetry-instrumentation-sqlalchemy = "^0.41b0"
opentelemetry-instrumentation-urllib3 = "^0.41b0"

Steps to reproduce
Describe exactly how to reproduce the error. Include a code sample if applicable.

from starlette.middleware.cors import CORSMiddleware
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor

app = FastAPI(...)

FastAPIInstrumentor.uninstrument_app(app)

app.add_middleware(
    CORSMiddleware,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

What is the expected behavior?
What did you expect to see?

FastAPIInstrumentor.uninstrument_app should not break ability to add more middlewares

What is the actual behavior?
What did you see instead?

Application crashes:

  File "/xxx/.venv/lib/python3.9/site-packages/starlette/applications.py", line 139, in add_middleware
    raise RuntimeError("Cannot add middleware after an application has started")

Additional context
Add any other context about the problem here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/init.py at build_middleware_stack and review FastAPIInstrumentor.uninstrument_app alongside the linked Starlette change. Reproduce with the provided FastAPI and CORSMiddleware example using FastAPI >=0.91 and Starlette >=0.24. Done means uninstrument_app no longer prevents a later app.add_middleware call.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.