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};
 11
 12pub struct Telemetry {
 13    http_client: Arc<dyn HttpClient>,
 14    executor: Arc<Background>,
 15    state: Mutex<TelemetryState>,
 16}
 17
 18#[derive(Default)]
 19struct TelemetryState {
 20    metrics_id: Option<Arc<str>>,      // Per logged-in user
 21    installation_id: Option<Arc<str>>, // Per app installation
 22    app_version: Option<Arc<str>>,
 23    release_channel: Option<&'static str>,
 24    os_name: &'static str,
 25    os_version: Option<Arc<str>>,
 26    architecture: &'static str,
 27    clickhouse_events_queue: Vec<ClickhouseEventWrapper>,
 28    flush_clickhouse_events_task: Option<Task<()>>,
 29    log_file: Option<NamedTempFile>,
 30    is_staff: Option<bool>,
 31}
 32
 33const CLICKHOUSE_EVENTS_URL_PATH: &'static str = "/api/events";
 34
 35lazy_static! {
 36    static ref CLICKHOUSE_EVENTS_URL: String =
 37        format!("{}{}", *ZED_SERVER_URL, CLICKHOUSE_EVENTS_URL_PATH);
 38}
 39
 40#[derive(Serialize, Debug)]
 41struct ClickhouseEventRequestBody {
 42    token: &'static str,
 43    installation_id: Option<Arc<str>>,
 44    is_staff: Option<bool>,
 45    app_version: Option<Arc<str>>,
 46    os_name: &'static str,
 47    os_version: Option<Arc<str>>,
 48    architecture: &'static str,
 49    release_channel: Option<&'static str>,
 50    events: Vec<ClickhouseEventWrapper>,
 51}
 52
 53#[derive(Serialize, Debug)]
 54struct ClickhouseEventWrapper {
 55    signed_in: bool,
 56    #[serde(flatten)]
 57    event: ClickhouseEvent,
 58}
 59
 60#[derive(Serialize, Debug)]
 61#[serde(rename_all = "snake_case")]
 62pub enum AssistantKind {
 63    Panel,
 64    Inline,
 65}
 66
 67#[derive(Serialize, Debug)]
 68#[serde(tag = "type")]
 69pub enum ClickhouseEvent {
 70    Editor {
 71        operation: &'static str,
 72        file_extension: Option<String>,
 73        vim_mode: bool,
 74        copilot_enabled: bool,
 75        copilot_enabled_for_language: bool,
 76    },
 77    Copilot {
 78        suggestion_id: Option<String>,
 79        suggestion_accepted: bool,
 80        file_extension: Option<String>,
 81    },
 82    Call {
 83        operation: &'static str,
 84        room_id: Option<u64>,
 85        channel_id: Option<u64>,
 86    },
 87    Assistant {
 88        conversation_id: Option<String>,
 89        kind: AssistantKind,
 90        model: &'static str,
 91    },
 92    Cpu {
 93        usage_as_percent: f32,
 94        core_count: u32,
 95    },
 96    Memory {
 97        memory_in_bytes: u64,
 98        virtual_memory_in_bytes: u64,
 99        start_time_in_seconds: u64,
100        run_time_in_seconds: u64,
101    },
102}
103
104#[cfg(debug_assertions)]
105const MAX_QUEUE_LEN: usize = 1;
106
107#[cfg(not(debug_assertions))]
108const MAX_QUEUE_LEN: usize = 10;
109
110#[cfg(debug_assertions)]
111const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(1);
112
113#[cfg(not(debug_assertions))]
114const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(30);
115
116impl Telemetry {
117    pub fn new(client: Arc<dyn HttpClient>, cx: &AppContext) -> Arc<Self> {
118        let platform = cx.platform();
119        let release_channel = if cx.has_global::<ReleaseChannel>() {
120            Some(cx.global::<ReleaseChannel>().display_name())
121        } else {
122            None
123        };
124        // TODO: Replace all hardware stuff with nested SystemSpecs json
125        let this = Arc::new(Self {
126            http_client: client,
127            executor: cx.background().clone(),
128            state: Mutex::new(TelemetryState {
129                os_name: platform.os_name().into(),
130                os_version: platform.os_version().ok().map(|v| v.to_string().into()),
131                architecture: env::consts::ARCH,
132                app_version: platform.app_version().ok().map(|v| v.to_string().into()),
133                release_channel,
134                installation_id: None,
135                metrics_id: None,
136                clickhouse_events_queue: Default::default(),
137                flush_clickhouse_events_task: Default::default(),
138                log_file: None,
139                is_staff: None,
140            }),
141        });
142
143        this
144    }
145
146    pub fn log_file_path(&self) -> Option<PathBuf> {
147        Some(self.state.lock().log_file.as_ref()?.path().to_path_buf())
148    }
149
150    pub fn start(self: &Arc<Self>, installation_id: Option<String>, cx: &mut AppContext) {
151        let mut state = self.state.lock();
152        state.installation_id = installation_id.map(|id| id.into());
153        let has_clickhouse_events = !state.clickhouse_events_queue.is_empty();
154        drop(state);
155
156        if has_clickhouse_events {
157            self.flush_clickhouse_events();
158        }
159
160        let this = self.clone();
161        cx.spawn(|mut cx| async move {
162            let mut system = System::new_all();
163            system.refresh_all();
164
165            loop {
166                // Waiting some amount of time before the first query is important to get a reasonable value
167                // https://docs.rs/sysinfo/0.29.10/sysinfo/trait.ProcessExt.html#tymethod.cpu_usage
168                const DURATION_BETWEEN_SYSTEM_EVENTS: Duration = Duration::from_secs(60);
169                smol::Timer::after(DURATION_BETWEEN_SYSTEM_EVENTS).await;
170
171                let telemetry_settings = cx.update(|cx| *settings::get::<TelemetrySettings>(cx));
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                    start_time_in_seconds: process.start_time(),
188                    run_time_in_seconds: process.run_time(),
189                };
190
191                let cpu_event = ClickhouseEvent::Cpu {
192                    usage_as_percent: process.cpu_usage(),
193                    core_count: system.cpus().len() as u32,
194                };
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                                is_staff: state.is_staff.clone(),
293                                app_version: state.app_version.clone(),
294                                os_name: state.os_name,
295                                os_version: state.os_version.clone(),
296                                architecture: state.architecture,
297
298                                release_channel: state.release_channel,
299                                events,
300                            },
301                        )?;
302                    }
303
304                    this.http_client
305                        .post_json(CLICKHOUSE_EVENTS_URL.as_str(), json_bytes.into())
306                        .await?;
307                    anyhow::Ok(())
308                }
309                .log_err(),
310            )
311            .detach();
312    }
313}