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.
This commit is contained in:
Thomas Eizinger
2024-12-17 17:22:38 +01:00
committed by GitHub
parent 1f457d2127
commit 98be884c3a

View File

@@ -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.
// <https://docs.sentry.io/platforms/rust/configuration/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::User>) {
sentry::Hub::main().configure_scope(|scope| scope.set_user(user));
}