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