Skip to content

sentry: Simplify init() function #3981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/sentry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use sentry::{ClientInitGuard, ClientOptions, IntoDsn};
use std::borrow::Cow;

/// Initializes the Sentry SDK from the environment variables.
///
Expand All @@ -9,22 +8,26 @@ use std::borrow::Cow;
///
/// `HEROKU_SLUG_COMMIT`, if present, will be used as the `release` property
/// on all events.
#[must_use]
pub fn init() -> Option<ClientInitGuard> {
dotenv::var("SENTRY_DSN_API")
pub fn init() -> ClientInitGuard {
let dsn = dotenv::var("SENTRY_DSN_API")
.ok()
.into_dsn()
.expect("SENTRY_DSN_API is not a valid Sentry DSN value")
.map(|dsn| {
let mut opts = ClientOptions::from(dsn);
opts.environment = Some(
dotenv::var("SENTRY_ENV_API")
.map(Cow::Owned)
.expect("SENTRY_ENV_API must be set when using SENTRY_DSN_API"),
);
.expect("SENTRY_DSN_API is not a valid Sentry DSN value");

opts.release = dotenv::var("HEROKU_SLUG_COMMIT").ok().map(Into::into);
let environment = dsn.as_ref().map(|_| {
dotenv::var("SENTRY_ENV_API")
.expect("SENTRY_ENV_API must be set when using SENTRY_DSN_API")
.into()
});

sentry::init(opts)
})
let release = dotenv::var("HEROKU_SLUG_COMMIT").ok().map(Into::into);

let opts = ClientOptions {
dsn,
environment,
release,
..Default::default()
};

sentry::init(opts)
}