Start the telemetry in create_app, which gunicorn runs in every worker after fork because preload_app is off, and in the relay and worker processes with their own service names, and stop it after the rest of the shutdown within TELEMETRY_SHUTDOWN_SECONDS. The API gets server spans and the http.server.request.duration histogram in seconds from the FastAPI instrumentation with stable HTTP semantic conventions, without /health probes and without the internal send and receive spans, and the request pipeline puts X-Request-Id on the server span as messenger.request_id. The worker broker gets the FastStream RabbitTelemetryMiddleware, so every delivery runs inside a consumer span. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
24 lines
656 B
Python
24 lines
656 B
Python
import os
|
|
|
|
from fastapi import FastAPI
|
|
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
|
|
from opentelemetry.metrics import MeterProvider
|
|
from opentelemetry.trace import TracerProvider
|
|
|
|
PROBES = "/health/"
|
|
|
|
|
|
def instrument_http(
|
|
app: FastAPI,
|
|
tracer_provider: TracerProvider | None = None,
|
|
meter_provider: MeterProvider | None = None,
|
|
) -> None:
|
|
os.environ.setdefault("OTEL_SEMCONV_STABILITY_OPT_IN", "http")
|
|
FastAPIInstrumentor.instrument_app(
|
|
app,
|
|
tracer_provider=tracer_provider,
|
|
meter_provider=meter_provider,
|
|
excluded_urls=PROBES,
|
|
exclude_spans=["receive", "send"],
|
|
)
|