telemetry.rs

  1use crate::{TelemetrySettings, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL};
  2use chrono::{DateTime, Utc};
  3use gpui::{serde_json, AppContext, AppMetadata, BackgroundExecutor, Task};
  4use lazy_static::lazy_static;
  5use parking_lot::Mutex;
  6use serde::Serialize;
  7use settings::Settings;
  8use std::{env, io::Write, mem, path::PathBuf, sync::Arc, time::Duration};
  9use sysinfo::{
 10    CpuRefreshKind, Pid, PidExt, ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt,
 11};
 12use tempfile::NamedTempFile;
 13use util::http::HttpClient;
 14use util::{channel::ReleaseChannel, TryFutureExt};
 15
 16pub struct Telemetry {
 17    http_client: Arc<dyn HttpClient>,
 18    executor: BackgroundExecutor,
 19    state: Mutex<TelemetryState>,
 20}
 21
 22struct TelemetryState {
 23    metrics_id: Option<Arc<str>>,      // Per logged-in user
 24    installation_id: Option<Arc<str>>, // Per app installation (different for dev, nightly, preview, and stable)
 25    session_id: Option<Arc<str>>,      // Per app launch
 26    release_channel: Option<&'static str>,
 27    app_metadata: AppMetadata,
 28    architecture: &'static str,
 29    clickhouse_events_queue: Vec<ClickhouseEventWrapper>,
 30    flush_clickhouse_events_task: Option<Task<()>>,
 31    log_file: Option<NamedTempFile>,
 32    is_staff: Option<bool>,
 33    first_event_datetime: Option<DateTime<Utc>>,
 34}
 35
 36const CLICKHOUSE_EVENTS_URL_PATH: &'static str = "/api/events";
 37
 38lazy_static! {
 39    static ref CLICKHOUSE_EVENTS_URL: String =
 40        format!("{}{}", *ZED_SERVER_URL, CLICKHOUSE_EVENTS_URL_PATH);
 41}
 42
 43#[derive(Serialize, Debug)]
 44struct ClickhouseEventRequestBody {
 45    token: &'static str,
 46    installation_id: Option<Arc<str>>,
 47    session_id: Option<Arc<str>>,
 48    is_staff: Option<bool>,
 49    app_version: Option<String>,
 50    os_name: &'static str,
 51    os_version: Option<String>,
 52    architecture: &'static str,
 53    release_channel: Option<&'static str>,
 54    events: Vec<ClickhouseEventWrapper>,
 55}
 56
 57#[derive(Serialize, Debug)]
 58struct ClickhouseEventWrapper {
 59    signed_in: bool,
 60    #[serde(flatten)]
 61    event: ClickhouseEvent,
 62}
 63
 64#[derive(Serialize, Debug)]
 65#[serde(rename_all = "snake_case")]
 66pub enum AssistantKind {
 67    Panel,
 68    Inline,
 69}
 70
 71#[derive(Serialize, Debug)]
 72#[serde(tag = "type")]
 73pub enum ClickhouseEvent {
 74    Editor {
 75        operation: &'static str,
 76        file_extension: Option<String>,
 77        vim_mode: bool,
 78        copilot_enabled: bool,
 79        copilot_enabled_for_language: bool,
 80        milliseconds_since_first_event: i64,
 81    },
 82    Copilot {
 83        suggestion_id: Option<String>,
 84        suggestion_accepted: bool,
 85        file_extension: Option<String>,
 86        milliseconds_since_first_event: i64,
 87    },
 88    Call {
 89        operation: &'static str,
 90        room_id: Option<u64>,
 91        channel_id: Option<u64>,
 92        milliseconds_since_first_event: i64,
 93    },
 94    Assistant {
 95        conversation_id: Option<String>,
 96        kind: AssistantKind,
 97        model: &'static str,
 98        milliseconds_since_first_event: i64,
 99    },
100    Cpu {
101        usage_as_percentage: f32,
102        core_count: u32,
103        milliseconds_since_first_event: i64,
104    },
105    Memory {
106        memory_in_bytes: u64,
107        virtual_memory_in_bytes: u64,
108        milliseconds_since_first_event: i64,
109    },
110    App {
111        operation: &'static str,
112        milliseconds_since_first_event: i64,
113    },
114}
115
116#[cfg(debug_assertions)]
117const MAX_QUEUE_LEN: usize = 1;
118
119#[cfg(not(debug_assertions))]
120const MAX_QUEUE_LEN: usize = 10;
121
122#[cfg(debug_assertions)]
123const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(1);
124
125#[cfg(not(debug_assertions))]
126const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(30);
127
128impl Telemetry {
129    pub fn new(client: Arc<dyn HttpClient>, cx: &mut AppContext) -> Arc<Self> {
130        let release_channel = if cx.has_global::<ReleaseChannel>() {
131            Some(cx.global::<ReleaseChannel>().display_name())
132        } else {
133            None
134        };
135
136        // TODO: Replace all hardware stuff with nested SystemSpecs json
137        let this = Arc::new(Self {
138            http_client: client,
139            executor: cx.background_executor().clone(),
140            state: Mutex::new(TelemetryState {
141                app_metadata: cx.app_metadata(),
142                architecture: env::consts::ARCH,
143                release_channel,
144                installation_id: None,
145                metrics_id: None,
146                session_id: None,
147                clickhouse_events_queue: Default::default(),
148                flush_clickhouse_events_task: Default::default(),
149                log_file: None,
150                is_staff: None,
151                first_event_datetime: None,
152            }),
153        });
154
155        // We should only ever have one instance of Telemetry, leak the subscription to keep it alive
156        // rather than store in TelemetryState, complicating spawn as subscriptions are not Send
157        // std::mem::forget(cx.on_app_quit({
158        //     let this = this.clone();
159        //     move |cx| this.shutdown_telemetry(cx)
160        // }));
161
162        this
163    }
164
165    // fn shutdown_telemetry(self: &Arc<Self>, cx: &mut AppContext) -> impl Future<Output = ()> {
166    //     let telemetry_settings = TelemetrySettings::get_global(cx).clone();
167    //     self.report_app_event(telemetry_settings, "close");
168    //     Task::ready(())
169    // }
170
171    pub fn log_file_path(&self) -> Option<PathBuf> {
172        Some(self.state.lock().log_file.as_ref()?.path().to_path_buf())
173    }
174
175    pub fn start(
176        self: &Arc<Self>,
177        installation_id: Option<String>,
178        session_id: String,
179        cx: &mut AppContext,
180    ) {
181        let mut state = self.state.lock();
182        state.installation_id = installation_id.map(|id| id.into());
183        state.session_id = Some(session_id.into());
184        drop(state);
185
186        let this = self.clone();
187        cx.spawn(|cx| async move {
188            // Avoiding calling `System::new_all()`, as there have been crashes related to it
189            let refresh_kind = RefreshKind::new()
190                .with_memory() // For memory usage
191                .with_processes(ProcessRefreshKind::everything()) // For process usage
192                .with_cpu(CpuRefreshKind::everything()); // For core count
193
194            let mut system = System::new_with_specifics(refresh_kind);
195
196            // Avoiding calling `refresh_all()`, just update what we need
197            system.refresh_specifics(refresh_kind);
198
199            loop {
200                // Waiting some amount of time before the first query is important to get a reasonable value
201                // https://docs.rs/sysinfo/0.29.10/sysinfo/trait.ProcessExt.html#tymethod.cpu_usage
202                const DURATION_BETWEEN_SYSTEM_EVENTS: Duration = Duration::from_secs(60);
203                smol::Timer::after(DURATION_BETWEEN_SYSTEM_EVENTS).await;
204
205                system.refresh_specifics(refresh_kind);
206
207                let current_process = Pid::from_u32(std::process::id());
208                let Some(process) = system.processes().get(&current_process) else {
209                    let process = current_process;
210                    log::error!("Failed to find own process {process:?} in system process table");
211                    // TODO: Fire an error telemetry event
212                    return;
213                };
214
215                let telemetry_settings = if let Ok(telemetry_settings) =
216                    cx.update(|cx| *TelemetrySettings::get_global(cx))
217                {
218                    telemetry_settings
219                } else {
220                    break;
221                };
222
223                this.report_memory_event(
224                    telemetry_settings,
225                    process.memory(),
226                    process.virtual_memory(),
227                );
228                this.report_cpu_event(
229                    telemetry_settings,
230                    process.cpu_usage(),
231                    system.cpus().len() as u32,
232                );
233            }
234        })
235        .detach();
236    }
237
238    pub fn set_authenticated_user_info(
239        self: &Arc<Self>,
240        metrics_id: Option<String>,
241        is_staff: bool,
242        cx: &AppContext,
243    ) {
244        if !TelemetrySettings::get_global(cx).metrics {
245            return;
246        }
247
248        let mut state = self.state.lock();
249        let metrics_id: Option<Arc<str>> = metrics_id.map(|id| id.into());
250        state.metrics_id = metrics_id.clone();
251        state.is_staff = Some(is_staff);
252        drop(state);
253    }
254
255    pub fn report_editor_event(
256        self: &Arc<Self>,
257        telemetry_settings: TelemetrySettings,
258        file_extension: Option<String>,
259        vim_mode: bool,
260        operation: &'static str,
261        copilot_enabled: bool,
262        copilot_enabled_for_language: bool,
263    ) {
264        let event = ClickhouseEvent::Editor {
265            file_extension,
266            vim_mode,
267            operation,
268            copilot_enabled,
269            copilot_enabled_for_language,
270            milliseconds_since_first_event: self.milliseconds_since_first_event(),
271        };
272
273        self.report_clickhouse_event(event, telemetry_settings, false)
274    }
275
276    pub fn report_copilot_event(
277        self: &Arc<Self>,
278        telemetry_settings: TelemetrySettings,
279        suggestion_id: Option<String>,
280        suggestion_accepted: bool,
281        file_extension: Option<String>,
282    ) {
283        let event = ClickhouseEvent::Copilot {
284            suggestion_id,
285            suggestion_accepted,
286            file_extension,
287            milliseconds_since_first_event: self.milliseconds_since_first_event(),
288        };
289
290        self.report_clickhouse_event(event, telemetry_settings, false)
291    }
292
293    pub fn report_assistant_event(
294        self: &Arc<Self>,
295        telemetry_settings: TelemetrySettings,
296        conversation_id: Option<String>,
297        kind: AssistantKind,
298        model: &'static str,
299    ) {
300        let event = ClickhouseEvent::Assistant {
301            conversation_id,
302            kind,
303            model,
304            milliseconds_since_first_event: self.milliseconds_since_first_event(),
305        };
306
307        self.report_clickhouse_event(event, telemetry_settings, false)
308    }
309
310    pub fn report_call_event(
311        self: &Arc<Self>,
312        telemetry_settings: TelemetrySettings,
313        operation: &'static str,
314        room_id: Option<u64>,
315        channel_id: Option<u64>,
316    ) {
317        let event = ClickhouseEvent::Call {
318            operation,
319            room_id,
320            channel_id,
321            milliseconds_since_first_event: self.milliseconds_since_first_event(),
322        };
323
324        self.report_clickhouse_event(event, telemetry_settings, false)
325    }
326
327    pub fn report_cpu_event(
328        self: &Arc<Self>,
329        telemetry_settings: TelemetrySettings,
330        usage_as_percentage: f32,
331        core_count: u32,
332    ) {
333        let event = ClickhouseEvent::Cpu {
334            usage_as_percentage,
335            core_count,
336            milliseconds_since_first_event: self.milliseconds_since_first_event(),
337        };
338
339        self.report_clickhouse_event(event, telemetry_settings, false)
340    }
341
342    pub fn report_memory_event(
343        self: &Arc<Self>,
344        telemetry_settings: TelemetrySettings,
345        memory_in_bytes: u64,
346        virtual_memory_in_bytes: u64,
347    ) {
348        let event = ClickhouseEvent::Memory {
349            memory_in_bytes,
350            virtual_memory_in_bytes,
351            milliseconds_since_first_event: self.milliseconds_since_first_event(),
352        };
353
354        self.report_clickhouse_event(event, telemetry_settings, false)
355    }
356
357    // app_events are called at app open and app close, so flush is set to immediately send
358    pub fn report_app_event(
359        self: &Arc<Self>,
360        telemetry_settings: TelemetrySettings,
361        operation: &'static str,
362    ) {
363        let event = ClickhouseEvent::App {
364            operation,
365            milliseconds_since_first_event: self.milliseconds_since_first_event(),
366        };
367
368        self.report_clickhouse_event(event, telemetry_settings, true)
369    }
370
371    fn milliseconds_since_first_event(&self) -> i64 {
372        let mut state = self.state.lock();
373        match state.first_event_datetime {
374            Some(first_event_datetime) => {
375                let now: DateTime<Utc> = Utc::now();
376                now.timestamp_millis() - first_event_datetime.timestamp_millis()
377            }
378            None => {
379                state.first_event_datetime = Some(Utc::now());
380                0
381            }
382        }
383    }
384
385    fn report_clickhouse_event(
386        self: &Arc<Self>,
387        event: ClickhouseEvent,
388        telemetry_settings: TelemetrySettings,
389        immediate_flush: bool,
390    ) {
391        if !telemetry_settings.metrics {
392            return;
393        }
394
395        let mut state = self.state.lock();
396        let signed_in = state.metrics_id.is_some();
397        state
398            .clickhouse_events_queue
399            .push(ClickhouseEventWrapper { signed_in, event });
400
401        if state.installation_id.is_some() {
402            if immediate_flush || state.clickhouse_events_queue.len() >= MAX_QUEUE_LEN {
403                drop(state);
404                self.flush_clickhouse_events();
405            } else {
406                let this = self.clone();
407                let executor = self.executor.clone();
408                state.flush_clickhouse_events_task = Some(self.executor.spawn(async move {
409                    executor.timer(DEBOUNCE_INTERVAL).await;
410                    this.flush_clickhouse_events();
411                }));
412            }
413        }
414    }
415
416    pub fn metrics_id(self: &Arc<Self>) -> Option<Arc<str>> {
417        self.state.lock().metrics_id.clone()
418    }
419
420    pub fn installation_id(self: &Arc<Self>) -> Option<Arc<str>> {
421        self.state.lock().installation_id.clone()
422    }
423
424    pub fn is_staff(self: &Arc<Self>) -> Option<bool> {
425        self.state.lock().is_staff
426    }
427
428    fn flush_clickhouse_events(self: &Arc<Self>) {
429        let mut state = self.state.lock();
430        state.first_event_datetime = None;
431        let mut events = mem::take(&mut state.clickhouse_events_queue);
432        state.flush_clickhouse_events_task.take();
433        drop(state);
434
435        let this = self.clone();
436        self.executor
437            .spawn(
438                async move {
439                    let mut json_bytes = Vec::new();
440
441                    if let Some(file) = &mut this.state.lock().log_file {
442                        let file = file.as_file_mut();
443                        for event in &mut events {
444                            json_bytes.clear();
445                            serde_json::to_writer(&mut json_bytes, event)?;
446                            file.write_all(&json_bytes)?;
447                            file.write(b"\n")?;
448                        }
449                    }
450
451                    {
452                        let state = this.state.lock();
453                        let request_body = ClickhouseEventRequestBody {
454                            token: ZED_SECRET_CLIENT_TOKEN,
455                            installation_id: state.installation_id.clone(),
456                            session_id: state.session_id.clone(),
457                            is_staff: state.is_staff.clone(),
458                            app_version: state
459                                .app_metadata
460                                .app_version
461                                .map(|version| version.to_string()),
462                            os_name: state.app_metadata.os_name,
463                            os_version: state
464                                .app_metadata
465                                .os_version
466                                .map(|version| version.to_string()),
467                            architecture: state.architecture,
468
469                            release_channel: state.release_channel,
470                            events,
471                        };
472                        json_bytes.clear();
473                        serde_json::to_writer(&mut json_bytes, &request_body)?;
474                    }
475
476                    this.http_client
477                        .post_json(CLICKHOUSE_EVENTS_URL.as_str(), json_bytes.into())
478                        .await?;
479                    anyhow::Ok(())
480                }
481                .log_err(),
482            )
483            .detach();
484    }
485}