telemetry.rs

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