All jobs

Add Request Tracing to an Axum API from a Linear Issue

An agent gives every Axum request an x-request-id on its tracing span, writes JSON logs filtered by RUST_LOG, and cargo test passes on Postgres.

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

What the agent does

The agent reads Cargo.toml, src/main.rs, src/lib.rs and src/http/mod.rs first. serve already added a bare TraceLayer::new_for_http(), main started env_logger, and nothing carried a request ID. The pinned tower-http is 0.2.0, and its request_id module turned out to have no MakeRequestUuid: the first build failed with unresolved import.

So src/http/tracing_layers.rs adds with_request_tracing, which keeps a client's x-request-id or makes a UUIDv4 with a small generator, opens a request span with request_id, method and uri, and echoes the ID on the response. src/telemetry.rs replaces env_logger with a JSON tracing-subscriber filtered by RUST_LOG, defaulting to info, and bridges log:: records and SQLx's query logs into tracing.

Start it with one API call

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

{
  "agent": "claude",
  "input": "A Linear issue labeled agent says: \"Add Request Tracing to an Axum API. Add tower-http's TraceLayer with a request ID on every span and JSON logs through tracing-subscriber, configurable with RUST_LOG.\" Do the work in this repository, add tests for it, run cargo test with Postgres started in the sandbox, and open a pull request that links back to the issue.",
  "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 launchbadge/realworld-axum-sqlx at f1b2565: 8 files changed, 472 insertions(+), 95 deletions(-), of which Cargo.lock is 286 lines and the new tracing_layers.rs 200. On the untouched clone cargo test exited 101 with due to 35 previous errors, because SQLx's query! macros need a migrated database to compile; with Postgres 15 and migrations/*.sql applied it exited 0. After the change cargo test ran 6 tests (test_slugify and five new tracing tests) and exited 0. In a live run, the SQLx query lines and finished processing request for GET /api/tags shared one request_id, and x-request-id: demo-1 came back unchanged.

For the reviewer: logs are now JSON for everyone, local development included, so decide whether that should be opt-out. The hand-written UUID generator goes away only by upgrading tower-http, which means upgrading axum. No Linear issue id was given to link.

Run it on every Linear issue labeled agent

An automation with a linear trigger on linear.issue.labeled and the label agent runs the linear-label-to-pr recipe's prompt when someone in your Rush org labels an issue. If the issue names the header your load balancer already sets, such as x-amzn-trace-id, the agent can key the span on that instead.

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

Why run it on Rush?

This repository cannot even compile its tests without a migrated Postgres, and its pinned tower-http lacks the helper most guides use. The run set up the database, recorded the baseline failure and the green baseline separately, and wrote around the missing MakeRequestUuid instead of upgrading the web framework to get it.

Frequently asked questions

Do the new tests need the database?

No. They build a small Router, send requests with oneshot and read the JSON lines from a captured writer. The crate still needs DATABASE_URL and a migrated database at compile time because of SQLx's macros.

Why did Cargo.lock change so much?

Adding tracing-subscriber with JSON and EnvFilter pulled in new crates, and uuid's v4 feature was enabled for the generator. The existing log:: calls were bridged, not rewritten.

More Axum jobs

More jobs like this: Turn a Linear label into a PR

Keep reading