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    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    Setting {
115        setting: &'static str,
116        value: String,
117        milliseconds_since_first_event: i64,
118    },
119}
120
121#[cfg(debug_assertions)]
122const MAX_QUEUE_LEN: usize = 1;
123
124#[cfg(not(debug_assertions))]
125const MAX_QUEUE_LEN: usize = 50;
126
127impl Telemetry {
128    pub fn new(client: Arc<dyn HttpClient>, cx: &mut AppContext) -> Arc<Self> {
129        let release_channel = if cx.has_global::<ReleaseChannel>() {
130            Some(cx.global::<ReleaseChannel>().display_name())
131        } else {
132            None
133        };
134
135        // TODO: Replace all hardware stuff with nested SystemSpecs json
136        let this = Arc::new(Self {
137            http_client: client,
138            executor: cx.background_executor().clone(),
139            state: Mutex::new(TelemetryState {
140                app_metadata: cx.app_metadata(),
141                architecture: env::consts::ARCH,
142                release_channel,
143                installation_id: None,
144                metrics_id: None,
145                session_id: None,
146                clickhouse_events_queue: Default::default(),
147                log_file: None,
148                is_staff: None,
149                first_event_datetime: None,
150            }),
151        });
152
153        // We should only ever have one instance of Telemetry, leak the subscription to keep it alive
154        // rather than store in TelemetryState, complicating spawn as subscriptions are not Send
155        std::mem::forget(cx.on_app_quit({
156            let this = this.clone();
157            move |cx| this.shutdown_telemetry(cx)
158        }));
159
160        this
161    }
162
163    #[cfg(any(test, feature = "test-support"))]
164    fn shutdown_telemetry(self: &Arc<Self>, _: &mut AppContext) -> impl Future<Output = ()> {
165        Task::ready(())
166    }
167
168    // Skip calling this function in tests.
169    // TestAppContext ends up calling this function on shutdown and it panics when trying to find the TelemetrySettings
170    #[cfg(not(any(test, feature = "test-support")))]
171    fn shutdown_telemetry(self: &Arc<Self>, cx: &mut AppContext) -> impl Future<Output = ()> {
172        let telemetry_settings = TelemetrySettings::get_global(cx).clone();
173        self.report_app_event(telemetry_settings, "close", true);
174        Task::ready(())
175    }
176
177    pub fn log_file_path(&self) -> Option<PathBuf> {
178        Some(self.state.lock().log_file.as_ref()?.path().to_path_buf())
179    }
180
181    pub fn start(
182        self: &Arc<Self>,
183        installation_id: Option<String>,
184        session_id: String,
185        cx: &mut AppContext,
186    ) {
187        let mut state = self.state.lock();
188        state.installation_id = installation_id.map(|id| id.into());
189        state.session_id = Some(session_id.into());
190        drop(state);
191
192        let this = self.clone();
193        cx.spawn(|cx| async move {
194            // Avoiding calling `System::new_all()`, as there have been crashes related to it
195            let refresh_kind = RefreshKind::new()
196                .with_memory() // For memory usage
197                .with_processes(ProcessRefreshKind::everything()) // For process usage
198                .with_cpu(CpuRefreshKind::everything()); // For core count
199
200            let mut system = System::new_with_specifics(refresh_kind);
201
202            // Avoiding calling `refresh_all()`, just update what we need
203            system.refresh_specifics(refresh_kind);
204
205            // Waiting some amount of time before the first query is important to get a reasonable value
206            // https://docs.rs/sysinfo/0.29.10/sysinfo/trait.ProcessExt.html#tymethod.cpu_usage
207            const DURATION_BETWEEN_SYSTEM_EVENTS: Duration = Duration::from_secs(4 * 60);
208
209            loop {
210                smol::Timer::after(DURATION_BETWEEN_SYSTEM_EVENTS).await;
211
212                system.refresh_specifics(refresh_kind);
213
214                let current_process = Pid::from_u32(std::process::id());
215                let Some(process) = system.processes().get(&current_process) else {
216                    let process = current_process;
217                    log::error!("Failed to find own process {process:?} in system process table");
218                    // TODO: Fire an error telemetry event
219                    return;
220                };
221
222                let telemetry_settings = if let Ok(telemetry_settings) =
223                    cx.update(|cx| *TelemetrySettings::get_global(cx))
224                {
225                    telemetry_settings
226                } else {
227                    break;
228                };
229
230                this.report_memory_event(
231                    telemetry_settings,
232                    process.memory(),
233                    process.virtual_memory(),
234                );
235                this.report_cpu_event(
236                    telemetry_settings,
237                    process.cpu_usage(),
238                    system.cpus().len() as u32,
239                );
240            }
241        })
242        .detach();
243    }
244
245    pub fn set_authenticated_user_info(
246        self: &Arc<Self>,
247        metrics_id: Option<String>,
248        is_staff: bool,
249        cx: &AppContext,
250    ) {
251        if !TelemetrySettings::get_global(cx).metrics {
252            return;
253        }
254
255        let mut state = self.state.lock();
256        let metrics_id: Option<Arc<str>> = metrics_id.map(|id| id.into());
257        state.metrics_id = metrics_id.clone();
258        state.is_staff = Some(is_staff);
259        drop(state);
260    }
261
262    pub fn report_editor_event(
263        self: &Arc<Self>,
264        telemetry_settings: TelemetrySettings,
265        file_extension: Option<String>,
266        vim_mode: bool,
267        operation: &'static str,
268        copilot_enabled: bool,
269        copilot_enabled_for_language: bool,
270    ) {
271        let event = ClickhouseEvent::Editor {
272            file_extension,
273            vim_mode,
274            operation,
275            copilot_enabled,
276            copilot_enabled_for_language,
277            milliseconds_since_first_event: self.milliseconds_since_first_event(),
278        };
279
280        self.report_clickhouse_event(event, telemetry_settings, false)
281    }
282
283    pub fn report_copilot_event(
284        self: &Arc<Self>,
285        telemetry_settings: TelemetrySettings,
286        suggestion_id: Option<String>,
287        suggestion_accepted: bool,
288        file_extension: Option<String>,
289    ) {
290        let event = ClickhouseEvent::Copilot {
291            suggestion_id,
292            suggestion_accepted,
293            file_extension,
294            milliseconds_since_first_event: self.milliseconds_since_first_event(),
295        };
296
297        self.report_clickhouse_event(event, telemetry_settings, false)
298    }
299
300    pub fn report_assistant_event(
301        self: &Arc<Self>,
302        telemetry_settings: TelemetrySettings,
303        conversation_id: Option<String>,
304        kind: AssistantKind,
305        model: &'static str,
306    ) {
307        let event = ClickhouseEvent::Assistant {
308            conversation_id,
309            kind,
310            model,
311            milliseconds_since_first_event: self.milliseconds_since_first_event(),
312        };
313
314        self.report_clickhouse_event(event, telemetry_settings, false)
315    }
316
317    pub fn report_call_event(
318        self: &Arc<Self>,
319        telemetry_settings: TelemetrySettings,
320        operation: &'static str,
321        room_id: Option<u64>,
322        channel_id: Option<u64>,
323    ) {
324        let event = ClickhouseEvent::Call {
325            operation,
326            room_id,
327            channel_id,
328            milliseconds_since_first_event: self.milliseconds_since_first_event(),
329        };
330
331        self.report_clickhouse_event(event, telemetry_settings, false)
332    }
333
334    pub fn report_cpu_event(
335        self: &Arc<Self>,
336        telemetry_settings: TelemetrySettings,
337        usage_as_percentage: f32,
338        core_count: u32,
339    ) {
340        let event = ClickhouseEvent::Cpu {
341            usage_as_percentage,
342            core_count,
343            milliseconds_since_first_event: self.milliseconds_since_first_event(),
344        };
345
346        self.report_clickhouse_event(event, telemetry_settings, false)
347    }
348
349    pub fn report_memory_event(
350        self: &Arc<Self>,
351        telemetry_settings: TelemetrySettings,
352        memory_in_bytes: u64,
353        virtual_memory_in_bytes: u64,
354    ) {
355        let event = ClickhouseEvent::Memory {
356            memory_in_bytes,
357            virtual_memory_in_bytes,
358            milliseconds_since_first_event: self.milliseconds_since_first_event(),
359        };
360
361        self.report_clickhouse_event(event, telemetry_settings, false)
362    }
363
364    pub fn report_app_event(
365        self: &Arc<Self>,
366        telemetry_settings: TelemetrySettings,
367        operation: &'static str,
368        immediate_flush: bool,
369    ) {
370        let event = ClickhouseEvent::App {
371            operation,
372            milliseconds_since_first_event: self.milliseconds_since_first_event(),
373        };
374
375        self.report_clickhouse_event(event, telemetry_settings, immediate_flush)
376    }
377
378    pub fn report_setting_event(
379        self: &Arc<Self>,
380        telemetry_settings: TelemetrySettings,
381        setting: &'static str,
382        value: String,
383    ) {
384        let event = ClickhouseEvent::Setting {
385            setting,
386            value,
387            milliseconds_since_first_event: self.milliseconds_since_first_event(),
388        };
389
390        self.report_clickhouse_event(event, telemetry_settings, false)
391    }
392
393    fn milliseconds_since_first_event(&self) -> i64 {
394        let mut state = self.state.lock();
395        match state.first_event_datetime {
396            Some(first_event_datetime) => {
397                let now: DateTime<Utc> = Utc::now();
398                now.timestamp_millis() - first_event_datetime.timestamp_millis()
399            }
400            None => {
401                state.first_event_datetime = Some(Utc::now());
402                0
403            }
404        }
405    }
406
407    fn report_clickhouse_event(
408        self: &Arc<Self>,
409        event: ClickhouseEvent,
410        telemetry_settings: TelemetrySettings,
411        immediate_flush: bool,
412    ) {
413        if !telemetry_settings.metrics {
414            return;
415        }
416
417        let mut state = self.state.lock();
418        let signed_in = state.metrics_id.is_some();
419        state
420            .clickhouse_events_queue
421            .push(ClickhouseEventWrapper { signed_in, event });
422
423        if state.installation_id.is_some() {
424            if immediate_flush || state.clickhouse_events_queue.len() >= MAX_QUEUE_LEN {
425                drop(state);
426                self.flush_clickhouse_events();
427            }
428        }
429    }
430
431    pub fn metrics_id(self: &Arc<Self>) -> Option<Arc<str>> {
432        self.state.lock().metrics_id.clone()
433    }
434
435    pub fn installation_id(self: &Arc<Self>) -> Option<Arc<str>> {
436        self.state.lock().installation_id.clone()
437    }
438
439    pub fn is_staff(self: &Arc<Self>) -> Option<bool> {
440        self.state.lock().is_staff
441    }
442
443    fn flush_clickhouse_events(self: &Arc<Self>) {
444        let mut state = self.state.lock();
445        state.first_event_datetime = None;
446        let mut events = mem::take(&mut state.clickhouse_events_queue);
447        drop(state);
448
449        let this = self.clone();
450        self.executor
451            .spawn(
452                async move {
453                    let mut json_bytes = Vec::new();
454
455                    if let Some(file) = &mut this.state.lock().log_file {
456                        let file = file.as_file_mut();
457                        for event in &mut events {
458                            json_bytes.clear();
459                            serde_json::to_writer(&mut json_bytes, event)?;
460                            file.write_all(&json_bytes)?;
461                            file.write(b"\n")?;
462                        }
463                    }
464
465                    {
466                        let state = this.state.lock();
467                        let request_body = ClickhouseEventRequestBody {
468                            token: ZED_SECRET_CLIENT_TOKEN,
469                            installation_id: state.installation_id.clone(),
470                            session_id: state.session_id.clone(),
471                            is_staff: state.is_staff.clone(),
472                            app_version: state
473                                .app_metadata
474                                .app_version
475                                .map(|version| version.to_string()),
476                            os_name: state.app_metadata.os_name,
477                            os_version: state
478                                .app_metadata
479                                .os_version
480                                .map(|version| version.to_string()),
481                            architecture: state.architecture,
482
483                            release_channel: state.release_channel,
484                            events,
485                        };
486                        json_bytes.clear();
487                        serde_json::to_writer(&mut json_bytes, &request_body)?;
488                    }
489
490                    this.http_client
491                        .post_json(CLICKHOUSE_EVENTS_URL.as_str(), json_bytes.into())
492                        .await?;
493                    anyhow::Ok(())
494                }
495                .log_err(),
496            )
497            .detach();
498    }
499}