All jobs

Add OpenTelemetry Tracing to a FastAPI Service

An agent adds OpenTelemetry to a FastAPI service with server, asyncpg and requests spans, then exports 23 spans from a live run to a local collector.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

In testdrivenio/fastapi-tdd-docker, where the service lives under project/, the agent read app/main.py, app/db.py, app/summarizer.py and app/api/summaries.py to find the service's real paths: Tortoise ORM over asyncpg for the database, and a generate_summary background task whose outbound fetch goes through requests.

It wrote app/telemetry.py, called from create_application() in app/main.py: a TracerProvider with FastAPIInstrumentor, AsyncPGInstrumentor and RequestsInstrumentor, exporting over OTLP/HTTP. Everything else comes from the SDK's own OTEL_* variables. Tracing stays off unless OTEL_EXPORTER_OTLP_ENDPOINT is set or OTEL_TRACES_EXPORTER=console, and per-chunk ASGI send and receive spans are dropped. Five packages are pinned in requirements.txt, and tests/test_telemetry.py covers the on and off switch.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add OpenTelemetry tracing to this service: HTTP server spans, database spans and outbound HTTP spans, configured through the standard OTEL_* environment variables. Run a local collector in the sandbox, exercise the main endpoints, and open a pull request with an export of the traces.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

Tried on testdrivenio/fastapi-tdd-docker at 16433b6: 5 files changed, 133 insertions(+), README included. pytest went from 27 tests to ============================== 32 passed in 0.14s ==============================, and flake8, black and isort exited 0 before and after. Against otelcol 0.119.0 in the sandbox, the endpoints exported 23 spans: 7 server, one BackgroundTask generate_summary, 14 asyncpg and one requests GET, with the database spans under each request's server span. The export, traces.jsonl, came back to the caller as a session artifact; no pull request was opened.

For the reviewer: tracing is opt-in by endpoint, and only http/protobuf is supported. In the final run the DELETE's asyncpg span was missing from the collector file, though its server span was there and the first run had it; the run did not find out why. With no Docker, Postgres came from the pgserver wheel, and neither the Dockerfile nor gunicorn was tested.

Run it on every Linear issue labelled observability

An automation with a linear trigger on linear.issue.labeled and the label observability runs the recipe's prompt: do the work the issue describes and open a pull request that links back to it. Put this page's request in the issue, and name the protocol your collector speaks if it is gRPC, since this trial only wired http/protobuf.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

The proof is the trace tree, not the imports. The run read the collector's file and showed each INSERT and SELECT under its request's server span, and the background summary's requests GET and UPDATE under its BackgroundTask span. It also reported the span that went missing.

Frequently asked questions

Does tracing change the test suite?

No. With no endpoint set, init_telemetry() returns False and nothing is instrumented. The five new tests check that switch: off by default, batch OTLP export when an endpoint is set, none winning over an endpoint, the console exporter, and OTEL_SDK_DISABLED.

Why is httpx not instrumented?

The run left it out because, it reports, the app uses httpx only in its tests; the service's outbound calls go through requests.

More FastAPI jobs

The same job on other stacks

Keep reading