Repository navigation
First Check
Commit to Help
Example Codeimport os
from fastapi import FastAPI
from fastapi.testclient import TestClient
from opentelemetry import metrics, trace
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import ConsoleMetricExporter, PeriodicExportingMetricReader
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
# Another library has already configured providers with its own exporters.
devnull = open(os.devnull, "w")
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter(out=devnull)))
trace.set_tracer_provider(tracer_provider)
meter_provider = MeterProvider(metric_readers=[PeriodicExportingMetricReader(ConsoleMetricExporter(out=devnull))])
metrics.set_meter_provider(meter_provider)
with TestClient(FastAPI()):
pass
print(
"span processors:", len(tracer_provider._active_span_processor._span_processors),
"metric readers:", len(meter_provider._all_metric_readers),
)Run with DescriptionWhat I'm trying to achieve. We run several FastAPI servers per job (NeMo Gym). In each process, a telemetry library (nemo-lens) creates the OpenTelemetry tracer, meter and logger providers and adds its own OTLP exporters to them. That library finds the collector through Since 0.142.0, FastAPI sees the same variable at startup and adds a second exporter to those providers. The docs say this is intended ("Existing exporters continue sending data to their destinations"). For us it means every span is exported twice, and each metric is exported by two readers with separate state. The cumulative histogram We'd like a way to say, once per process, that export is already handled and FastAPI should not add its own exporters, without touching every Why
The part of the stack that knows export is already handled is the telemetry library, which owns the providers. It doesn't create the apps, though. What already seems to work. Setting Questions.
Operating SystemLinux Operating System DetailsNo response FastAPI Version0.142.4 Pydantic Version2.13.5 Python Version3.13.14 Additional ContextOpenTelemetry SDK 1.45.1. |
Replies: 3 comments 1 reply
|
I checked FastAPI's implementation and tests. In So this is supported by FastAPI's current implementation and tests, and it works as a process-wide, per-signal opt-out. It leaves providers and exporters already configured by another library alone. The docs currently describe the per-app |
|
Thank you for the report and the clear explanation @ananthsub ! 🙌 I fixed/refactored it here: #16476 Now exporters are not automatically configured. So, it is opt-in, not opt-out. It can be enabled with the config |
Thank you for the report and the clear explanation @ananthsub ! 🙌
I fixed/refactored it here: #16476
Now exporters are not automatically configured. So, it is opt-in, not opt-out. It can be enabled with the config
auto_configureor with the env varFASTAPI_OTEL_AUTO_CONFIGURE=true. This is available in FastAPI 0.143.0, just released. 🚀