From 98be884c3af8e59c685d0eeb6f28c3f45fadaac1 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Dec 2024 17:22:38 +0100 Subject: [PATCH] fix(telemetry): dispose previous session when starting new one (#7542) For persistent applications like the IPC service, it is possible that telemetry gets initialised with different parameters depending on what the user logs in with. Currently, only the first one is persisted and all consecutive ones are ignored, leading to events that may be wrongly tagged for a certain user / environment. To fix this, we only skip the init if we are still in the same environment. Otherwise, the close the previous session and initialise a new one. Fixes: #7525. --- rust/telemetry/src/lib.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/rust/telemetry/src/lib.rs b/rust/telemetry/src/lib.rs index 7bdbfaa08..a2cd0b332 100644 --- a/rust/telemetry/src/lib.rs +++ b/rust/telemetry/src/lib.rs @@ -38,10 +38,6 @@ impl Drop for Telemetry { impl Telemetry { pub fn start(&mut self, api_url: &str, release: &str, dsn: Dsn) { - if self.inner.is_some() { - return; - } - // Can't use URLs as `environment` directly, because Sentry doesn't allow slashes in environments. // let environment = match api_url { @@ -54,6 +50,29 @@ impl Telemetry { } }; + if self + .inner + .as_ref() + .and_then(|i| i.options().environment.as_ref()) + .is_some_and(|env| env == environment) + { + tracing::debug!("Telemetry already initialised"); + + return; + } + + // Stop any previous telemetry session. + if let Some(inner) = self.inner.take() { + tracing::debug!("Stopping previous telemetry session"); + + sentry::end_session(); + drop(inner); + + self.account_slug = None; + self.firezone_id = None; + set_current_user(None); + } + tracing::info!("Starting telemetry"); let inner = sentry::init(( dsn.0, @@ -142,6 +161,10 @@ impl Telemetry { .map(|slug| ("account_slug".to_owned(), slug.into())), ); - sentry::Hub::main().configure_scope(|scope| scope.set_user(Some(user))); + set_current_user(Some(user)); } } + +fn set_current_user(user: Option) { + sentry::Hub::main().configure_scope(|scope| scope.set_user(user)); +}