Actix Web

Learn about monitoring your Actix Web application with Sentry.

The Sentry SDK offers a middleware for the Actix Web framework that supports:

  • Capturing server errors returned by Actix Web services.
  • Starting a transaction for each request-response cycle.

To add Sentry with the Actix Web integration to your Rust project, just add a new dependency to your Cargo.toml:

Cargo.toml
Copied
[dependencies]
actix-web = "4.11.0"
sentry = { version = "0.38.1", features = ["actix"] }

Initialize and configure the Sentry client. This will enable a set of default integrations, such as panic reporting. Then, initialize Actix Web with the Sentry middleware.

main.rs
Copied
use std::io;

use actix_web::{get, App, Error, HttpRequest, HttpServer};

#[get("/")]
async fn failing(_req: HttpRequest) -> Result<String, Error> {
    Err(io::Error::new(io::ErrorKind::Other, "An error happens here").into())
}

fn main() -> io::Result<()> {
    let _guard = sentry::init((
        "https://examplePublicKey@o0.ingest.sentry.io/0",
        sentry::ClientOptions {
            release: sentry::release_name!(),
            // Capture all traces and spans. Set to a lower value in production.
            traces_sample_rate: 1.0,
            // Capture user IPs and potentially sensitive headers when using HTTP server integrations
            // see https://docs.sentry.io/platforms/rust/data-management/data-collected for more info
            send_default_pii: true,
            // Capture all HTTP request bodies, regardless of size
            max_request_body_size: sentry::MaxRequestBodySize::Always,
            ..Default::default()
        },
    ));

    actix_web::rt::System::new().block_on(async {
        HttpServer::new(|| {
            App::new()
                .wrap(
                    sentry::integrations::actix::Sentry::builder()
                        .capture_server_errors(true) // Capture server errors
                        .start_transaction(true) // Start a transaction (Sentry root span) for each request
                        .finish(),
                )
                .service(failing)
        })
        .bind("127.0.0.1:3001")?
        .run()
        .await
    })?;

    Ok(())
}

The snippet above sets up a service that always fails, so you can test that everything is working as soon as you set it up.

Send a request to the application. The response error will be captured by Sentry.

Copied
curl http://localhost:3001/

To view and resolve the recorded error, log into sentry.io and select your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").