Skip to content

Commit c254961

Browse files
committed
Extract Sentry initialization code into dedicated function
1 parent b070d14 commit c254961

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

src/bin/server.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,20 @@
11
#![warn(clippy::all, rust_2018_idioms)]
22

33
use cargo_registry::{metrics::LogEncoder, util::errors::AppResult, App, Env};
4-
use std::{borrow::Cow, fs::File, process::Command, sync::Arc, time::Duration};
4+
use std::{fs::File, process::Command, sync::Arc, time::Duration};
55

66
use conduit_hyper::Service;
77
use futures_util::future::FutureExt;
88
use prometheus::Encoder;
99
use reqwest::blocking::Client;
10-
use sentry::{ClientOptions, IntoDsn};
1110
use std::io::Write;
1211
use tokio::io::AsyncWriteExt;
1312
use tokio::signal::unix::{signal, SignalKind};
1413

1514
const CORE_THREADS: usize = 4;
1615

1716
fn main() -> Result<(), Box<dyn std::error::Error>> {
18-
let _sentry = dotenv::var("SENTRY_DSN_API")
19-
.ok()
20-
.into_dsn()
21-
.expect("SENTRY_DSN_API is not a valid Sentry DSN value")
22-
.map(|dsn| {
23-
let mut opts = ClientOptions::from(dsn);
24-
opts.environment = Some(
25-
dotenv::var("SENTRY_ENV_API")
26-
.map(Cow::Owned)
27-
.expect("SENTRY_ENV_API must be set when using SENTRY_DSN_API"),
28-
);
29-
30-
opts.release = dotenv::var("HEROKU_SLUG_COMMIT").ok().map(Into::into);
31-
32-
sentry::init(opts)
33-
});
17+
let _sentry = cargo_registry::sentry::init();
3418

3519
// Initialize logging
3620
tracing_subscriber::fmt::init();

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub mod util;
5555
pub mod controllers;
5656
pub mod models;
5757
mod router;
58+
pub mod sentry;
5859
pub mod views;
5960

6061
/// Used for setting different values depending on whether the app is being run in production,

src/sentry.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use sentry::{ClientInitGuard, ClientOptions, IntoDsn};
2+
use std::borrow::Cow;
3+
4+
#[must_use]
5+
pub fn init() -> Option<ClientInitGuard> {
6+
dotenv::var("SENTRY_DSN_API")
7+
.ok()
8+
.into_dsn()
9+
.expect("SENTRY_DSN_API is not a valid Sentry DSN value")
10+
.map(|dsn| {
11+
let mut opts = ClientOptions::from(dsn);
12+
opts.environment = Some(
13+
dotenv::var("SENTRY_ENV_API")
14+
.map(Cow::Owned)
15+
.expect("SENTRY_ENV_API must be set when using SENTRY_DSN_API"),
16+
);
17+
18+
opts.release = dotenv::var("HEROKU_SLUG_COMMIT").ok().map(Into::into);
19+
20+
sentry::init(opts)
21+
})
22+
}

0 commit comments

Comments
 (0)