add user to sentry scope outside of requests context (#131)

This commit is contained in:
Anastasiia Solop
2025-07-11 12:57:44 +02:00
committed by GitHub
parent 55a7bd1cf3
commit 790d05bef5
3 changed files with 29 additions and 13 deletions

View File

@@ -189,4 +189,29 @@ impl AppState {
tracing::debug!("Analytics disabled, skipping event: {}", event_name);
}
}
pub async fn update_sentry_scope(&self) {
let config = self.get_config().read().await;
let username = config.github.username.clone();
let email = config.github.primary_email.clone();
drop(config);
let sentry_user = if username.is_some() || email.is_some() {
sentry::User {
id: Some(self.user_id.clone()),
username,
email,
..Default::default()
}
} else {
sentry::User {
id: Some(self.user_id.clone()),
..Default::default()
}
};
sentry::configure_scope(|scope| {
scope.set_user(Some(sentry_user));
});
}
}

View File

@@ -167,6 +167,8 @@ fn main() -> anyhow::Result<()> {
// Create app state
let app_state = AppState::new(pool.clone(), config_arc.clone()).await;
app_state.update_sentry_scope().await;
// Track session start event
app_state.track_analytics_event("session_start", None).await;
// Start background task to check for init status and spawn processes

View File

@@ -229,6 +229,7 @@ async fn device_poll(
});
}
}
app_state.update_sentry_scope().await;
// Identify user in PostHog
let mut props = serde_json::Map::new();
if let Some(ref username) = username {
@@ -304,18 +305,6 @@ pub async fn sentry_user_context_middleware(
req: Request,
next: Next,
) -> Response {
let config = app_state.get_config().read().await;
let username = config.github.username.clone();
let email = config.github.primary_email.clone();
drop(config);
if username.is_some() || email.is_some() {
sentry::configure_scope(|scope| {
scope.set_user(Some(sentry::User {
username,
email,
..Default::default()
}));
});
}
app_state.update_sentry_scope().await;
next.run(req).await
}