FastAPI traces with OpenTelemetry, shown as a waterfall
Install the OpenTelemetry Python distro, set OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:8000 and OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf, and start your FastAPI app with opentelemetry-instrument. Each request then shows up in Buggregator as a trace, with its spans on a waterfall. Your app code does not change.
Steps
1. Start Buggregator
OTLP comes in on the main HTTP port, 8000.
docker run --pull always \
-p 127.0.0.1:8000:8000 \
ghcr.io/buggregator/server:latest2. Install the OpenTelemetry packages
opentelemetry-bootstrap adds the instrumentation for the libraries it finds, FastAPI among them.
pip install opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a install3. Set the standard variables
OTEL_EXPORTER_OTLP_PROTOCOL must be http/protobuf or http/json: Buggregator does not accept gRPC. It does not take metrics either, so turn the metrics exporter off.
OTEL_SERVICE_NAME=checkout
OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:8000
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_TRACES_EXPORTER=otlp
OTEL_METRICS_EXPORTER=noneIn Docker Compose, replace 127.0.0.1 with the Buggregator service name, for example buggregator.
4. Start the app under opentelemetry-instrument
opentelemetry-instrument uvicorn main:app5. Open the trace
Call an endpoint, then open http://127.0.0.1:8000 and the Sentry page, Traces tab. The trace is labeled OTel and shows your service name. Open it to see each span on a timeline, with a Service column. A span with status ERROR is marked as an error.
Why it works
opentelemetry-instrument sets up the OTel SDK and the FastAPI instrumentation before your app starts. The OTLP exporter adds /v1/traces to the endpoint and posts each batch of spans there. Buggregator answers that path on port 8000 and shows the spans next to traces from Sentry SDKs. OTEL_SERVICE_NAME is what the UI shows as the service.
Docs: docs.buggregator.dev/config/opentelemetry.html#otel-sdk
Requires Buggregator v2.12.0+
Related tips
- OpenTelemetry
Add Buggregator to your OTel Collector as one more exporter
Add an otlphttp exporter to your Collector config. Traces and logs from every service show up in Buggregator, and the apps stay as they are.
- Sentry
Django errors, traces and logs on your machine
Point sentry-sdk at Buggregator with traces and logs on. Django errors, request traces and log records show up locally, with no Sentry account.
- Sentry
See a Laravel request's Sentry trace as a waterfall
Turn on Sentry tracing in Laravel and send it to Buggregator. Every span of a request shows on a timeline, and errors link to their trace.
- HTTP
See every API call your Python script makes
Route Python requests through Buggregator's HTTP proxy on port 8080. See each request and response with status code and timing. Dev only.
Checked on with Buggregator v2.13.0