telemetry.rs

  1use crate::{TelemetrySettings, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL};
  2use gpui::{executor::Background, serde_json, AppContext, Task};
  3use lazy_static::lazy_static;
  4use parking_lot::Mutex;
  5use serde::Serialize;
  6use std::{env, io::Write, mem, path::PathBuf, sync::Arc, time::Duration};
  7use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};
  8use tempfile::NamedTempFile;
  9use util::http::HttpClient;
 10use util::{channel::ReleaseChannel, TryFutureExt};
 11use uuid::Uuid;
 12
 13pub struct Telemetry {
 14    http_client: Arc<dyn HttpClient>,
 15    executor: Arc<Background>,
 16    state: Mutex<TelemetryState>,
 17}
 18
 19#[derive(Default)]
 20struct TelemetryState {
 21    metrics_id: Option<Arc<str>>,      // Per logged-in user
 22    installation_id: Option<Arc<str>>, // Per app installation (different for dev, preview, and stable)
 23    session_id: String,                // Per app launch
 24    app_version: Option<Arc<str>>,
 25    release_channel: Option<&'static str>,
 26    os_name: &'static str,
 27    os_version: Option<Arc<str>>,
 28    architecture: &'static str,
 29    clickhouse_events_queue: Vec<ClickhouseEventWrapper>,
 30    flush_clickhouse_events_task: Option<Task<()>>,
 31    log_file: Option<NamedTempFile>,
 32    is_staff: Option<bool>,
 33}
 34
 35const CLICKHOUSE_EVENTS_URL_PATH: &'static str = "/api/events";
 36
 37lazy_static! {
 38    static ref CLICKHOUSE_EVENTS_URL: String =
 39        format!("{}{}", *ZED_SERVER_URL, CLICKHOUSE_EVENTS_URL_PATH);
 40}
 41
 42#[derive(Serialize, Debug)]
 43struct ClickhouseEventRequestBody {
 44    token: &'static str,
 45    installation_id: Option<Arc<str>>,
 46    session_id: String,
 47    is_staff: Option<bool>,
 48    app_version: Option<Arc<str>>,
 49    os_name: &'static str,
 50    os_version: Option<Arc<str>>,
 51    architecture: &'static str,
 52    release_channel: Option<&'static str>,
 53    events: Vec<ClickhouseEventWrapper>,
 54}
 55
 56#[derive(Serialize, Debug)]
 57struct ClickhouseEventWrapper {
 58    signed_in: bool,
 59    #[serde(flatten)]
 60    event: ClickhouseEvent,
 61}
 62
 63#[derive(Serialize, Debug)]
 64#[serde(rename_all = "snake_case")]
 65pub enum AssistantKind {
 66    Panel,
 67    Inline,
 68}
 69
 70#[derive(Serialize, Debug)]
 71#[serde(tag = "type")]
 72pub enum ClickhouseEvent {
 73    Editor {
 74        operation: &'static str,
 75        file_extension: Option<String>,
 76        vim_mode: bool,
 77        copilot_enabled: bool,
 78        copilot_enabled_for_language: bool,
 79    },
 80    Copilot {
 81        suggestion_id: Option<String>,
 82        suggestion_accepted: bool,
 83        file_extension: Option<String>,
 84    },
 85    Call {
 86        operation: &'static str,
 87        room_id: Option<u64>,
 88        channel_id: Option<u64>,
 89    },
 90    Assistant {
 91        conversation_id: Option<String>,
 92        kind: AssistantKind,
 93        model: &'static str,
 94    },
 95    Cpu {
 96        usage_as_percentage: f32,
 97        core_count: u32,
 98    },
 99    Memory {
100        memory_in_bytes: u64,
101        virtual_memory_in_bytes: u64,
102    },
103}
104
105#[cfg(debug_assertions)]
106const MAX_QUEUE_LEN: usize = 1;
107
108#[cfg(not(debug_assertions))]
109const MAX_QUEUE_LEN: usize = 10;
110
111#[cfg(debug_assertions)]
112const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(1);
113
114#[cfg(not(debug_assertions))]
115const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(30);
116
117impl Telemetry {
118    pub fn new(client: Arc<dyn HttpClient>, cx: &AppContext) -> Arc<Self> {
119        let platform = cx.platform();
120        let release_channel = if cx.has_global::<ReleaseChannel>() {
121            Some(cx.global::<ReleaseChannel>().display_name())
122        } else {
123            None
124        };
125        // TODO: Replace all hardware stuff with nested SystemSpecs json
126        let this = Arc::new(Self {
127            http_client: client,
128            executor: cx.background().clone(),
129            state: Mutex::new(TelemetryState {
130                os_name: platform.os_name().into(),
131                os_version: platform.os_version().ok().map(|v| v.to_string().into()),
132                architecture: env::consts::ARCH,
133                app_version: platform.app_version().ok().map(|v| v.to_string().into()),
134                release_channel,
135                installation_id: None,
136                metrics_id: None,
137                session_id: Uuid::new_v4().to_string(),
138                clickhouse_events_queue: Default::default(),
139                flush_clickhouse_events_task: Default::default(),
140                log_file: None,
141                is_staff: None,
142            }),
143        });
144
145        this
146    }
147
148    pub fn log_file_path(&self) -> Option<PathBuf> {
149        Some(self.state.lock().log_file.as_ref()?.path().to_path_buf())
150    }
151
152    pub fn start(self: &Arc<Self>, installation_id: Option<String>, cx: &mut AppContext) {
153        let mut state = self.state.lock();
154        state.installation_id = installation_id.map(|id| id.into());
155        let has_clickhouse_events = !state.clickhouse_events_queue.is_empty();
156        drop(state);
157
158        if has_clickhouse_events {
159            self.flush_clickhouse_events();
160        }
161
162        let this = self.clone();
163        cx.spawn(|mut cx| async move {
164            let mut system = System::new_all();
165            system.refresh_all();
166
167            loop {
168                // Waiting some amount of time before the first query is important to get a reasonable value
169                // https://docs.rs/sysinfo/0.29.10/sysinfo/trait.ProcessExt.html#tymethod.cpu_usage
170                const DURATION_BETWEEN_SYSTEM_EVENTS: Duration = Duration::from_secs(60);
171                smol::Timer::after(DURATION_BETWEEN_SYSTEM_EVENTS).await;
172
173                system.refresh_memory();
174                system.refresh_processes();
175
176                let current_process = Pid::from_u32(std::process::id());
177                let Some(process) = system.processes().get(&current_process) else {
178                    let process = current_process;
179                    log::error!("Failed to find own process {process:?} in system process table");
180                    // TODO: Fire an error telemetry event
181                    return;
182                };
183
184                let memory_event = ClickhouseEvent::Memory {
185                    memory_in_bytes: process.memory(),
186                    virtual_memory_in_bytes: process.virtual_memory(),
187                };
188
189                let cpu_event = ClickhouseEvent::Cpu {
190                    usage_as_percentage: process.cpu_usage(),
191                    core_count: system.cpus().len() as u32,
192                };
193
194                let telemetry_settings = cx.update(|cx| *settings::get::<TelemetrySettings>(cx));
195
196                this.report_clickhouse_event(memory_event, telemetry_settings);
197                this.report_clickhouse_event(cpu_event, telemetry_settings);
198            }
199        })
200        .detach();
201    }
202
203    pub fn set_authenticated_user_info(
204        self: &Arc<Self>,
205        metrics_id: Option<String>,
206        is_staff: bool,
207        cx: &AppContext,
208    ) {
209        if !settings::get::<TelemetrySettings>(cx).metrics {
210            return;
211        }
212
213        let mut state = self.state.lock();
214        let metrics_id: Option<Arc<str>> = metrics_id.map(|id| id.into());
215        state.metrics_id = metrics_id.clone();
216        state.is_staff = Some(is_staff);
217        drop(state);
218    }
219
220    pub fn report_clickhouse_event(
221        self: &Arc<Self>,
222        event: ClickhouseEvent,
223        telemetry_settings: TelemetrySettings,
224    ) {
225        if !telemetry_settings.metrics {
226            return;
227        }
228
229        let mut state = self.state.lock();
230        let signed_in = state.metrics_id.is_some();
231        state
232            .clickhouse_events_queue
233            .push(ClickhouseEventWrapper { signed_in, event });
234
235        if state.installation_id.is_some() {
236            if state.clickhouse_events_queue.len() >= MAX_QUEUE_LEN {
237                drop(state);
238                self.flush_clickhouse_events();
239            } else {
240                let this = self.clone();
241                let executor = self.executor.clone();
242                state.flush_clickhouse_events_task = Some(self.executor.spawn(async move {
243                    executor.timer(DEBOUNCE_INTERVAL).await;
244                    this.flush_clickhouse_events();
245                }));
246            }
247        }
248    }
249
250    pub fn metrics_id(self: &Arc<Self>) -> Option<Arc<str>> {
251        self.state.lock().metrics_id.clone()
252    }
253
254    pub fn installation_id(self: &Arc<Self>) -> Option<Arc<str>> {
255        self.state.lock().installation_id.clone()
256    }
257
258    pub fn is_staff(self: &Arc<Self>) -> Option<bool> {
259        self.state.lock().is_staff
260    }
261
262    fn flush_clickhouse_events(self: &Arc<Self>) {
263        let mut state = self.state.lock();
264        let mut events = mem::take(&mut state.clickhouse_events_queue);
265        state.flush_clickhouse_events_task.take();
266        drop(state);
267
268        let this = self.clone();
269        self.executor
270            .spawn(
271                async move {
272                    let mut json_bytes = Vec::new();
273
274                    if let Some(file) = &mut this.state.lock().log_file {
275                        let file = file.as_file_mut();
276                        for event in &mut events {
277                            json_bytes.clear();
278                            serde_json::to_writer(&mut json_bytes, event)?;
279                            file.write_all(&json_bytes)?;
280                            file.write(b"\n")?;
281                        }
282                    }
283
284                    {
285                        let state = this.state.lock();
286                        json_bytes.clear();
287                        serde_json::to_writer(
288                            &mut json_bytes,
289                            &ClickhouseEventRequestBody {
290                                token: ZED_SECRET_CLIENT_TOKEN,
291                                installation_id: state.installation_id.clone(),
292                                session_id: state.session_id.clone(),
293                                is_staff: state.is_staff.clone(),
294                                app_version: state.app_version.clone(),
295                                os_name: state.os_name,
296                                os_version: state.os_version.clone(),
297                                architecture: state.architecture,
298
299                                release_channel: state.release_channel,
300                                events,
301                            },
302                        )?;
303                    }
304
305                    this.http_client
306                        .post_json(CLICKHOUSE_EVENTS_URL.as_str(), json_bytes.into())
307                        .await?;
308                    anyhow::Ok(())
309                }
310                .log_err(),
311            )
312            .detach();
313    }
314}