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