flyer 3.0.0

HTTP framework for rust
Documentation
use std::time::Duration;

use sentry::{
    protocol::Event,
    ClientInitGuard,
    Level,
};

use crate::{
    loggers::{Logger, PanicErrorInfo},
    request::Request,
    response::Response,
};

pub struct Sentry {
    guard: ClientInitGuard,
}

impl Sentry {
    pub fn new(dsn: impl Into<String>, environment: impl Into<String>) -> Self {
        let guard = sentry::init((
            dsn.into(),
            sentry::ClientOptions {
                release: sentry::release_name!(),
                environment: Some(environment.into().into()),
                send_default_pii: true,
                ..Default::default()
            },
        ));

        Self { guard }
    }
}

impl Logger for Sentry {
    async fn call(&self, info: PanicErrorInfo, req: Request, res: Response) {
        sentry::configure_scope(|scope| {
            scope.clear();

            scope.set_extra("request", req.into());
            scope.set_extra("response", res.into());
        });

        sentry::capture_event(Event {
            level: Level::Error,
            message: Some(format!("{}", info.error)),
            ..Default::default()
        });

        self.guard.flush(Some(Duration::from_secs(2)));
    }
}