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

Warning triggered by asgi.OpenTelemetryMiddleware

Open
#1,335 7 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

Describe your environment
Python: 3.10.7
opentelemetry-instrumentation-asgi: 0.33b0
opentelemetry-api 1.12.0
Fastapi: 0.79.0

Steps to reproduce
Setup the asgi middleware with fastapi as following:

app = fastapi.FastAPI()
app.add_middleware(OpenTelemetryMiddleware)
# Configure OTEL exporter

What is the expected behavior?
We should not see any warnings.

What is the actual behavior?
The following warning is raised multiple times.

An instrument with name http.server.duration, type Histogram, unit ms and description measures the duration of the inbound HTTP request has been created already.

Additional context
Seems to be related to https://github.com/open-telemetry/opentelemetry-python/issues/2558. This is the code triggering the issue

https://github.com/open-telemetry/opentelemetry-python/blob/75313b6c6d58945c1401622b6683ccdd28657984/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py#L195-L202

I imagine that somehow FastAPI is creating multiple instances of the middleware, and hence the instruments are created multiple times.

Labels: asgi, warning, fastapi

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

Reproduce the FastAPI setup with OpenTelemetryMiddleware and read opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/init.py around lines 195-202, alongside issue 2558. Trace why the http.server.duration instrument is created repeatedly and identify the relevant instrumentation path. Done means the reproduction no longer emits duplicate-instrument warnings, with regression coverage if the repository provides a suitable test location.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
backend, observability
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.