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.19.0"
sentry-actix = "0.19.0"
And your Rust code:
main.rs
Copied
extern crate actix_web;
extern crate sentry;
extern crate sentry_actix;
use std::env;
use std::io;
use actix_web::{server, App, Error, HttpRequest};
use sentry_actix::SentryMiddleware;
fn failing(_req: &HttpRequest) -> Result<String, Error> {
Err(io::Error::new(io::ErrorKind::Other, "An error happens here").into())
}
fn main() {
let _guard = sentry::init("https://examplePublicKey@o0.ingest.sentry.io/0");
env::set_var("RUST_BACKTRACE", "1");
server::new(|| {
App::new()
.middleware(SentryMiddleware::new())
.resource("/", |r| r.f(failing))
}).bind("127.0.0.1:3001")
.unwrap()
.run();
}
Reusing the Hub
If you use this integration the Hub::current()
returned hub is typically the wrong one.
To get the request specific one you need to use the ActixWebHubExt
trait:
Copied
use sentry::{Hub, Level};
use sentry_actix::ActixWebHubExt;
let hub = Hub::from_request(req);
hub.capture_message("Something is not well", Level::Warning);
The hub can also be made current:
Copied
use sentry::{Hub, Level};
use sentry_actix::ActixWebHubExt;
let hub = Hub::from_request(req);
Hub::run(hub, || {
sentry::capture_message("Something is not well", Level::Warning);
});
You can edit this page on GitHub.
- Package:
- cargo:sentry
- Version:
- 0.21.0
- Repository:
- https://github.com/getsentry/sentry-rust