telemetry.rs

  1use crate::{TelemetrySettings, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL};
  2use chrono::{DateTime, Utc};
  3use gpui::{executor::Background, serde_json, AppContext, Task};
  4use lazy_static::lazy_static;
  5use parking_lot::Mutex;
  6use serde::Serialize;
  7use std::{env, io::Write, mem, path::PathBuf, sync::Arc, time::Duration};
  8use sysinfo::{
  9    CpuRefreshKind, Pid, PidExt, ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt,
 10};
 11use tempfile::NamedTempFile;
 12use util::http::HttpClient;
 13use util::{channel::ReleaseChannel, TryFutureExt};
 14
 15pub struct Telemetry {
 16    http_client: Arc<dyn HttpClient>,
 17    executor: Arc<Background>,
 18    state: Mutex<TelemetryState>,
 19}
 20
 21#[derive(Default)]
 22struct TelemetryState {
 23    metrics_id: Option<Arc<str>>,      // Per logged-in user
 24    installation_id: Option<Arc<str>>, // Per app installation (different for dev, nightly, preview, and stable)
 25    session_id: Option<Arc<str>>,      // Per app launch
 26    app_version: Option<Arc<str>>,
 27    release_channel: Option<&'static str>,
 28    os_name: &'static str,
 29    os_version: Option<Arc<str>>,
 30    architecture: &'static str,
 31    clickhouse_events_queue: Vec<ClickhouseEventWrapper>,
 32    flush_clickhouse_events_task: Option<Task<()>>,
 33    log_file: Option<NamedTempFile>,
 34    is_staff: Option<bool>,
 35    first_event_datetime: Option<DateTime<Utc>>,
 36}
 37
 38const CLICKHOUSE_EVENTS_URL_PATH: &'static str = "/api/events";
 39
 40lazy_static! {
 41    static ref CLICKHOUSE_EVENTS_URL: String =
 42        format!("{}{}", *ZED_SERVER_URL, CLICKHOUSE_EVENTS_URL_PATH);
 43}
 44
 45#[derive(Serialize, Debug)]
 46struct ClickhouseEventRequestBody {
 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<Arc<str>>,
 52    os_name: &'static str,
 53    os_version: Option<Arc<str>>,
 54    architecture: &'static str,
 55    release_channel: Option<&'static str>,
 56    events: Vec<ClickhouseEventWrapper>,
 57}
 58
 59#[derive(Serialize, Debug)]
 60struct ClickhouseEventWrapper {
 61    signed_in: bool,
 62    #[serde(flatten)]
 63    event: ClickhouseEvent,
 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 ClickhouseEvent {
 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}
113
114#[cfg(debug_assertions)]
115const MAX_QUEUE_LEN: usize = 1;
116
117#[cfg(not(debug_assertions))]
118const MAX_QUEUE_LEN: usize = 10;
119
120#[cfg(debug_assertions)]
121const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(1);
122
123#[cfg(not(debug_assertions))]
124const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(30);
125
126impl Telemetry {
127    pub fn new(client: Arc<dyn HttpClient>, cx: &AppContext) -> Arc<Self> {
128        let platform = cx.platform();
129        let release_channel = if cx.has_global::<ReleaseChannel>() {
130            Some(cx.global::<ReleaseChannel>().display_name())
131        } else {
132            None
133        };
134        // TODO: Replace all hardware stuff with nested SystemSpecs json
135        let this = Arc::new(Self {
136            http_client: client,
137            executor: cx.background().clone(),
138            state: Mutex::new(TelemetryState {
139                os_name: platform.os_name().into(),
140                os_version: platform.os_version().ok().map(|v| v.to_string().into()),
141                architecture: env::consts::ARCH,
142                app_version: platform.app_version().ok().map(|v| v.to_string().into()),
143                release_channel,
144                installation_id: None,
145                metrics_id: None,
146                session_id: None,
147                clickhouse_events_queue: Default::default(),
148                flush_clickhouse_events_task: Default::default(),
149                log_file: None,
150                is_staff: None,
151                first_event_datetime: None,
152            }),
153        });
154
155        this
156    }
157
158    pub fn log_file_path(&self) -> Option<PathBuf> {
159        Some(self.state.lock().log_file.as_ref()?.path().to_path_buf())
160    }
161
162    pub fn start(
163        self: &Arc<Self>,
164        installation_id: Option<String>,
165        session_id: String,
166        cx: &mut AppContext,
167    ) {
168        let mut state = self.state.lock();
169        state.installation_id = installation_id.map(|id| id.into());
170        state.session_id = Some(session_id.into());
171        let has_clickhouse_events = !state.clickhouse_events_queue.is_empty();
172        drop(state);
173
174        if has_clickhouse_events {
175            self.flush_clickhouse_events();
176        }
177
178        let this = self.clone();
179        cx.spawn(|mut cx| async move {
180            // Avoiding calling `System::new_all()`, as there have been crashes related to it
181            let refresh_kind = RefreshKind::new()
182                .with_memory() // For memory usage
183                .with_processes(ProcessRefreshKind::everything()) // For process usage
184                .with_cpu(CpuRefreshKind::everything()); // For core count
185
186            let mut system = System::new_with_specifics(refresh_kind);
187
188            // Avoiding calling `refresh_all()`, just update what we need
189            system.refresh_specifics(refresh_kind);
190
191            loop {
192                // Waiting some amount of time before the first query is important to get a reasonable value
193                // https://docs.rs/sysinfo/0.29.10/sysinfo/trait.ProcessExt.html#tymethod.cpu_usage
194                const DURATION_BETWEEN_SYSTEM_EVENTS: Duration = Duration::from_secs(60);
195                smol::Timer::after(DURATION_BETWEEN_SYSTEM_EVENTS).await;
196
197                system.refresh_specifics(refresh_kind);
198
199                let current_process = Pid::from_u32(std::process::id());
200                let Some(process) = system.processes().get(&current_process) else {
201                    let process = current_process;
202                    log::error!("Failed to find own process {process:?} in system process table");
203                    // TODO: Fire an error telemetry event
204                    return;
205                };
206
207                let telemetry_settings = cx.update(|cx| *settings::get::<TelemetrySettings>(cx));
208
209                this.report_memory_event(
210                    telemetry_settings,
211                    process.memory(),
212                    process.virtual_memory(),
213                );
214                this.report_cpu_event(
215                    telemetry_settings,
216                    process.cpu_usage(),
217                    system.cpus().len() as u32,
218                );
219            }
220        })
221        .detach();
222    }
223
224    pub fn set_authenticated_user_info(
225        self: &Arc<Self>,
226        metrics_id: Option<String>,
227        is_staff: bool,
228        cx: &AppContext,
229    ) {
230        if !settings::get::<TelemetrySettings>(cx).metrics {
231            return;
232        }
233
234        let mut state = self.state.lock();
235        let metrics_id: Option<Arc<str>> = metrics_id.map(|id| id.into());
236        state.metrics_id = metrics_id.clone();
237        state.is_staff = Some(is_staff);
238        drop(state);
239    }
240
241    pub fn report_editor_event(
242        self: &Arc<Self>,
243        telemetry_settings: TelemetrySettings,
244        file_extension: Option<String>,
245        vim_mode: bool,
246        operation: &'static str,
247        copilot_enabled: bool,
248        copilot_enabled_for_language: bool,
249    ) {
250        let event = ClickhouseEvent::Editor {
251            file_extension,
252            vim_mode,
253            operation,
254            copilot_enabled,
255            copilot_enabled_for_language,
256            milliseconds_since_first_event: self.milliseconds_since_first_event(),
257        };
258
259        self.report_clickhouse_event(event, telemetry_settings)
260    }
261
262    pub fn report_copilot_event(
263        self: &Arc<Self>,
264        telemetry_settings: TelemetrySettings,
265        suggestion_id: Option<String>,
266        suggestion_accepted: bool,
267        file_extension: Option<String>,
268    ) {
269        let event = ClickhouseEvent::Copilot {
270            suggestion_id,
271            suggestion_accepted,
272            file_extension,
273            milliseconds_since_first_event: self.milliseconds_since_first_event(),
274        };
275
276        self.report_clickhouse_event(event, telemetry_settings)
277    }
278
279    pub fn report_assistant_event(
280        self: &Arc<Self>,
281        telemetry_settings: TelemetrySettings,
282        conversation_id: Option<String>,
283        kind: AssistantKind,
284        model: &'static str,
285    ) {
286        let event = ClickhouseEvent::Assistant {
287            conversation_id,
288            kind,
289            model,
290            milliseconds_since_first_event: self.milliseconds_since_first_event(),
291        };
292
293        self.report_clickhouse_event(event, telemetry_settings)
294    }
295
296    pub fn report_call_event(
297        self: &Arc<Self>,
298        telemetry_settings: TelemetrySettings,
299        operation: &'static str,
300        room_id: Option<u64>,
301        channel_id: Option<u64>,
302    ) {
303        let event = ClickhouseEvent::Call {
304            operation,
305            room_id,
306            channel_id,
307            milliseconds_since_first_event: self.milliseconds_since_first_event(),
308        };
309
310        self.report_clickhouse_event(event, telemetry_settings)
311    }
312
313    pub fn report_cpu_event(
314        self: &Arc<Self>,
315        telemetry_settings: TelemetrySettings,
316        usage_as_percentage: f32,
317        core_count: u32,
318    ) {
319        let event = ClickhouseEvent::Cpu {
320            usage_as_percentage,
321            core_count,
322            milliseconds_since_first_event: self.milliseconds_since_first_event(),
323        };
324
325        self.report_clickhouse_event(event, telemetry_settings)
326    }
327
328    pub fn report_memory_event(
329        self: &Arc<Self>,
330        telemetry_settings: TelemetrySettings,
331        memory_in_bytes: u64,
332        virtual_memory_in_bytes: u64,
333    ) {
334        let event = ClickhouseEvent::Memory {
335            memory_in_bytes,
336            virtual_memory_in_bytes,
337            milliseconds_since_first_event: self.milliseconds_since_first_event(),
338        };
339
340        self.report_clickhouse_event(event, telemetry_settings)
341    }
342
343    fn milliseconds_since_first_event(&self) -> i64 {
344        let mut state = self.state.lock();
345        match state.first_event_datetime {
346            Some(first_event_datetime) => {
347                let now: DateTime<Utc> = Utc::now();
348                now.timestamp_millis() - first_event_datetime.timestamp_millis()
349            }
350            None => {
351                state.first_event_datetime = Some(Utc::now());
352                0
353            }
354        }
355    }
356
357    fn report_clickhouse_event(
358        self: &Arc<Self>,
359        event: ClickhouseEvent,
360        telemetry_settings: TelemetrySettings,
361    ) {
362        if !telemetry_settings.metrics {
363            return;
364        }
365
366        let mut state = self.state.lock();
367        let signed_in = state.metrics_id.is_some();
368        state
369            .clickhouse_events_queue
370            .push(ClickhouseEventWrapper { signed_in, event });
371
372        if state.installation_id.is_some() {
373            if state.clickhouse_events_queue.len() >= MAX_QUEUE_LEN {
374                drop(state);
375                self.flush_clickhouse_events();
376            } else {
377                let this = self.clone();
378                let executor = self.executor.clone();
379                state.flush_clickhouse_events_task = Some(self.executor.spawn(async move {
380                    executor.timer(DEBOUNCE_INTERVAL).await;
381                    this.flush_clickhouse_events();
382                }));
383            }
384        }
385    }
386
387    pub fn metrics_id(self: &Arc<Self>) -> Option<Arc<str>> {
388        self.state.lock().metrics_id.clone()
389    }
390
391    pub fn installation_id(self: &Arc<Self>) -> Option<Arc<str>> {
392        self.state.lock().installation_id.clone()
393    }
394
395    pub fn is_staff(self: &Arc<Self>) -> Option<bool> {
396        self.state.lock().is_staff
397    }
398
399    fn flush_clickhouse_events(self: &Arc<Self>) {
400        let mut state = self.state.lock();
401        state.first_event_datetime = None;
402        let mut events = mem::take(&mut state.clickhouse_events_queue);
403        state.flush_clickhouse_events_task.take();
404        drop(state);
405
406        let this = self.clone();
407        self.executor
408            .spawn(
409                async move {
410                    let mut json_bytes = Vec::new();
411
412                    if let Some(file) = &mut this.state.lock().log_file {
413                        let file = file.as_file_mut();
414                        for event in &mut events {
415                            json_bytes.clear();
416                            serde_json::to_writer(&mut json_bytes, event)?;
417                            file.write_all(&json_bytes)?;
418                            file.write(b"\n")?;
419                        }
420                    }
421
422                    {
423                        let state = this.state.lock();
424                        let request_body = ClickhouseEventRequestBody {
425                            token: ZED_SECRET_CLIENT_TOKEN,
426                            installation_id: state.installation_id.clone(),
427                            session_id: state.session_id.clone(),
428                            is_staff: state.is_staff.clone(),
429                            app_version: state.app_version.clone(),
430                            os_name: state.os_name,
431                            os_version: state.os_version.clone(),
432                            architecture: state.architecture,
433
434                            release_channel: state.release_channel,
435                            events,
436                        };
437                        json_bytes.clear();
438                        serde_json::to_writer(&mut json_bytes, &request_body)?;
439                    }
440
441                    this.http_client
442                        .post_json(CLICKHOUSE_EVENTS_URL.as_str(), json_bytes.into())
443                        .await?;
444                    anyhow::Ok(())
445                }
446                .log_err(),
447            )
448            .detach();
449    }
450}