Line data Source code
1 : use std::borrow::Cow;
2 : use std::env;
3 :
4 : use sentry::ClientInitGuard;
5 : pub use sentry::release_name;
6 : use tracing::{error, info};
7 :
8 : #[must_use]
9 0 : pub fn init_sentry(
10 0 : release_name: Option<Cow<'static, str>>,
11 0 : extra_options: &[(&str, &str)],
12 0 : ) -> Option<ClientInitGuard> {
13 0 : let Ok(dsn) = env::var("SENTRY_DSN") else {
14 0 : info!("not initializing Sentry, no SENTRY_DSN given");
15 0 : return None;
16 : };
17 0 : let environment = env::var("SENTRY_ENVIRONMENT").unwrap_or_else(|_| "development".into());
18 0 :
19 0 : let guard = sentry::init((
20 0 : dsn,
21 0 : sentry::ClientOptions {
22 0 : release: release_name.clone(),
23 0 : environment: Some(environment.clone().into()),
24 0 : ..Default::default()
25 0 : },
26 0 : ));
27 0 : sentry::configure_scope(|scope| {
28 0 : for &(key, value) in extra_options {
29 0 : scope.set_extra(key, value.into());
30 0 : }
31 0 : });
32 :
33 0 : if let Some(dsn) = guard.dsn() {
34 0 : info!(
35 0 : "initialized Sentry for project {}, environment {}, release {} (using API {})",
36 0 : dsn.project_id(),
37 0 : environment,
38 0 : release_name.unwrap_or(Cow::Borrowed("None")),
39 0 : dsn.envelope_api_url(),
40 : );
41 : } else {
42 : // This should panic during sentry::init(), but we may as well cover it.
43 0 : error!("failed to initialize Sentry, invalid DSN");
44 : }
45 :
46 0 : Some(guard)
47 0 : }
|