telemetry.rs

  1use crate::{TelemetrySettings, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL};
  2use gpui2::{serde_json, AppContext, AppMetadata, Executor, Task};
  3use lazy_static::lazy_static;
  4use parking_lot::Mutex;
  5use serde::Serialize;
  6use settings2::Settings;
  7use std::{env, io::Write, mem, path::PathBuf, sync::Arc, time::Duration};
  8use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};
  9use tempfile::NamedTempFile;
 10use util::http::HttpClient;
 11use util::{channel::ReleaseChannel, TryFutureExt};
 12
 13pub struct Telemetry {
 14    http_client: Arc<dyn HttpClient>,
 15    executor: Executor,
 16    state: Mutex<TelemetryState>,
 17}
 18
 19struct TelemetryState {
 20    metrics_id: Option<Arc<str>>,      // Per logged-in user
 21    installation_id: Option<Arc<str>>, // Per app installation (different for dev, preview, and stable)
 22    session_id: Option<Arc<str>>,      // Per app launch
 23    release_channel: Option<&'static str>,
 24    app_metadata: AppMetadata,
 25    architecture: &'static str,
 26    clickhouse_events_queue: Vec<ClickhouseEventWrapper>,
 27    flush_clickhouse_events_task: Option<Task<()>>,
 28    log_file: Option<NamedTempFile>,
 29    is_staff: Option<bool>,
 30}
 31
 32const CLICKHOUSE_EVENTS_URL_PATH: &'static str = "/api/events";
 33
 34lazy_static! {
 35    static ref CLICKHOUSE_EVENTS_URL: String =
 36        format!("{}{}", *ZED_SERVER_URL, CLICKHOUSE_EVENTS_URL_PATH);
 37}
 38
 39#[derive(Serialize, Debug)]
 40struct ClickhouseEventRequestBody {
 41    token: &'static str,
 42    installation_id: Option<Arc<str>>,
 43    session_id: Option<Arc<str>>,
 44    is_staff: Option<bool>,
 45    app_version: Option<String>,
 46    os_name: &'static str,
 47    os_version: Option<String>,
 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_percentage: f32,
 94        core_count: u32,
 95    },
 96    Memory {
 97        memory_in_bytes: u64,
 98        virtual_memory_in_bytes: u64,
 99    },
100}
101
102#[cfg(debug_assertions)]
103const MAX_QUEUE_LEN: usize = 1;
104
105#[cfg(not(debug_assertions))]
106const MAX_QUEUE_LEN: usize = 10;
107
108#[cfg(debug_assertions)]
109const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(1);
110
111#[cfg(not(debug_assertions))]
112const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(30);
113
114impl Telemetry {
115    pub fn new(client: Arc<dyn HttpClient>, cx: &AppContext) -> Arc<Self> {
116        let release_channel = if cx.has_global::<ReleaseChannel>() {
117            Some(cx.global::<ReleaseChannel>().display_name())
118        } else {
119            None
120        };
121        // TODO: Replace all hardware stuff with nested SystemSpecs json
122        let this = Arc::new(Self {
123            http_client: client,
124            executor: cx.executor().clone(),
125            state: Mutex::new(TelemetryState {
126                app_metadata: cx.app_metadata(),
127                architecture: env::consts::ARCH,
128                release_channel,
129                installation_id: None,
130                metrics_id: None,
131                session_id: None,
132                clickhouse_events_queue: Default::default(),
133                flush_clickhouse_events_task: Default::default(),
134                log_file: None,
135                is_staff: None,
136            }),
137        });
138
139        this
140    }
141
142    pub fn log_file_path(&self) -> Option<PathBuf> {
143        Some(self.state.lock().log_file.as_ref()?.path().to_path_buf())
144    }
145
146    pub fn start(
147        self: &Arc<Self>,
148        installation_id: Option<String>,
149        session_id: String,
150        cx: &mut AppContext,
151    ) {
152        let mut state = self.state.lock();
153        state.installation_id = installation_id.map(|id| id.into());
154        state.session_id = Some(session_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(|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 = if let Ok(telemetry_settings) =
195                    cx.update(|cx| *TelemetrySettings::get_global(cx))
196                {
197                    telemetry_settings
198                } else {
199                    break;
200                };
201
202                this.report_clickhouse_event(memory_event, telemetry_settings);
203                this.report_clickhouse_event(cpu_event, telemetry_settings);
204            }
205        })
206        .detach();
207    }
208
209    pub fn set_authenticated_user_info(
210        self: &Arc<Self>,
211        metrics_id: Option<String>,
212        is_staff: bool,
213        cx: &AppContext,
214    ) {
215        if !TelemetrySettings::get_global(cx).metrics {
216            return;
217        }
218
219        let mut state = self.state.lock();
220        let metrics_id: Option<Arc<str>> = metrics_id.map(|id| id.into());
221        state.metrics_id = metrics_id.clone();
222        state.is_staff = Some(is_staff);
223        drop(state);
224    }
225
226    pub fn report_clickhouse_event(
227        self: &Arc<Self>,
228        event: ClickhouseEvent,
229        telemetry_settings: TelemetrySettings,
230    ) {
231        if !telemetry_settings.metrics {
232            return;
233        }
234
235        let mut state = self.state.lock();
236        let signed_in = state.metrics_id.is_some();
237        state
238            .clickhouse_events_queue
239            .push(ClickhouseEventWrapper { signed_in, event });
240
241        if state.installation_id.is_some() {
242            if state.clickhouse_events_queue.len() >= MAX_QUEUE_LEN {
243                drop(state);
244                self.flush_clickhouse_events();
245            } else {
246                let this = self.clone();
247                let executor = self.executor.clone();
248                state.flush_clickhouse_events_task = Some(self.executor.spawn(async move {
249                    executor.timer(DEBOUNCE_INTERVAL).await;
250                    this.flush_clickhouse_events();
251                }));
252            }
253        }
254    }
255
256    pub fn metrics_id(self: &Arc<Self>) -> Option<Arc<str>> {
257        self.state.lock().metrics_id.clone()
258    }
259
260    pub fn installation_id(self: &Arc<Self>) -> Option<Arc<str>> {
261        self.state.lock().installation_id.clone()
262    }
263
264    pub fn is_staff(self: &Arc<Self>) -> Option<bool> {
265        self.state.lock().is_staff
266    }
267
268    fn flush_clickhouse_events(self: &Arc<Self>) {
269        let mut state = self.state.lock();
270        let mut events = mem::take(&mut state.clickhouse_events_queue);
271        state.flush_clickhouse_events_task.take();
272        drop(state);
273
274        let this = self.clone();
275        self.executor
276            .spawn(
277                async move {
278                    let mut json_bytes = Vec::new();
279
280                    if let Some(file) = &mut this.state.lock().log_file {
281                        let file = file.as_file_mut();
282                        for event in &mut events {
283                            json_bytes.clear();
284                            serde_json::to_writer(&mut json_bytes, event)?;
285                            file.write_all(&json_bytes)?;
286                            file.write(b"\n")?;
287                        }
288                    }
289
290                    {
291                        let state = this.state.lock();
292                        let request_body = ClickhouseEventRequestBody {
293                            token: ZED_SECRET_CLIENT_TOKEN,
294                            installation_id: state.installation_id.clone(),
295                            session_id: state.session_id.clone(),
296                            is_staff: state.is_staff.clone(),
297                            app_version: state
298                                .app_metadata
299                                .app_version
300                                .map(|version| version.to_string()),
301                            os_name: state.app_metadata.os_name,
302                            os_version: state
303                                .app_metadata
304                                .os_version
305                                .map(|version| version.to_string()),
306                            architecture: state.architecture,
307
308                            release_channel: state.release_channel,
309                            events,
310                        };
311                        json_bytes.clear();
312                        serde_json::to_writer(&mut json_bytes, &request_body)?;
313                    }
314
315                    this.http_client
316                        .post_json(CLICKHOUSE_EVENTS_URL.as_str(), json_bytes.into())
317                        .await?;
318                    anyhow::Ok(())
319                }
320                .log_err(),
321            )
322            .detach();
323    }
324}