What the agent does
The agent reads the app and what the workspace already knows about metrics. In a service shaped like basics/todo in actix/examples, the workspace Cargo.toml sets the 2024 edition, rust-version 1.98 and actix-web 4.11, actix-files 0.7, actix-session 0.11, env_logger 0.11 and tracing-actix-web 0.7.22 as workspace dependencies, and the crate adds sqlx 0.9 with SQLite and tera 2. src/main.rs builds the HttpServer with a Tera instance, a SqlitePool from DATABASE_URL, Logger::default(), a cookie SessionMiddleware, ErrorHandlers for 500, 400 and 404, and four services: GET / rendering the task list, POST /todo, POST /todo/{id} which api::update turns into a toggle or a delete from a _method form field, and Files::new for /static, bound to 127.0.0.1:8080 with two workers. .env sets SQLX_OFFLINE=true and the README explains sqlx database create and sqlx migrate run. Nothing counts requests or times them. The sibling tracing/mainmatter-workshop crate in the same workspace already does: it depends on metrics 0.24 and metrics-exporter-prometheus 0.18 with default features off, src/prometheus.rs builds a PrometheusBuilder with exponential buckets for http_request_duration and calls install_recorder() for a PrometheusHandle, src/middleware.rs is a from_fn middleware that moves a concurrent-requests gauge and records the histogram with an outcome label, and src/routes.rs serves GET /metrics by calling render() on the handle from ThinData. That is the pattern the agent follows.
It adds metrics and metrics-exporter-prometheus to basics/todo/Cargo.toml at the sibling's versions. src/metrics.rs holds the metric names as constants, an init() that describes http_request_duration in seconds with the same buckets and installs the recorder, and a record_request middleware for middleware::from_fn that times each request and records three series: the duration histogram, an http_requests_total counter and an http_requests_in_flight gauge, each labelled with method, the matched route pattern from req.match_pattern() so /todo/{id} is one series rather than one per id, and status. In main.rs the handle goes into app_data as ThinData, GET /metrics is added before the static files service and answers handle.render() as text, and the middleware is the last wrap so it is outermost and times the error handlers and the session layer too. Requests to /metrics are counted like any other, with the route label making them easy to drop in a scrape config.
basics/todo has no tests of its own; the workspace CI runs cargo check --workspace, cargo test --workspace --all-features after creating todo.db with sqlx, and just check for prettier, taplo and clippy. The agent adds a tests module in metrics.rs that builds a recorder with PrometheusBuilder::new().build_recorder() instead of installing one globally, runs actix_web::test::init_service over a stub route wrapped in record_request under metrics::with_local_recorder, calls the stub twice and a missing path once, and asserts the rendered text contains the counter series for the stub with a count of two and a 404 series for the miss. It runs DATABASE_URL=sqlite://./todo.db sqlx database create and sqlx migrate run --source=./basics/todo/migrations as the workflow does, then cargo test -p todo, cargo clippy -p todo --all-targets, cargo +nightly fmt --check and taplo format --check on the edited Cargo.toml. Then it starts the server with cargo run -p todo, loads the index, posts a task, toggles it, requests /metrics with curl and pastes the exposition lines into the pull request.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "Add Prometheus metrics to the basics/todo service in this Actix Web workspace and open a pull request. Read the workspace Cargo.toml, basics/todo/Cargo.toml, basics/todo/src/main.rs, basics/todo/src/api.rs, basics/todo/.env, basics/todo/README.md, the tracing/mainmatter-workshop crate's Cargo.toml, src/prometheus.rs, src/middleware.rs and src/routes.rs, the justfile and .github/workflows/ci.yml first. Add metrics and metrics-exporter-prometheus at the versions the workshop crate uses. Write basics/todo/src/metrics.rs with the metric names as constants, an init() that describes http_request_duration in seconds with the workshop's buckets and installs the recorder, and a from_fn middleware record_request that records a duration histogram, an http_requests_total counter and an http_requests_in_flight gauge labelled with method, the matched route pattern and status. In main.rs put the PrometheusHandle in app_data as ThinData, add GET /metrics returning handle.render() before the static files service, and wrap record_request as the outermost middleware. Add a tests module using build_recorder and with_local_recorder over a stub route under actix_web::test::init_service, asserting the counter series after two calls and a 404 series after a miss. Create the SQLite database with sqlx as CI does, run cargo test -p todo, cargo clippy -p todo --all-targets, cargo +nightly fmt --check and taplo format --check, then run the server, make a few requests and paste the /metrics output in the pull request. Do not change the handlers, templates or session settings.",
"resources": [
{
"url": "https://github.com/your-org/your-app"
}
]
}- 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.
- 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.
- 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 that edits basics/todo/Cargo.toml and src/main.rs, adds src/metrics.rs, updates the workspace Cargo.lock, and touches nothing in api.rs, db.rs or the templates. The description pastes the cargo test output with the new test named, the clean clippy, fmt and taplo results, and an excerpt of /metrics showing the three series with their labels.
On a repository with the CI workflow, the pull request runs cargo check and cargo test across the whole workspace after creating the todo database, so the two new dependencies compile under the same stable toolchain as every other crate, and the nightly workflow repeats it on nightly.
A reviewer checks that the labels come from the route pattern and never from the raw path, that the histogram buckets suit their latencies, that /metrics is served on the same bind address as the app and whether that is acceptable, and that the middleware is outermost. Then they add the scrape target and decide whether to drop the /metrics series itself.
Run it on every Actix service without metrics
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 main.rs, so a service that already uses tracing-actix-web gets the middleware beside TracingLogger and a service with several App scopes gets one /metrics at the top, and each run opens its own pull request.
Give the API key a webhook_url and Rush POSTs run.completed when each turn ends, with the session_url to read the pull request and the test output from.
The use case this job follows, with its automation and trigger: Hand off a long task.
Why run it on Rush?
Metrics code is easy to write and hard to trust: a label on the raw path explodes cardinality, a middleware wrapped in the wrong order misses the errors it should count, and the exposition format has to be exactly what a scraper reads. In a Rush sandbox the agent compiles the crate under the workspace's toolchain, runs the tests against a local recorder, starts the binary, makes requests and reads /metrics back with curl, keeping all of it in the run's record.
The change copies the shape the workspace already uses in its telemetry example, two crates and a from_fn middleware, so a reviewer who knows that crate knows this one. The same request works on any Actix Web 4 service.
Frequently asked questions
Why label by the route pattern and not the request path?
/todo/{id} is one route, but each id would be a new path and a new series, and a scrape would grow with the table. req.match_pattern() gives the pattern Actix matched, so the series stay fixed in number and an unmatched path is counted under the 404 handler.
Why is the metrics middleware the last wrap?
Actix applies wrap calls inside out: the last one registered is the outermost and sees the request first and the response last. Putting record_request there means the timing includes the session middleware and the error handlers, and the recorded status is the one the client receives.
Does /metrics need its own port?
Not in this change; it is a route on the same server, as the workshop crate's README shows. If the app is public, a reviewer may prefer a second HttpServer on an internal address, and the handle can be shared with it.
How do the tests avoid installing a global recorder?
They build a recorder with build_recorder() and run the service under metrics::with_local_recorder, so each test owns its recorder and init(), which installs the global one for the real server, is never called in a test.