From dc945e1b1e34b8227bf9445c8d48c15948390b93 Mon Sep 17 00:00:00 2001 From: endigma Date: Sun, 1 Oct 2023 11:34:55 -0300 Subject: [PATCH] remove telemetry --- .github/workflows/release.yaml | 2 - crates/cli/src/main.rs | 66 ----------- crates/cli/src/telemetry/mod.rs | 35 ------ crates/cli/src/telemetry/segment.rs | 175 ---------------------------- crates/cli/src/telemetry/sentry.rs | 25 ---- 5 files changed, 303 deletions(-) delete mode 100644 crates/cli/src/telemetry/mod.rs delete mode 100644 crates/cli/src/telemetry/segment.rs delete mode 100644 crates/cli/src/telemetry/sentry.rs diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index cc6bcd8..6959881 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -86,8 +86,6 @@ jobs: command: build args: -p katoa-cli --release --target ${{ matrix.target }} -F ${{ matrix.features }} env: - # SENTRY_AUTH_TOKEN: \$\{{ secrets.SENTRY_AUTH_TOKEN }} - # SEGMENT_WRITE_KEY: \$\{{ secrets.SEGMENT_WRITE_KEY }} CC_aarch64_unknown_linux_musl: clang AR_aarch64_unknown_linux_musl: llvm-ar CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUSTFLAGS: -Clink-self-contained=yes -Clinker=rust-lld diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index eedd674..cefc3f2 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -5,8 +5,6 @@ mod git; mod job; mod logging; mod oci; -#[cfg(feature = "telemetry")] -mod telemetry; #[cfg(feature = "self-update")] mod update; mod util; @@ -23,8 +21,6 @@ use std::{ path::{Path, PathBuf}, process::{ExitCode, Stdio}, }; -#[cfg(feature = "telemetry")] -use telemetry::{segment::TrackEvent, segment_enabled, sentry::sentry_init}; use tracing::{error, info, info_span, warn, Instrument}; #[cfg(feature = "self-update")] use update::check_for_update; @@ -509,28 +505,6 @@ impl Commands { info!(trigger = true); - // Only send telemetry when we know we should execute - #[cfg(feature = "telemetry")] - let telem_join = segment_enabled().then(|| { - let pipeline_name = pipeline_name.clone(); - let pipeline_length = std::fs::read_to_string(&pipeline_path) - .map(|f| f.lines().count()) - .ok(); - - tokio::spawn( - TrackEvent::PipelineExecuted { - pipeline_name, - pipeline_length, - job_count: pipeline.jobs.len(), - step_count: pipeline - .jobs - .iter() - .fold(0, |acc, job| acc + job.steps.len()), - } - .post(), - ) - }); - let inspect_output = Command::new(oci_backend.as_str()) .args([ "inspect", @@ -772,11 +746,6 @@ impl Commands { } } - #[cfg(feature = "telemetry")] - if let Some(join) = telem_join { - join.await.ok(); - } - if exit_code != 0 { std::process::exit(exit_code) } @@ -1009,29 +978,10 @@ impl Commands { Commands::Debug { .. } => "debug", } } - - #[cfg(feature = "telemetry")] - fn track(&self) -> bool { - match self { - Commands::Run { .. } => true, - Commands::Step { .. } => false, - Commands::Init { .. } => true, - Commands::New { .. } => true, - Commands::Update => true, - Commands::Completions { .. } => false, - #[cfg(feature = "fig-completions")] - Commands::FigCompletion => false, - Commands::Open { .. } => false, - Commands::Doctor { .. } => true, - Commands::Debug { .. } => false, - } - } } #[tokio::main] async fn main() -> ExitCode { - #[cfg(feature = "telemetry")] - let _sentry_guard = sentry_init(); if let Err(err) = logging_init() { eprintln!("Failed to init logger: {err:#?}"); return ExitCode::FAILURE; @@ -1043,24 +993,8 @@ async fn main() -> ExitCode { let command = Commands::parse(); - #[cfg(feature = "telemetry")] - let telem_join = (command.track() && segment_enabled()).then(|| { - let subcommand = command.subcommand().to_owned(); - tokio::spawn( - TrackEvent::SubcommandExecuted { - subcommand_name: subcommand, - } - .post(), - ) - }); - let res = command.execute().await; - #[cfg(feature = "telemetry")] - if let Some(join) = telem_join { - join.await.ok(); - } - match res { Ok(_) => ExitCode::SUCCESS, Err(err) => { diff --git a/crates/cli/src/telemetry/mod.rs b/crates/cli/src/telemetry/mod.rs deleted file mode 100644 index e11537a..0000000 --- a/crates/cli/src/telemetry/mod.rs +++ /dev/null @@ -1,35 +0,0 @@ -pub(crate) mod segment; -pub(crate) mod sentry; - -use once_cell::sync::Lazy; - -const SEGMENT_WRITE_KEY: Option<&str> = option_env!("SEGMENT_WRITE_KEY"); - -static SEGMENT_ENABLED: Lazy = Lazy::new(|| { - let musl = cfg!(target_env = "musl"); - let debug = cfg!(debug_assertions); - let disabled = std::env::var_os("KATOA_DISABLE_TELEMETRY").is_some(); - let has_write_key = SEGMENT_WRITE_KEY.is_some(); - - !musl && !debug && !disabled && has_write_key -}); - -/// Segment tracks event based analytics -pub fn segment_enabled() -> bool { - *SEGMENT_ENABLED -} - -const SENTRY_AUTH_TOKEN: Option<&str> = option_env!("SENTRY_AUTH_TOKEN"); - -static SENTRY_ENABLED: Lazy = Lazy::new(|| { - let debug = cfg!(debug_assertions); - let disabled = std::env::var_os("KATOA_DISABLE_SENTRY").is_some(); - let has_auth_token = SENTRY_AUTH_TOKEN.is_some(); - - !debug && !disabled && has_auth_token -}); - -/// Sentry tracks error based analytics -pub fn sentry_enabled() -> bool { - *SENTRY_ENABLED -} diff --git a/crates/cli/src/telemetry/segment.rs b/crates/cli/src/telemetry/segment.rs deleted file mode 100644 index 80e4731..0000000 --- a/crates/cli/src/telemetry/segment.rs +++ /dev/null @@ -1,175 +0,0 @@ -use std::fs::read_to_string; - -use anyhow::Context; -use anyhow::Result; -use once_cell::sync::Lazy; -use serde::Deserialize; -use serde::Serialize; -use serde_json::Map; -use serde_json::Value; -use time::OffsetDateTime; -use uuid::Uuid; - -use crate::util::data_path; -use crate::util::digest; - -use super::SEGMENT_WRITE_KEY; - -static ANONYMOUS_ID: Lazy> = Lazy::new(|| { - let data_path = data_path().ok()?.join("segment_anonymous_id"); - - let contents = match read_to_string(&data_path) { - Ok(contents) => contents.trim().to_string(), - Err(_) => { - let uuid = Uuid::new_v4().to_string(); - std::fs::write(&data_path, &uuid).ok()?; - uuid - } - }; - - Some(contents) -}); - -static SEGMENT_SALT: Lazy> = Lazy::new(|| { - let data_path = data_path().ok()?.join("segment_salt"); - - let contents = match read_to_string(&data_path) { - Ok(contents) => contents.trim().to_string(), - Err(_) => { - let uuid = Uuid::new_v4().to_string(); - std::fs::write(&data_path, &uuid).ok()?; - uuid - } - }; - - Some(contents) -}); - -#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)] -pub enum TrackEvent { - SubcommandExecuted { - subcommand_name: String, - }, - PipelineExecuted { - pipeline_name: String, - pipeline_length: Option, - job_count: usize, - step_count: usize, - }, -} - -impl TrackEvent { - fn name(&self) -> &'static str { - match self { - TrackEvent::SubcommandExecuted { .. } => "Subcommand Executed", - TrackEvent::PipelineExecuted { .. } => "Pipeline Executed", - } - } - - pub async fn post(self) -> Result { - let segment_write_key = SEGMENT_WRITE_KEY.context("No segment write key found")?; - - let anonymous_id = (*ANONYMOUS_ID) - .to_owned() - .context("failed to acquire user id")?; - - let event_name = self.name().to_owned(); - - let mut properties: Map = match self { - TrackEvent::SubcommandExecuted { subcommand_name } => { - [("subcommand_name".to_owned(), Value::String(subcommand_name))] - .into_iter() - .collect() - } - TrackEvent::PipelineExecuted { - pipeline_name, - pipeline_length, - job_count, - step_count, - } => [ - ( - "pipeline_name_hash".into(), - digest( - format!( - "{}{pipeline_name}", - (*SEGMENT_SALT) - .to_owned() - .context("failed to acquire salt")? - ) - .as_bytes(), - ) - .into(), - ), - ("pipeline_length".into(), pipeline_length.into()), - ("job_count".to_owned(), job_count.into()), - ("step_count".to_owned(), step_count.into()), - ( - "gh_actions".into(), - std::env::var_os("GITHUB_ACTIONS").is_some().into(), - ), - ("vercel".into(), std::env::var_os("VERCEL").is_some().into()), - ( - "circle_ci".into(), - std::env::var_os("CIRCLECI").is_some().into(), - ), - ( - "gitlab".into(), - std::env::var_os("GITLAB_CI").is_some().into(), - ), - ("travis".into(), std::env::var_os("TRAVIS").is_some().into()), - ( - "jenkins".into(), - std::env::var_os("JENKINS_URL").is_some().into(), - ), - ( - "azure".into(), - std::env::var_os("BUILD_BUILDURI").is_some().into(), - ), - ] - .into_iter() - .collect(), - }; - - // Insert the default properties (os, architecture, environment, cli_version) - // - // Want to make sure the telemetry is useful but not too identifying basically - // just identify which binary build is being used and nothing about the user's - // env or machine besides that - properties.insert("os".to_owned(), std::env::consts::OS.into()); - properties.insert("architecture".to_owned(), std::env::consts::ARCH.into()); - - #[cfg(target_env = "gnu")] - properties.insert("environment".to_owned(), "gnu".into()); - #[cfg(target_env = "musl")] - properties.insert("environment".to_owned(), "musl".into()); - #[cfg(not(any(target_env = "gnu", target_env = "musl")))] - properties.insert("environment".to_owned(), Value::Null); - - properties.insert("cli_version".to_owned(), env!("CARGO_PKG_VERSION").into()); - - let timestamp = OffsetDateTime::now_utc(); - - reqwest::Client::new() - .post("https://api.segment.io/v1/track") - .basic_auth::<_, &str>(segment_write_key, None) - .json(&Track { - anonymous_id, - event: event_name, - properties, - timestamp, - }) - .send() - .await? - .error_for_status() - .context("failed to post track event") - } -} - -#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -struct Track { - anonymous_id: String, - event: String, - properties: Map, - timestamp: OffsetDateTime, -} diff --git a/crates/cli/src/telemetry/sentry.rs b/crates/cli/src/telemetry/sentry.rs deleted file mode 100644 index 3120038..0000000 --- a/crates/cli/src/telemetry/sentry.rs +++ /dev/null @@ -1,25 +0,0 @@ -use std::sync::Arc; - -use sentry::ClientInitGuard; - -use super::{sentry_enabled, SENTRY_AUTH_TOKEN}; - -pub(crate) fn sentry_init() -> Option { - sentry_enabled() - .then_some(SENTRY_AUTH_TOKEN) - .flatten() - .map(|token| { - sentry::init(( - token, - sentry::ClientOptions { - release: sentry::release_name!(), - before_send: Some(Arc::new(|mut event| { - // This is too identifying - event.server_name = None; - Some(event) - })), - ..Default::default() - }, - )) - }) -}