56 releases (28 breaking)

0.34.0 Jun 5, 2024
0.32.3 Apr 16, 2024
0.32.2 Jan 30, 2024
0.32.1 Dec 18, 2023
0.2.0 Jul 21, 2018

#124 in HTTP server

Download history 4457/week @ 2024-07-25 3856/week @ 2024-08-01 3383/week @ 2024-08-08 3456/week @ 2024-08-15 3610/week @ 2024-08-22 3349/week @ 2024-08-29 3365/week @ 2024-09-05 3419/week @ 2024-09-12 3323/week @ 2024-09-19 3860/week @ 2024-09-26 4843/week @ 2024-10-03 3739/week @ 2024-10-10 4633/week @ 2024-10-17 4958/week @ 2024-10-24 4765/week @ 2024-10-31 4195/week @ 2024-11-07

19,251 downloads per month
Used in hook0-api

Apache-2.0

245KB
4.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. As a result many of the sentry integrations such as breadcrumbs do not work unless you bind the actix hub.

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 will capture a message in the current request's Hub:

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

Resources

License: Apache-2.0

Dependencies

~15–27MB
~441K SLoC