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<Arc<str>>,
 45    os_name: &'static str,
 46    os_version: Option<Arc<str>>,
 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(|mut 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 = cx.update(|cx| *settings2::get::<TelemetrySettings>(cx));
194
195                this.report_clickhouse_event(memory_event, telemetry_settings);
196                this.report_clickhouse_event(cpu_event, telemetry_settings);
197            }
198        })
199        .detach();
200    }
201
202    pub fn set_authenticated_user_info(
203        self: &Arc<Self>,
204        metrics_id: Option<String>,
205        is_staff: bool,
206        cx: &AppContext,
207    ) {
208        if !settings2::get::<TelemetrySettings>(cx).metrics {
209            return;
210        }
211
212        let mut state = self.state.lock();
213        let metrics_id: Option<Arc<str>> = metrics_id.map(|id| id.into());
214        state.metrics_id = metrics_id.clone();
215        state.is_staff = Some(is_staff);
216        drop(state);
217    }
218
219    pub fn report_clickhouse_event(
220        self: &Arc<Self>,
221        event: ClickhouseEvent,
222        telemetry_settings: TelemetrySettings,
223    ) {
224        if !telemetry_settings.metrics {
225            return;
226        }
227
228        let mut state = self.state.lock();
229        let signed_in = state.metrics_id.is_some();
230        state
231            .clickhouse_events_queue
232            .push(ClickhouseEventWrapper { signed_in, event });
233
234        if state.installation_id.is_some() {
235            if state.clickhouse_events_queue.len() >= MAX_QUEUE_LEN {
236                drop(state);
237                self.flush_clickhouse_events();
238            } else {
239                let this = self.clone();
240                let executor = self.executor.clone();
241                state.flush_clickhouse_events_task = Some(self.executor.spawn(async move {
242                    executor.timer(DEBOUNCE_INTERVAL).await;
243                    this.flush_clickhouse_events();
244                }));
245            }
246        }
247    }
248
249    pub fn metrics_id(self: &Arc<Self>) -> Option<Arc<str>> {
250        self.state.lock().metrics_id.clone()
251    }
252
253    pub fn installation_id(self: &Arc<Self>) -> Option<Arc<str>> {
254        self.state.lock().installation_id.clone()
255    }
256
257    pub fn is_staff(self: &Arc<Self>) -> Option<bool> {
258        self.state.lock().is_staff
259    }
260
261    fn flush_clickhouse_events(self: &Arc<Self>) {
262        let mut state = self.state.lock();
263        let mut events = mem::take(&mut state.clickhouse_events_queue);
264        state.flush_clickhouse_events_task.take();
265        drop(state);
266
267        let this = self.clone();
268        self.executor
269            .spawn(
270                async move {
271                    let mut json_bytes = Vec::new();
272
273                    if let Some(file) = &mut this.state.lock().log_file {
274                        let file = file.as_file_mut();
275                        for event in &mut events {
276                            json_bytes.clear();
277                            serde_json::to_writer(&mut json_bytes, event)?;
278                            file.write_all(&json_bytes)?;
279                            file.write(b"\n")?;
280                        }
281                    }
282
283                    {
284                        let state = this.state.lock();
285                        let request_body = ClickhouseEventRequestBody {
286                            token: ZED_SECRET_CLIENT_TOKEN,
287                            installation_id: state.installation_id.clone(),
288                            session_id: state.session_id.clone(),
289                            is_staff: state.is_staff.clone(),
290                            app_version: state.app_version.clone(),
291                            os_name: state.os_name,
292                            os_version: state.os_version.clone(),
293                            architecture: state.architecture,
294
295                            release_channel: state.release_channel,
296                            events,
297                        };
298                        json_bytes.clear();
299                        serde_json::to_writer(&mut json_bytes, &request_body)?;
300                    }
301
302                    this.http_client
303                        .post_json(CLICKHOUSE_EVENTS_URL.as_str(), json_bytes.into())
304                        .await?;
305                    anyhow::Ok(())
306                }
307                .log_err(),
308            )
309            .detach();
310    }
311}