1use crate::{TelemetrySettings, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL};
2use chrono::{DateTime, Utc};
3use gpui::{executor::Background, serde_json, AppContext, Task};
4use lazy_static::lazy_static;
5use parking_lot::Mutex;
6use serde::Serialize;
7use std::{env, io::Write, mem, path::PathBuf, sync::Arc, time::Duration};
8use sysinfo::{
9 CpuRefreshKind, Pid, PidExt, ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt,
10};
11use tempfile::NamedTempFile;
12use util::http::HttpClient;
13use util::{channel::ReleaseChannel, TryFutureExt};
14
15pub struct Telemetry {
16 http_client: Arc<dyn HttpClient>,
17 executor: Arc<Background>,
18 state: Mutex<TelemetryState>,
19}
20
21#[derive(Default)]
22struct TelemetryState {
23 metrics_id: Option<Arc<str>>, // Per logged-in user
24 installation_id: Option<Arc<str>>, // Per app installation (different for dev, nightly, preview, and stable)
25 session_id: Option<Arc<str>>, // Per app launch
26 app_version: Option<Arc<str>>,
27 release_channel: Option<&'static str>,
28 os_name: &'static str,
29 os_version: Option<Arc<str>>,
30 architecture: &'static str,
31 clickhouse_events_queue: Vec<ClickhouseEventWrapper>,
32 flush_clickhouse_events_task: Option<Task<()>>,
33 log_file: Option<NamedTempFile>,
34 is_staff: Option<bool>,
35 first_event_datetime: Option<DateTime<Utc>>,
36}
37
38const CLICKHOUSE_EVENTS_URL_PATH: &'static str = "/api/events";
39
40lazy_static! {
41 static ref CLICKHOUSE_EVENTS_URL: String =
42 format!("{}{}", *ZED_SERVER_URL, CLICKHOUSE_EVENTS_URL_PATH);
43}
44
45#[derive(Serialize, Debug)]
46struct ClickhouseEventRequestBody {
47 token: &'static str,
48 installation_id: Option<Arc<str>>,
49 session_id: Option<Arc<str>>,
50 is_staff: Option<bool>,
51 app_version: Option<Arc<str>>,
52 os_name: &'static str,
53 os_version: Option<Arc<str>>,
54 architecture: &'static str,
55 release_channel: Option<&'static str>,
56 events: Vec<ClickhouseEventWrapper>,
57}
58
59#[derive(Serialize, Debug)]
60struct ClickhouseEventWrapper {
61 signed_in: bool,
62 #[serde(flatten)]
63 event: ClickhouseEvent,
64}
65
66#[derive(Serialize, Debug)]
67#[serde(rename_all = "snake_case")]
68pub enum AssistantKind {
69 Panel,
70 Inline,
71}
72
73#[derive(Serialize, Debug)]
74#[serde(tag = "type")]
75pub enum ClickhouseEvent {
76 Editor {
77 operation: &'static str,
78 file_extension: Option<String>,
79 vim_mode: bool,
80 copilot_enabled: bool,
81 copilot_enabled_for_language: bool,
82 milliseconds_since_first_event: i64,
83 },
84 Copilot {
85 suggestion_id: Option<String>,
86 suggestion_accepted: bool,
87 file_extension: Option<String>,
88 milliseconds_since_first_event: i64,
89 },
90 Call {
91 operation: &'static str,
92 room_id: Option<u64>,
93 channel_id: Option<u64>,
94 milliseconds_since_first_event: i64,
95 },
96 Assistant {
97 conversation_id: Option<String>,
98 kind: AssistantKind,
99 model: &'static str,
100 milliseconds_since_first_event: i64,
101 },
102 Cpu {
103 usage_as_percentage: f32,
104 core_count: u32,
105 milliseconds_since_first_event: i64,
106 },
107 Memory {
108 memory_in_bytes: u64,
109 virtual_memory_in_bytes: u64,
110 milliseconds_since_first_event: i64,
111 },
112 App {
113 operation: &'static str,
114 milliseconds_since_first_event: i64,
115 },
116}
117
118#[cfg(debug_assertions)]
119const MAX_QUEUE_LEN: usize = 1;
120
121#[cfg(not(debug_assertions))]
122const MAX_QUEUE_LEN: usize = 10;
123
124#[cfg(debug_assertions)]
125const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(1);
126
127#[cfg(not(debug_assertions))]
128const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(30);
129
130impl Telemetry {
131 pub fn new(client: Arc<dyn HttpClient>, cx: &AppContext) -> Arc<Self> {
132 let platform = cx.platform();
133 let release_channel = if cx.has_global::<ReleaseChannel>() {
134 Some(cx.global::<ReleaseChannel>().display_name())
135 } else {
136 None
137 };
138 // TODO: Replace all hardware stuff with nested SystemSpecs json
139 let this = Arc::new(Self {
140 http_client: client,
141 executor: cx.background().clone(),
142 state: Mutex::new(TelemetryState {
143 os_name: platform.os_name().into(),
144 os_version: platform.os_version().ok().map(|v| v.to_string().into()),
145 architecture: env::consts::ARCH,
146 app_version: platform.app_version().ok().map(|v| v.to_string().into()),
147 release_channel,
148 installation_id: None,
149 metrics_id: None,
150 session_id: None,
151 clickhouse_events_queue: Default::default(),
152 flush_clickhouse_events_task: Default::default(),
153 log_file: None,
154 is_staff: None,
155 first_event_datetime: None,
156 }),
157 });
158
159 this
160 }
161
162 pub fn log_file_path(&self) -> Option<PathBuf> {
163 Some(self.state.lock().log_file.as_ref()?.path().to_path_buf())
164 }
165
166 pub fn start(
167 self: &Arc<Self>,
168 installation_id: Option<String>,
169 session_id: String,
170 cx: &mut AppContext,
171 ) {
172 let mut state = self.state.lock();
173 state.installation_id = installation_id.map(|id| id.into());
174 state.session_id = Some(session_id.into());
175 drop(state);
176
177 let this = self.clone();
178 cx.spawn(|mut cx| async move {
179 // Avoiding calling `System::new_all()`, as there have been crashes related to it
180 let refresh_kind = RefreshKind::new()
181 .with_memory() // For memory usage
182 .with_processes(ProcessRefreshKind::everything()) // For process usage
183 .with_cpu(CpuRefreshKind::everything()); // For core count
184
185 let mut system = System::new_with_specifics(refresh_kind);
186
187 // Avoiding calling `refresh_all()`, just update what we need
188 system.refresh_specifics(refresh_kind);
189
190 loop {
191 // Waiting some amount of time before the first query is important to get a reasonable value
192 // https://docs.rs/sysinfo/0.29.10/sysinfo/trait.ProcessExt.html#tymethod.cpu_usage
193 const DURATION_BETWEEN_SYSTEM_EVENTS: Duration = Duration::from_secs(60);
194 smol::Timer::after(DURATION_BETWEEN_SYSTEM_EVENTS).await;
195
196 system.refresh_specifics(refresh_kind);
197
198 let current_process = Pid::from_u32(std::process::id());
199 let Some(process) = system.processes().get(¤t_process) else {
200 let process = current_process;
201 log::error!("Failed to find own process {process:?} in system process table");
202 // TODO: Fire an error telemetry event
203 return;
204 };
205
206 let telemetry_settings = cx.update(|cx| *settings::get::<TelemetrySettings>(cx));
207
208 this.report_memory_event(
209 telemetry_settings,
210 process.memory(),
211 process.virtual_memory(),
212 );
213 this.report_cpu_event(
214 telemetry_settings,
215 process.cpu_usage(),
216 system.cpus().len() as u32,
217 );
218 }
219 })
220 .detach();
221 }
222
223 pub fn set_authenticated_user_info(
224 self: &Arc<Self>,
225 metrics_id: Option<String>,
226 is_staff: bool,
227 cx: &AppContext,
228 ) {
229 if !settings::get::<TelemetrySettings>(cx).metrics {
230 return;
231 }
232
233 let mut state = self.state.lock();
234 let metrics_id: Option<Arc<str>> = metrics_id.map(|id| id.into());
235 state.metrics_id = metrics_id.clone();
236 state.is_staff = Some(is_staff);
237 drop(state);
238 }
239
240 pub fn report_editor_event(
241 self: &Arc<Self>,
242 telemetry_settings: TelemetrySettings,
243 file_extension: Option<String>,
244 vim_mode: bool,
245 operation: &'static str,
246 copilot_enabled: bool,
247 copilot_enabled_for_language: bool,
248 ) {
249 let event = ClickhouseEvent::Editor {
250 file_extension,
251 vim_mode,
252 operation,
253 copilot_enabled,
254 copilot_enabled_for_language,
255 milliseconds_since_first_event: self.milliseconds_since_first_event(),
256 };
257
258 self.report_clickhouse_event(event, telemetry_settings, false)
259 }
260
261 pub fn report_copilot_event(
262 self: &Arc<Self>,
263 telemetry_settings: TelemetrySettings,
264 suggestion_id: Option<String>,
265 suggestion_accepted: bool,
266 file_extension: Option<String>,
267 ) {
268 let event = ClickhouseEvent::Copilot {
269 suggestion_id,
270 suggestion_accepted,
271 file_extension,
272 milliseconds_since_first_event: self.milliseconds_since_first_event(),
273 };
274
275 self.report_clickhouse_event(event, telemetry_settings, false)
276 }
277
278 pub fn report_assistant_event(
279 self: &Arc<Self>,
280 telemetry_settings: TelemetrySettings,
281 conversation_id: Option<String>,
282 kind: AssistantKind,
283 model: &'static str,
284 ) {
285 let event = ClickhouseEvent::Assistant {
286 conversation_id,
287 kind,
288 model,
289 milliseconds_since_first_event: self.milliseconds_since_first_event(),
290 };
291
292 self.report_clickhouse_event(event, telemetry_settings, false)
293 }
294
295 pub fn report_call_event(
296 self: &Arc<Self>,
297 telemetry_settings: TelemetrySettings,
298 operation: &'static str,
299 room_id: Option<u64>,
300 channel_id: Option<u64>,
301 ) {
302 let event = ClickhouseEvent::Call {
303 operation,
304 room_id,
305 channel_id,
306 milliseconds_since_first_event: self.milliseconds_since_first_event(),
307 };
308
309 self.report_clickhouse_event(event, telemetry_settings, false)
310 }
311
312 pub fn report_cpu_event(
313 self: &Arc<Self>,
314 telemetry_settings: TelemetrySettings,
315 usage_as_percentage: f32,
316 core_count: u32,
317 ) {
318 let event = ClickhouseEvent::Cpu {
319 usage_as_percentage,
320 core_count,
321 milliseconds_since_first_event: self.milliseconds_since_first_event(),
322 };
323
324 self.report_clickhouse_event(event, telemetry_settings, false)
325 }
326
327 pub fn report_memory_event(
328 self: &Arc<Self>,
329 telemetry_settings: TelemetrySettings,
330 memory_in_bytes: u64,
331 virtual_memory_in_bytes: u64,
332 ) {
333 let event = ClickhouseEvent::Memory {
334 memory_in_bytes,
335 virtual_memory_in_bytes,
336 milliseconds_since_first_event: self.milliseconds_since_first_event(),
337 };
338
339 self.report_clickhouse_event(event, telemetry_settings, false)
340 }
341
342 // app_events are called at app open and app close, so flush is set to immediately send
343 pub fn report_app_event(
344 self: &Arc<Self>,
345 telemetry_settings: TelemetrySettings,
346 operation: &'static str,
347 ) {
348 let event = ClickhouseEvent::App {
349 operation,
350 milliseconds_since_first_event: self.milliseconds_since_first_event(),
351 };
352
353 dbg!(telemetry_settings);
354 self.report_clickhouse_event(event, telemetry_settings, true)
355 }
356
357 fn milliseconds_since_first_event(&self) -> i64 {
358 let mut state = self.state.lock();
359 match state.first_event_datetime {
360 Some(first_event_datetime) => {
361 let now: DateTime<Utc> = Utc::now();
362 now.timestamp_millis() - first_event_datetime.timestamp_millis()
363 }
364 None => {
365 state.first_event_datetime = Some(Utc::now());
366 0
367 }
368 }
369 }
370
371 fn report_clickhouse_event(
372 self: &Arc<Self>,
373 event: ClickhouseEvent,
374 telemetry_settings: TelemetrySettings,
375 immediate_flush: bool,
376 ) {
377 if !telemetry_settings.metrics {
378 return;
379 }
380
381 let mut state = self.state.lock();
382 let signed_in = state.metrics_id.is_some();
383 state
384 .clickhouse_events_queue
385 .push(ClickhouseEventWrapper { signed_in, event });
386
387 if state.installation_id.is_some() {
388 if immediate_flush || state.clickhouse_events_queue.len() >= MAX_QUEUE_LEN {
389 drop(state);
390 self.flush_clickhouse_events();
391 } else {
392 let this = self.clone();
393 let executor = self.executor.clone();
394 state.flush_clickhouse_events_task = Some(self.executor.spawn(async move {
395 executor.timer(DEBOUNCE_INTERVAL).await;
396 this.flush_clickhouse_events();
397 }));
398 }
399 }
400 }
401
402 pub fn metrics_id(self: &Arc<Self>) -> Option<Arc<str>> {
403 self.state.lock().metrics_id.clone()
404 }
405
406 pub fn installation_id(self: &Arc<Self>) -> Option<Arc<str>> {
407 self.state.lock().installation_id.clone()
408 }
409
410 pub fn is_staff(self: &Arc<Self>) -> Option<bool> {
411 self.state.lock().is_staff
412 }
413
414 fn flush_clickhouse_events(self: &Arc<Self>) {
415 let mut state = self.state.lock();
416 state.first_event_datetime = None;
417 let mut events = mem::take(&mut state.clickhouse_events_queue);
418 state.flush_clickhouse_events_task.take();
419 drop(state);
420
421 let this = self.clone();
422 self.executor
423 .spawn(
424 async move {
425 let mut json_bytes = Vec::new();
426
427 if let Some(file) = &mut this.state.lock().log_file {
428 let file = file.as_file_mut();
429 for event in &mut events {
430 json_bytes.clear();
431 serde_json::to_writer(&mut json_bytes, event)?;
432 file.write_all(&json_bytes)?;
433 file.write(b"\n")?;
434 }
435 }
436
437 {
438 let state = this.state.lock();
439 let request_body = ClickhouseEventRequestBody {
440 token: ZED_SECRET_CLIENT_TOKEN,
441 installation_id: state.installation_id.clone(),
442 session_id: state.session_id.clone(),
443 is_staff: state.is_staff.clone(),
444 app_version: state.app_version.clone(),
445 os_name: state.os_name,
446 os_version: state.os_version.clone(),
447 architecture: state.architecture,
448
449 release_channel: state.release_channel,
450 events,
451 };
452 json_bytes.clear();
453 serde_json::to_writer(&mut json_bytes, &request_body)?;
454 }
455
456 this.http_client
457 .post_json(CLICKHOUSE_EVENTS_URL.as_str(), json_bytes.into())
458 .await?;
459 anyhow::Ok(())
460 }
461 .log_err(),
462 )
463 .detach();
464 }
465}