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