# Add OpenTelemetry Tracing to an Actix Web App

Yes. An agent replaces env_logger with tracing and tracing-actix-web, exports spans over OTLP, instruments the SQLx calls and opens a pull request with test output.

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

## What the agent does

The agent clones the repository into its own cloud sandbox and reads how the app logs today. In the `basics/todo` member of actix/examples, `main.rs` calls `env_logger::init_from_env` with `info` as the default, wraps the `App` in `middleware::Logger::default()`, binds `127.0.0.1:8080` with two workers and serves `index`, `create` and `update` handlers that call `db::get_all_tasks`, `create_task`, `toggle_task` and `delete_task` over a `SqlitePool` built in `db.rs` with a one-second acquire timeout. `Cargo.toml` takes `actix-web`, `actix-session`, `env_logger`, `log` and `serde` from the workspace, which is on edition 2024, `rust-version` 1.98 and actix-web 4.11, plus `sqlx` 0.9 with the SQLite driver and `tera` 2. `.env` sets `DATABASE_URL`, `SQLX_OFFLINE=true` and `RUST_LOG=info`.

The workspace already carries the pieces. Its `[workspace.dependencies]` pin `tracing` 0.1.30, `tracing-actix-web` 0.7.22 and `tracing-subscriber` 0.3 with the `env-filter` and `json` features, and `tracing/custom-root-span` shows the whole pipeline: `opentelemetry` and `opentelemetry-otlp` 0.32 with `grpc-tonic`, `opentelemetry_sdk` 0.32, `tracing-opentelemetry` 0.33, `tracing-actix-web` with the `opentelemetry_0_32` feature, a `Resource` with `service.name`, a `SdkTracerProvider` with a batch exporter, `TraceContextPropagator` as the global propagator, and a `provider.shutdown()` after the server returns. The agent follows that example rather than picking versions of its own, so the new crates resolve against the one `Cargo.lock` at the root.

In `basics/todo` it adds a `telemetry.rs` that builds the provider, reads the collector address from `OTEL_EXPORTER_OTLP_ENDPOINT` with the example's `localhost:4317` as the default, and installs a subscriber with an `EnvFilter` that still honours `RUST_LOG`, the OpenTelemetry layer and a formatting layer, JSON in production and compact otherwise. `main.rs` swaps `Logger::default()` for `TracingLogger::default()`, so every request gets a root span with method, route and status, and the `log::info!` lines keep working through the subscriber's log bridge. The four functions in `db.rs` get `#[tracing::instrument(skip(pool))]`, so each query appears as a child span under its request, and the provider is shut down after `run().await` so the last spans are flushed.

To prove it, the agent follows the README: `sqlx database create`, `sqlx migrate run`, then `cargo check -p todo`, `cargo clippy` and `cargo test --workspace` as the CI workflow runs them. It starts an OpenTelemetry Collector container in the sandbox with its debug exporter, runs the app, posts a task through the form and reads the collector output to confirm one trace with the request span and the `insert` span beneath it. It opens a pull request with the code, the dependency lines and a README paragraph on the endpoint variable.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add OpenTelemetry tracing to this Actix Web app. Read Cargo.toml, the workspace Cargo.toml, .env, every file under src, the migrations and the README first, and look at any tracing example already in the repository so the versions match. Replace env_logger and the Logger middleware with tracing-subscriber and TracingLogger from tracing-actix-web, export spans over OTLP gRPC to the address in OTEL_EXPORTER_OTLP_ENDPOINT, set a service.name resource, instrument the database functions so queries show as child spans, keep RUST_LOG working through an EnvFilter, and shut the tracer provider down cleanly on exit. Run cargo check, clippy and the tests, start a collector with its debug exporter and show one trace from a real request, and open a pull request.",
  "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 on its own branch. The diff adds the tracing and OpenTelemetry crates to `basics/todo/Cargo.toml` and the updated `Cargo.lock`, a new `src/telemetry.rs`, the middleware and shutdown changes in `main.rs`, the `instrument` attributes in `db.rs` and a README section naming `OTEL_EXPORTER_OTLP_ENDPOINT` and `RUST_LOG`. The description pastes one trace as the collector printed it.

The run's record keeps the `cargo check`, `cargo clippy` and `cargo test` output, the collector's log with the request and query spans, and the commands that created and migrated the SQLite database, so a reviewer can see the span tree without running a collector.

Before merging, a reviewer checks that the runtime flavour the provider's batch exporter expects matches how `main` starts, as the in-repo example pairs `rt-tokio-current-thread` with a local runtime, that no span records the task description if that counts as user data, and that the endpoint default suits their deployment.

## Run it on every new Rust service

A team with several Actix services starts one run per repository with `POST /o/{org}/sessions`, this prompt and the repository in `resources`. Each run reads its own `main.rs` and middleware stack, so a service that already uses `tracing` gets only the exporter and the instrumented database layer, and each opens its own pull request.

A `request_id` in the body makes a retry safe. The API key's webhook posts `run.completed` or `run.failed` with the `session_url` when each turn ends, so a script can gather the pull request links and the sample traces for the platform team.

## Why run it on Rush?

Tracing is only proven by a trace, and producing one means a migrated database, a running server, a request and a collector, all at once. The sandbox has the Rust toolchain, SQLite, `sqlx-cli` and Docker, so the agent does the whole sequence and keeps the collector output, instead of a reviewer reading `telemetry.rs` and hoping the spans export.

Dependency choices for OpenTelemetry in Rust are version-sensitive, and the right answer lives in the repository's own lockfile and examples. The agent reads those before adding a line, and the same prompt runs across every service in parallel, each resolving against its own workspace.

## Frequently asked questions

### Will my RUST_LOG settings keep working?

Yes. The subscriber uses `EnvFilter::try_from_default_env()` with `info` as the fallback, the same variable `env_logger` read, and `log` macros reach the subscriber through its log bridge, so the existing `log::info!` lines appear in both the console output and the traces.

### Which exporter does it use?

OTLP over gRPC through `opentelemetry-otlp` with the `grpc-tonic` feature, as the repository's own tracing example does. The endpoint comes from `OTEL_EXPORTER_OTLP_ENDPOINT`, so the same binary points at Jaeger, Tempo, Honeycomb or a collector without a rebuild.

### Does it slow the request path?

Spans are batched and exported on a background task, and the request span is created by the middleware the same way the logger middleware was. The agent keeps `TracingLogger` as the only per-request addition and instruments the four database functions, not the template rendering.

### What about the SQLx query macros and SQLX_OFFLINE?

Nothing changes for them. `#[instrument]` wraps the functions in `db.rs`, not the `query!` macros in `model.rs`, so the offline metadata and the `.env` settings the README describes stay as they are.

## More Actix Web jobs

- [Add Prometheus Metrics to an Actix Web App](https://getrush.ai/run/add-prometheus-metrics/actix/)
- [Add Rate Limiting to an Actix Web API](https://getrush.ai/run/add-rate-limiting/actix/)
- [Add Request ID Logging to an Actix Web App](https://getrush.ai/run/add-request-id-logging/actix/)

## The same job on other stacks

- [Add OpenTelemetry Tracing to an aiohttp App](https://getrush.ai/run/add-opentelemetry/aiohttp/)
- [Add OpenTelemetry Tracing to an ASP.NET Core API](https://getrush.ai/run/add-opentelemetry/aspnet-core/)
- [Add OpenTelemetry Tracing to a chi API](https://getrush.ai/run/add-opentelemetry/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/)
