Send OpenTelemetry from FastAPI to your Mac
Python needs no code change. The OpenTelemetry distro wraps the process and instruments FastAPI, the HTTP client, and the database driver it finds.
Before you start
Section titled “Before you start”- Loggerhead is open.
- You know the effective port. Open Collectors → Loggerhead receiver → Connection and copy the HTTP port. The first workspace uses 4318. See Ports and the effective port.
Step 1: install
Section titled “Step 1: install”pip install opentelemetry-distro opentelemetry-exporter-otlpopentelemetry-bootstrap -a installopentelemetry-bootstrap reads the packages you already have and installs the matching instrumentation.
Step 2: set the endpoint
Section titled “Step 2: set the endpoint”OTEL_SERVICE_NAME=my-apiOTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318OTEL_EXPORTER_OTLP_PROTOCOL=http/protobufOTEL_LOGS_EXPORTER=otlpOTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=trueOTEL_SEMCONV_STABILITY_OPT_IN=http,databaseThe two LOGS lines send your logging records as well. Without them Python sends traces and metrics only. The last line asks the instrumentation for the current attribute names, such as http.request.method and db.query.text. Without it the spans carry the names that OpenTelemetry renamed, and the lint rule “Renamed attribute keys” reports them.
Step 3: start the app
Section titled “Step 3: start the app”opentelemetry-instrument uvicorn main:app --reloadCall one endpoint, then open Traces in Loggerhead.
What arrives
Section titled “What arrives”| Signal | State |
|---|---|
| Traces | Every request, and the calls the instrumentation finds inside it. |
| Metrics | Request duration and count, plus the runtime metrics. |
| Logs | Every logging record, with its trace ID, when the two log settings are on. |
Tag the environment
Section titled “Tag the environment”OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=stagingSee Workspaces and environments.
Then read it with your agent
Section titled “Then read it with your agent”- Open Logs in Loggerhead. Click a log line, then click its trace ID in the detail to open the trace.
- Connect your coding agent once, and it reads the same data. See Claude Code.