add user to sentry scope outside of requests context (#131)
This commit is contained in:
@@ -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));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user