telemetry.rs

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