actix-web

The sentry-actix crate adds a middleware for actix-web that captures errors and report them to Sentry.

To use this middleware, configure Sentry then add it to your actix web app as a middleware. Because actix is generally working with non sendable objects and is highly concurrent, this middleware creates a new hub per request. As a result, many of the Sentry integrations, such as breadcrumbs, do not work unless you bind the actix hub.

Example

In your Cargo.toml:

Cargo.toml
Copied
[dependencies]
sentry = "0.34.0"
sentry-actix = "0.34.0"

And your Rust code:

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())
}

#[actix_web::main]
async fn main() -> io::Result<()> {
    let _guard = sentry::init(("https://examplePublicKey@o0.ingest.sentry.io/0", sentry::ClientOptions {
        release: sentry::release_name!(),
        ..Default::default()
    }));
    std::env::set_var("RUST_BACKTRACE", "1");

    HttpServer::new(|| {
        App::new()
            .wrap(sentry_actix::Sentry::new())
            .service(failing)
    })
    .bind("127.0.0.1:3001")?
    .run()
    .await?;

    Ok(())
}

Reusing the Hub

When using the actix integration, a new per-request Hub will be created from the main Hub, and will be set automatically as the current Hub (Hub::current()). No manual intervention in needed.