59 releases (31 breaking)

0.37.0 Apr 1, 2025
0.36.0 Jan 7, 2025
0.35.0 Nov 29, 2024
0.34.0 Jun 5, 2024
0.2.0 Jul 21, 2018

#284 in HTTP server

Download history 2565/week @ 2024-12-23 2402/week @ 2024-12-30 5543/week @ 2025-01-06 4904/week @ 2025-01-13 5010/week @ 2025-01-20 4965/week @ 2025-01-27 5374/week @ 2025-02-03 4972/week @ 2025-02-10 5122/week @ 2025-02-17 5226/week @ 2025-02-24 5288/week @ 2025-03-03 5142/week @ 2025-03-10 5056/week @ 2025-03-17 5047/week @ 2025-03-24 5723/week @ 2025-03-31 5360/week @ 2025-04-07

21,488 downloads per month
Used in hook0-api

MIT license

200KB
3.5K SLoC

Sentry

Sentry Rust SDK: sentry-actix

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

To use this middleware just configure Sentry and then add it to your actix web app as a middleware. Because actix is generally working with non sendable objects and highly concurrent this middleware creates a new Hub per request.

Example

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(sentry::ClientOptions {
        release: sentry::release_name!(),
        ..Default::default()
    });
    std::env::set_var("RUST_BACKTRACE", "1");

    let runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()?;
    runtime.block_on(async move {
        HttpServer::new(|| {
            App::new()
                .wrap(sentry_actix::Sentry::new())
                .service(failing)
        })
        .bind("127.0.0.1:3001")?
        .run()
        .await
    })
}

Using Release Health

The actix middleware will automatically start a new session for each request when auto_session_tracking is enabled and the client is configured to use SessionMode::Request.

let _sentry = sentry::init(sentry::ClientOptions {
    release: sentry::release_name!(),
    session_mode: sentry::SessionMode::Request,
    auto_session_tracking: true,
    ..Default::default()
});

Reusing the Hub

This integration will automatically create a new per-request Hub from the main Hub, and update the current Hub instance. For example, the following in the handler or in any of the subsequent middleware will capture a message in the current request's Hub:

sentry::capture_message("Something is not well", sentry::Level::Warning);

It is recommended to register the Sentry middleware as the last, i.e. the first to be executed when processing a request, so that the rest of the processing will run with the correct Hub.

Resources

License: MIT

Dependencies

~16–27MB
~458K SLoC