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            // Waiting some amount of time before the first query is important to get a reasonable value
209            // https://docs.rs/sysinfo/0.29.10/sysinfo/trait.ProcessExt.html#tymethod.cpu_usage
210            const DURATION_BETWEEN_SYSTEM_EVENTS: Duration = Duration::from_secs(4 * 60);
211
212            loop {
213                smol::Timer::after(DURATION_BETWEEN_SYSTEM_EVENTS).await;
214
215                system.refresh_specifics(refresh_kind);
216
217                let current_process = Pid::from_u32(std::process::id());
218                let Some(process) = system.processes().get(&current_process) else {
219                    let process = current_process;
220                    log::error!("Failed to find own process {process:?} in system process table");
221                    // TODO: Fire an error telemetry event
222                    return;
223                };
224
225                let telemetry_settings = if let Ok(telemetry_settings) =
226                    cx.update(|cx| *TelemetrySettings::get_global(cx))
227                {
228                    telemetry_settings
229                } else {
230                    break;
231                };
232
233                this.report_memory_event(
234                    telemetry_settings,
235                    process.memory(),
236                    process.virtual_memory(),
237                );
238                this.report_cpu_event(
239                    telemetry_settings,
240                    process.cpu_usage(),
241                    system.cpus().len() as u32,
242                );
243            }
244        })
245        .detach();
246    }
247
248    pub fn set_authenticated_user_info(
249        self: &Arc<Self>,
250        metrics_id: Option<String>,
251        is_staff: bool,
252        cx: &AppContext,
253    ) {
254        if !TelemetrySettings::get_global(cx).metrics {
255            return;
256        }
257
258        let mut state = self.state.lock();
259        let metrics_id: Option<Arc<str>> = metrics_id.map(|id| id.into());
260        state.metrics_id = metrics_id.clone();
261        state.is_staff = Some(is_staff);
262        drop(state);
263    }
264
265    pub fn report_editor_event(
266        self: &Arc<Self>,
267        telemetry_settings: TelemetrySettings,
268        file_extension: Option<String>,
269        vim_mode: bool,
270        operation: &'static str,
271        copilot_enabled: bool,
272        copilot_enabled_for_language: bool,
273    ) {
274        let event = ClickhouseEvent::Editor {
275            file_extension,
276            vim_mode,
277            operation,
278            copilot_enabled,
279            copilot_enabled_for_language,
280            milliseconds_since_first_event: self.milliseconds_since_first_event(),
281        };
282
283        self.report_clickhouse_event(event, telemetry_settings, false)
284    }
285
286    pub fn report_copilot_event(
287        self: &Arc<Self>,
288        telemetry_settings: TelemetrySettings,
289        suggestion_id: Option<String>,
290        suggestion_accepted: bool,
291        file_extension: Option<String>,
292    ) {
293        let event = ClickhouseEvent::Copilot {
294            suggestion_id,
295            suggestion_accepted,
296            file_extension,
297            milliseconds_since_first_event: self.milliseconds_since_first_event(),
298        };
299
300        self.report_clickhouse_event(event, telemetry_settings, false)
301    }
302
303    pub fn report_assistant_event(
304        self: &Arc<Self>,
305        telemetry_settings: TelemetrySettings,
306        conversation_id: Option<String>,
307        kind: AssistantKind,
308        model: &'static str,
309    ) {
310        let event = ClickhouseEvent::Assistant {
311            conversation_id,
312            kind,
313            model,
314            milliseconds_since_first_event: self.milliseconds_since_first_event(),
315        };
316
317        self.report_clickhouse_event(event, telemetry_settings, false)
318    }
319
320    pub fn report_call_event(
321        self: &Arc<Self>,
322        telemetry_settings: TelemetrySettings,
323        operation: &'static str,
324        room_id: Option<u64>,
325        channel_id: Option<u64>,
326    ) {
327        let event = ClickhouseEvent::Call {
328            operation,
329            room_id,
330            channel_id,
331            milliseconds_since_first_event: self.milliseconds_since_first_event(),
332        };
333
334        self.report_clickhouse_event(event, telemetry_settings, false)
335    }
336
337    pub fn report_cpu_event(
338        self: &Arc<Self>,
339        telemetry_settings: TelemetrySettings,
340        usage_as_percentage: f32,
341        core_count: u32,
342    ) {
343        let event = ClickhouseEvent::Cpu {
344            usage_as_percentage,
345            core_count,
346            milliseconds_since_first_event: self.milliseconds_since_first_event(),
347        };
348
349        self.report_clickhouse_event(event, telemetry_settings, false)
350    }
351
352    pub fn report_memory_event(
353        self: &Arc<Self>,
354        telemetry_settings: TelemetrySettings,
355        memory_in_bytes: u64,
356        virtual_memory_in_bytes: u64,
357    ) {
358        let event = ClickhouseEvent::Memory {
359            memory_in_bytes,
360            virtual_memory_in_bytes,
361            milliseconds_since_first_event: self.milliseconds_since_first_event(),
362        };
363
364        self.report_clickhouse_event(event, telemetry_settings, false)
365    }
366
367    // app_events are called at app open and app close, so flush is set to immediately send
368    pub fn report_app_event(
369        self: &Arc<Self>,
370        telemetry_settings: TelemetrySettings,
371        operation: &'static str,
372    ) {
373        let event = ClickhouseEvent::App {
374            operation,
375            milliseconds_since_first_event: self.milliseconds_since_first_event(),
376        };
377
378        self.report_clickhouse_event(event, telemetry_settings, true)
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_clickhouse_event(
396        self: &Arc<Self>,
397        event: ClickhouseEvent,
398        telemetry_settings: TelemetrySettings,
399        immediate_flush: bool,
400    ) {
401        if !telemetry_settings.metrics {
402            return;
403        }
404
405        let mut state = self.state.lock();
406        let signed_in = state.metrics_id.is_some();
407        state
408            .clickhouse_events_queue
409            .push(ClickhouseEventWrapper { signed_in, event });
410
411        if state.installation_id.is_some() {
412            if immediate_flush || state.clickhouse_events_queue.len() >= MAX_QUEUE_LEN {
413                drop(state);
414                self.flush_clickhouse_events();
415            } else {
416                let this = self.clone();
417                let executor = self.executor.clone();
418                state.flush_clickhouse_events_task = Some(self.executor.spawn(async move {
419                    executor.timer(DEBOUNCE_INTERVAL).await;
420                    this.flush_clickhouse_events();
421                }));
422            }
423        }
424    }
425
426    pub fn metrics_id(self: &Arc<Self>) -> Option<Arc<str>> {
427        self.state.lock().metrics_id.clone()
428    }
429
430    pub fn installation_id(self: &Arc<Self>) -> Option<Arc<str>> {
431        self.state.lock().installation_id.clone()
432    }
433
434    pub fn is_staff(self: &Arc<Self>) -> Option<bool> {
435        self.state.lock().is_staff
436    }
437
438    fn flush_clickhouse_events(self: &Arc<Self>) {
439        let mut state = self.state.lock();
440        state.first_event_datetime = None;
441        let mut events = mem::take(&mut state.clickhouse_events_queue);
442        state.flush_clickhouse_events_task.take();
443        drop(state);
444
445        let this = self.clone();
446        self.executor
447            .spawn(
448                async move {
449                    let mut json_bytes = Vec::new();
450
451                    if let Some(file) = &mut this.state.lock().log_file {
452                        let file = file.as_file_mut();
453                        for event in &mut events {
454                            json_bytes.clear();
455                            serde_json::to_writer(&mut json_bytes, event)?;
456                            file.write_all(&json_bytes)?;
457                            file.write(b"\n")?;
458                        }
459                    }
460
461                    {
462                        let state = this.state.lock();
463                        let request_body = ClickhouseEventRequestBody {
464                            token: ZED_SECRET_CLIENT_TOKEN,
465                            installation_id: state.installation_id.clone(),
466                            session_id: state.session_id.clone(),
467                            is_staff: state.is_staff.clone(),
468                            app_version: state
469                                .app_metadata
470                                .app_version
471                                .map(|version| version.to_string()),
472                            os_name: state.app_metadata.os_name,
473                            os_version: state
474                                .app_metadata
475                                .os_version
476                                .map(|version| version.to_string()),
477                            architecture: state.architecture,
478
479                            release_channel: state.release_channel,
480                            events,
481                        };
482                        json_bytes.clear();
483                        serde_json::to_writer(&mut json_bytes, &request_body)?;
484                    }
485
486                    this.http_client
487                        .post_json(CLICKHOUSE_EVENTS_URL.as_str(), json_bytes.into())
488                        .await?;
489                    anyhow::Ok(())
490                }
491                .log_err(),
492            )
493            .detach();
494    }
495}