|
| 1 | +use anyhow::*; |
| 2 | +use serde_json::json; |
| 3 | +use sysinfo::System; |
| 4 | +use tokio::{ |
| 5 | + sync::{Mutex, OnceCell}, |
| 6 | + task::JoinSet, |
| 7 | + time::Duration, |
| 8 | +}; |
| 9 | +use toolchain::{meta, paths}; |
| 10 | + |
| 11 | +pub static JOIN_SET: OnceCell<Mutex<JoinSet<()>>> = OnceCell::const_new(); |
| 12 | + |
| 13 | +/// Get the global join set for telemetry futures. |
| 14 | +async fn join_set() -> &'static Mutex<JoinSet<()>> { |
| 15 | + JOIN_SET |
| 16 | + .get_or_init(|| async { Mutex::new(JoinSet::new()) }) |
| 17 | + .await |
| 18 | +} |
| 19 | + |
| 20 | +/// Waits for all telemetry events to finish. |
| 21 | +pub async fn wait_all() { |
| 22 | + let mut join_set = join_set().await.lock().await; |
| 23 | + match tokio::time::timeout(Duration::from_secs(5), async move { |
| 24 | + while join_set.join_next().await.is_some() {} |
| 25 | + }) |
| 26 | + .await |
| 27 | + { |
| 28 | + Result::Ok(_) => {} |
| 29 | + Err(_) => { |
| 30 | + println!("Timed out waiting for request to finish") |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// This API key is safe to hardcode. It will not change and is intended to be public. |
| 36 | +const POSTHOG_API_KEY: &str = "phc_6kfTNEAVw7rn1LA51cO3D69FefbKupSWFaM7OUgEpEo"; |
| 37 | + |
| 38 | +fn build_client() -> async_posthog::Client { |
| 39 | + async_posthog::client(POSTHOG_API_KEY) |
| 40 | +} |
| 41 | + |
| 42 | +/// Builds a new PostHog event with associated data. |
| 43 | +/// |
| 44 | +/// This is slightly expensive, so it should not be used frequently. |
| 45 | +pub async fn capture_event<F: FnOnce(&mut async_posthog::Event) -> Result<()>>( |
| 46 | + name: &str, |
| 47 | + mutate: Option<F>, |
| 48 | +) -> Result<()> { |
| 49 | + // Check if telemetry disabled |
| 50 | + let (toolchain_instance_id, telemetry_disabled, api_endpoint) = |
| 51 | + meta::read_project(&paths::data_dir()?, |x| { |
| 52 | + let api_endpoint = x.cloud.as_ref().map(|cloud| cloud.api_endpoint.clone()); |
| 53 | + (x.toolchain_instance_id, x.telemetry_disabled, api_endpoint) |
| 54 | + }) |
| 55 | + .await?; |
| 56 | + |
| 57 | + if telemetry_disabled { |
| 58 | + return Ok(()); |
| 59 | + } |
| 60 | + |
| 61 | + // Read project ID. If not signed in or fails to reach server, then ignore. |
| 62 | + let (project_id, project_name) = match toolchain::toolchain_ctx::try_load().await { |
| 63 | + Result::Ok(Some(ctx)) => ( |
| 64 | + Some(ctx.project.game_id), |
| 65 | + Some(ctx.project.display_name.clone()), |
| 66 | + ), |
| 67 | + Result::Ok(None) => (None, None), |
| 68 | + Err(_) => { |
| 69 | + // Ignore error |
| 70 | + (None, None) |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + let distinct_id = format!("toolchain:{toolchain_instance_id}"); |
| 75 | + |
| 76 | + let mut event = async_posthog::Event::new(name, &distinct_id); |
| 77 | + |
| 78 | + // Helps us understand what version of the CLI is being used. |
| 79 | + let version = json!({ |
| 80 | + "git_sha": env!("VERGEN_GIT_SHA"), |
| 81 | + "git_branch": env!("VERGEN_GIT_BRANCH"), |
| 82 | + "build_semver": env!("CARGO_PKG_VERSION"), |
| 83 | + "build_timestamp": env!("VERGEN_BUILD_TIMESTAMP"), |
| 84 | + "build_target": env!("VERGEN_CARGO_TARGET_TRIPLE"), |
| 85 | + "build_debug": env!("VERGEN_CARGO_DEBUG"), |
| 86 | + "rustc_version": env!("VERGEN_RUSTC_SEMVER"), |
| 87 | + }); |
| 88 | + |
| 89 | + // Add properties |
| 90 | + if let Some(project_id) = project_id { |
| 91 | + event.insert_prop( |
| 92 | + "$groups", |
| 93 | + &json!({ |
| 94 | + "project_id": project_id, |
| 95 | + }), |
| 96 | + )?; |
| 97 | + } |
| 98 | + |
| 99 | + event.insert_prop( |
| 100 | + "$set", |
| 101 | + &json!({ |
| 102 | + "name": project_name, |
| 103 | + "toolchain_instance_id": toolchain_instance_id, |
| 104 | + "api_endpoint": api_endpoint, |
| 105 | + "version": version, |
| 106 | + "project_id": project_id, |
| 107 | + "project_root": paths::project_root()?, |
| 108 | + "sys": { |
| 109 | + "name": System::name(), |
| 110 | + "kernel_version": System::kernel_version(), |
| 111 | + "os_version": System::os_version(), |
| 112 | + "host_name": System::host_name(), |
| 113 | + "cpu_arch": System::cpu_arch(), |
| 114 | + }, |
| 115 | + }), |
| 116 | + )?; |
| 117 | + |
| 118 | + event.insert_prop("api_endpoint", api_endpoint)?; |
| 119 | + event.insert_prop("args", std::env::args().collect::<Vec<_>>())?; |
| 120 | + |
| 121 | + // Customize the event properties |
| 122 | + if let Some(mutate) = mutate { |
| 123 | + mutate(&mut event)?; |
| 124 | + } |
| 125 | + |
| 126 | + // Capture event |
| 127 | + join_set().await.lock().await.spawn(async move { |
| 128 | + match build_client().capture(event).await { |
| 129 | + Result::Ok(_) => {} |
| 130 | + Err(_) => { |
| 131 | + // Fail silently |
| 132 | + } |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + Ok(()) |
| 137 | +} |
0 commit comments