Sentry

Sentry崩溃报告

Sentry是一个崩溃报告系统,它支持基于actix错误报告的failure crate。使用Sentry中间件,可以自动将服务器错误报告给Sentry。

Sentry中间件

该中间件捕获服务器错误范围(500-599)中的任何错误,并通过其堆栈跟踪将附加的错误发送给哨兵。

要使用中间件,需要初始化和配置Sentry,并且需要添加sentry-actix中间件。此外,注册panic处理程序以通知困难panic也是有意义的。

  1. extern crate sentry;
  2. extern crate sentry_actix;
  3. use sentry_actix::SentryMiddleware;
  4. use std::env;
  5. fn main() {
  6. sentry::init("SENTRY_DSN_GOES_HERE");
  7. env::set_var("RUST_BACKTRACE", "1");
  8. sentry::integrations::panic::register_panic_handler();
  9. let mut app = App::with_state(state)
  10. .middleware(SentryMiddleware::new())
  11. // ...
  12. }

Reusing the Hub

如果使用这种集成,默认的sentry hub(Hub::current())通常是错误的。要获得特定的请求,您需要使用ActixWebHubExt trait:

  1. use sentry::{Hub, Level};
  2. use sentry_actix::ActixWebHubExt;
  3. let hub = Hub::from_request(req);
  4. hub.capture_message("Something is not well", Level::Warning);

The hub can also be made current for the duration of a call. Then Hub::current() works correctly until the end of the run block.

  1. use sentry::{Hub, Level};
  2. use sentry_actix::ActixWebHubExt;
  3. let hub = Hub::from_request(req);
  4. Hub::run(hub, || {
  5. sentry::capture_message("Something is not well", Level::Warning);
  6. });