compute_rust_sentry/
lib.rs

1use fastly::{http::Method, Error, Request};
2use log::debug;
3use url::Url;
4
5mod types;
6
7pub use types::*;
8
9pub struct Raven {
10    dsn: Url,
11    backend: String,
12}
13
14impl Raven {
15    pub fn from_dsn_and_backend(dsn: impl Into<Url>, backend: impl Into<String>) -> Self {
16        Raven {
17            dsn: dsn.into(),
18            backend: backend.into(),
19        }
20    }
21
22    pub fn report_error(
23        &self,
24        error: impl std::error::Error,
25        request: &Request,
26    ) -> Result<(), Error> {
27        let event_payload = EventPayload {
28            request: Some(request.into()),
29            ..error.into()
30        };
31
32        let auth_string = format!(
33            "Sentry sentry_version=7,sentry_key={},sentry_secret={},sentry_client={}/{}",
34            self.dsn.username(),
35            self.dsn.password().unwrap_or(""),
36            env!("CARGO_PKG_NAME"),
37            env!("CARGO_PKG_VERSION")
38        );
39
40        let req = Request::new(
41            Method::POST,
42            format!(
43                "{}://{}/api{}/store/",
44                self.dsn.scheme(),
45                self.dsn.host_str().unwrap(),
46                self.dsn.path()
47            ),
48        )
49        .with_header("X-Sentry-Auth", auth_string)
50        .with_body_json(&event_payload)?;
51
52        debug!("-> Submitting report to Sentry");
53
54        let resp = req.send(self.backend.clone())?;
55
56        debug!("-> {} from Sentry", resp.get_status());
57
58        Ok(())
59    }
60}