fix sentry logging (#982)
This commit is contained in:
committed by
GitHub
parent
f2cc538a8f
commit
e52ced99e0
@@ -19,6 +19,7 @@ rust-embed = "8.2"
|
||||
directories = "6.0.0"
|
||||
open = "5.3.2"
|
||||
regex = "1.11.1"
|
||||
sentry = { version = "0.41.0", features = ["anyhow", "backtrace", "panic", "debug-images"] }
|
||||
sentry-tracing = { version = "0.41.0", features = ["backtrace"] }
|
||||
lazy_static = "1.4"
|
||||
futures-util = "0.3"
|
||||
|
||||
@@ -1,6 +1,71 @@
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use sentry_tracing::{EventFilter, SentryLayer};
|
||||
use tracing::Level;
|
||||
|
||||
const SENTRY_DSN: &str = "https://1065a1d276a581316999a07d5dffee26@o4509603705192449.ingest.de.sentry.io/4509605576441937";
|
||||
|
||||
static INIT_GUARD: OnceLock<sentry::ClientInitGuard> = OnceLock::new();
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum SentrySource {
|
||||
Backend,
|
||||
Mcp,
|
||||
}
|
||||
|
||||
impl SentrySource {
|
||||
fn tag(self) -> &'static str {
|
||||
match self {
|
||||
SentrySource::Backend => "backend",
|
||||
SentrySource::Mcp => "mcp",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn environment() -> &'static str {
|
||||
if cfg!(debug_assertions) {
|
||||
"dev"
|
||||
} else {
|
||||
"production"
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_once(source: SentrySource) {
|
||||
INIT_GUARD.get_or_init(|| {
|
||||
sentry::init((
|
||||
SENTRY_DSN,
|
||||
sentry::ClientOptions {
|
||||
release: sentry::release_name!(),
|
||||
environment: Some(environment().into()),
|
||||
..Default::default()
|
||||
},
|
||||
))
|
||||
});
|
||||
|
||||
sentry::configure_scope(|scope| {
|
||||
scope.set_tag("source", source.tag());
|
||||
});
|
||||
}
|
||||
|
||||
pub fn configure_user_scope(user_id: &str, username: Option<&str>, email: Option<&str>) {
|
||||
let mut sentry_user = sentry::User {
|
||||
id: Some(user_id.to_string()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
if let Some(username) = username {
|
||||
sentry_user.username = Some(username.to_string());
|
||||
}
|
||||
|
||||
if let Some(email) = email {
|
||||
sentry_user.email = Some(email.to_string());
|
||||
}
|
||||
|
||||
sentry::configure_scope(|scope| {
|
||||
scope.set_user(Some(sentry_user));
|
||||
});
|
||||
}
|
||||
|
||||
pub fn sentry_layer<S>() -> SentryLayer<S>
|
||||
where
|
||||
S: tracing::Subscriber,
|
||||
|
||||
Reference in New Issue
Block a user