fix(telemetry): don't log events for local and CI env (#9492)

Avoids spamming PostHog with events from our CI or other instances of
the docker-compose setup.
This commit is contained in:
Thomas Eizinger
2025-06-10 16:34:20 +02:00
committed by GitHub
parent 1fa345aa5e
commit 182a560091
4 changed files with 25 additions and 10 deletions

View File

@@ -48,8 +48,14 @@ async fn capture<P>(
where
P: Serialize,
{
let event = event.into();
let env = Env::from_api_url(&api_url);
let api_key = crate::posthog::api_key_for_env(env);
let Some(api_key) = crate::posthog::api_key_for_env(env) else {
tracing::debug!(%event, %env, "Not sending event because we don't have an API key");
return Ok(());
};
let response = reqwest::ClientBuilder::new()
.connection_verbose(true)
@@ -58,7 +64,7 @@ where
.json(&CaptureRequest {
api_key: api_key.to_string(),
distinct_id,
event: event.into(),
event,
properties,
})
.send()

View File

@@ -28,7 +28,7 @@ pub(crate) fn reevaluate(user_id: String, env: &str) {
let api_key = match env.parse() {
Ok(Env::Production) => POSTHOG_API_KEY_PROD,
Ok(Env::Staging) => POSTHOG_API_KEY_STAGING,
Ok(Env::OnPrem) | Err(_) => return,
Ok(Env::OnPrem | Env::DockerCompose | Env::Localhost) | Err(_) => return,
};
RUNTIME.spawn(async move {

View File

@@ -44,14 +44,18 @@ pub const TESTING: Dsn = Dsn(
pub(crate) enum Env {
Production,
Staging,
DockerCompose,
Localhost,
OnPrem,
}
impl Env {
pub(crate) fn from_api_url(api_url: &str) -> Self {
match api_url {
"wss://api.firezone.dev" | "wss://api.firezone.dev/" => Self::Production,
"wss://api.firez.one" | "wss://api.firez.one/" => Self::Staging,
match api_url.trim_end_matches('/') {
"wss://api.firezone.dev" => Self::Production,
"wss://api.firez.one" => Self::Staging,
"ws://api:8081" => Self::DockerCompose,
"ws://localhost:8081" => Self::DockerCompose,
_ => Self::OnPrem,
}
}
@@ -60,6 +64,8 @@ impl Env {
match self {
Env::Production => "production",
Env::Staging => "staging",
Env::DockerCompose => "docker-compose",
Env::Localhost => "localhost",
Env::OnPrem => "on-prem",
}
}
@@ -72,6 +78,8 @@ impl FromStr for Env {
match s {
"production" => Ok(Self::Production),
"staging" => Ok(Self::Staging),
"docker-compose" => Ok(Self::DockerCompose),
"localhost" => Ok(Self::Localhost),
"on-prem" => Ok(Self::OnPrem),
other => bail!("Unknown env `{other}`"),
}

View File

@@ -9,11 +9,12 @@ pub(crate) const POSTHOG_API_KEY_ON_PREM: &str = "phc_4R9Ii6q4SEofVkH7LvajwuJ3ns
pub(crate) static RUNTIME: LazyLock<Runtime> = LazyLock::new(init_runtime);
pub(crate) fn api_key_for_env(env: Env) -> &'static str {
pub(crate) fn api_key_for_env(env: Env) -> Option<&'static str> {
match env {
Env::Production => POSTHOG_API_KEY_PROD,
Env::Staging => POSTHOG_API_KEY_STAGING,
Env::OnPrem => POSTHOG_API_KEY_ON_PREM,
Env::Production => Some(POSTHOG_API_KEY_PROD),
Env::Staging => Some(POSTHOG_API_KEY_STAGING),
Env::OnPrem => Some(POSTHOG_API_KEY_ON_PREM),
Env::DockerCompose | Env::Localhost => None,
}
}