# Add Request ID Logging to an Actix Web App

Yes. An agent adds tracing-actix-web to your Actix Web app, logs every request under its id, echoes it as x-request-id, tests both, and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-request-id-logging/actix/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads how the app logs today before it changes it. In an app shaped like the `basics/todo` crate of actix/examples, `src/main.rs` calls `env_logger::init_from_env` with `info` as the default filter, wraps `Logger::default()` from `actix_web::middleware`, and writes `log::info!` and `log::debug!` lines by hand; `.env` sets `RUST_LOG=info` beside `DATABASE_URL` and `SQLX_OFFLINE=true`. `src/api.rs` turns database failures into `error::ErrorInternalServerError`, and the `ErrorHandlers` in `main.rs` serve `static/errors/500.html` for them, so a failed request leaves a status line in the log and nothing that ties it to the response the user saw.

The workspace `Cargo.toml` already pins what the change needs: `tracing 0.1.30`, `tracing-actix-web 0.7.22`, `tracing-subscriber 0.3` with the `env-filter` and `json` features, and `uuid 1.6`, all under `[workspace.dependencies]`, so the crate adds them with `workspace = true` and pins no version of its own. The repository also shows the exact pattern: `tracing/request-id-response-header/src/main.rs` wraps `TracingLogger::default()` and, outside it, a `from_fn` middleware that reads `RequestId` from `req.extensions()` before calling `next`, then inserts `x-request-id` on the response, and its README shows the header in `curl -v` output.

The agent replaces `env_logger` and `Logger` with a `tracing_subscriber` setup that reads `RUST_LOG` through `EnvFilter` and switches to the JSON formatter when `LOG_FORMAT=json`, wraps `TracingLogger::default()` so every request opens a root span with `request_id`, the method, route, status and elapsed time, and adds the `x-request-id` middleware from the tracing example outside it. The `log::info!` lines in `main.rs` keep working through tracing's `log` bridge, and `api.rs` gains a `tracing::error!` with the database error before it maps to the 500, so the line and the response share an id. The middleware order is written out in a comment, since `wrap` calls run in reverse.

The crate has no tests, so the agent moves the `App` construction into a function and adds a `#[cfg(test)]` module with `actix_web::test::init_service`: `GET /` answers with an `x-request-id` that parses as a UUID, two requests get different ids, and a request to a missing path still carries one on its 404 from the error handler. `sqlx::query_as!` in `src/model.rs` checks its SQL at compile time, so the agent creates and migrates `todo.db` the way `.github/workflows/ci.yml` does, with `sqlx database create` and `sqlx migrate run`, before `cargo test -p todo` and `cargo clippy`, and keeps both outputs along with one JSON log line from a request it makes in the sandbox.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "codex",
  "input": "Add request ID logging to this Actix Web app and open a pull request. Read Cargo.toml, src/main.rs, src/api.rs, .env and the workspace Cargo.toml first, and the tracing/request-id-response-header example in this repository for the pattern. Add tracing, tracing-actix-web, tracing-subscriber and uuid from the workspace dependencies. Replace env_logger and the Logger middleware with a tracing-subscriber setup that reads RUST_LOG through EnvFilter and uses the JSON formatter when LOG_FORMAT=json, wrap TracingLogger::default so each request has a root span with its request_id, and add a from_fn middleware outside it that sets x-request-id on every response. Log the database error in api.rs with tracing::error! before it becomes a 500. Move App construction into a function and add tests with actix_web::test that check a UUID x-request-id on GET /, different ids on two requests, and the header on a 404. Create and migrate the SQLite database the way the CI workflow does so the sqlx macros compile, run cargo test and cargo clippy for this crate, make one request against the running app with LOG_FORMAT=json and paste that log line and the test output in the pull request. Do not change the routes, the templates or the session setup.",
  "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

What comes back is a pull request with four `workspace = true` lines in the crate's `Cargo.toml`, the subscriber setup and two middleware lines in `src/main.rs`, one `tracing::error!` in `src/api.rs`, a test module, and an `.env` note for `LOG_FORMAT`. The description pastes one JSON log line with its `request_id`, method, route and status, the matching `x-request-id` from a `curl -v`, and the `cargo test` and `cargo clippy` output.

The repository's `ci.yml` builds and tests every workspace member on the pull request after creating `todo.db` with `sqlx`, so the new tests run there as well as in the sandbox.

A reviewer runs the app, makes a request that fails on purpose, and checks that the 500 page's `x-request-id` is the same value as the error line's `request_id` field. They check that `RUST_LOG` still filters as before and that the text formatter is still the default. Then they merge.

## Run it on every service whose logs cannot be tied to a response

A team with several Actix Web services sends one `POST /o/{org}/sessions` per repository with this prompt and that repository in `resources`. Each run reads its own logging setup, so a service already on `tracing` gets only the `TracingLogger` and the header middleware, and each opens its own pull request.

Give the key a `webhook_url` and Rush POSTs `run.completed` when each turn ends, with `session_url` to read the pull request from. A batch across many Actix services goes out at once: past the key's `max_concurrent`, four by default and up to 100, a request answers `202` with `status: "queued"` and starts, oldest first, when a slot frees.

## Why run it on Rush?

The point of a request id is that one value appears in two places, the log line and the response, and the only way to know it does is to make a request and look. In a Rush sandbox the agent starts the app, makes the request, reads the JSON line and the header, and keeps both in the run's record beside the test output, so the pull request shows the id matching rather than describing it.

The change is short, but choosing the subscriber, keeping the existing `log` calls working, ordering the middleware and migrating the database so `sqlx` compiles is a morning on a laptop. The agent does it in one pass, following the example already in the repository, and the same request works unchanged on the next service.

## Frequently asked questions

### Do the existing log::info! lines still work?

Yes. `tracing-subscriber`'s `fmt` layer picks up `log` records through the bridge the `tracing` crate enables, so the two `log::info!` lines in `main.rs` keep printing, now with the same filter from `RUST_LOG`.

### Can it honour an incoming x-request-id from a proxy?

Not in this pull request. `tracing-actix-web` generates the id, and reusing an upstream one needs a custom root span builder like the one in `tracing/custom-root-span`. The pull request names that example as the follow-up.

### Why is the header middleware outside TracingLogger?

Because `RequestId` is put into the request extensions by `TracingLogger`, and `wrap` calls run in reverse order of registration. The `from_fn` middleware is registered after it, so it runs first, reads the id once `next` has run through the logger, and sets the header on the way out, as the repository's example does.

### Why does the build need a database?

`sqlx::query!` and `query_as!` in `src/model.rs` check their SQL against `DATABASE_URL` at compile time unless `.sqlx` metadata exists. The agent creates and migrates `todo.db` the way `ci.yml` does, and the pull request repeats the two commands.

## More Actix Web jobs

- [Add Request Timeouts to an Actix Web App](https://getrush.ai/run/add-request-timeouts/actix/)
- [Add Security Headers to an Actix Web App](https://getrush.ai/run/add-security-headers/actix/)
- [Add Soft Delete to an Actix Web API](https://getrush.ai/run/add-soft-delete/actix/)

## The same job on other stacks

- [Add Request ID Logging to an AdonisJS App](https://getrush.ai/run/add-request-id-logging/adonisjs/)
- [Add Request ID Logging to an aiohttp App](https://getrush.ai/run/add-request-id-logging/aiohttp/)
- [Add Request ID Logging to a chi App](https://getrush.ai/run/add-request-id-logging/chi/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
