1mod event_coalescer;
2
3use crate::{ChannelId, TelemetrySettings};
4use anyhow::Result;
5use clock::SystemClock;
6use collections::{HashMap, HashSet};
7use futures::Future;
8use gpui::{AppContext, BackgroundExecutor, Task};
9use http_client::{self, AsyncBody, HttpClient, HttpClientWithUrl, Method, Request};
10use once_cell::sync::Lazy;
11use parking_lot::Mutex;
12use release_channel::ReleaseChannel;
13use settings::{Settings, SettingsStore};
14use sha2::{Digest, Sha256};
15use std::fs::File;
16use std::io::Write;
17use std::time::Instant;
18use std::{env, mem, path::PathBuf, sync::Arc, time::Duration};
19use telemetry_events::{
20 ActionEvent, AppEvent, AssistantEvent, CallEvent, EditEvent, EditorEvent, Event,
21 EventRequestBody, EventWrapper, ExtensionEvent, InlineCompletionEvent, ReplEvent, SettingEvent,
22};
23use util::{ResultExt, TryFutureExt};
24use worktree::{UpdatedEntriesSet, WorktreeId};
25
26use self::event_coalescer::EventCoalescer;
27
28pub struct Telemetry {
29 clock: Arc<dyn SystemClock>,
30 http_client: Arc<HttpClientWithUrl>,
31 executor: BackgroundExecutor,
32 state: Arc<Mutex<TelemetryState>>,
33}
34
35struct TelemetryState {
36 settings: TelemetrySettings,
37 system_id: Option<Arc<str>>, // Per system
38 installation_id: Option<Arc<str>>, // Per app installation (different for dev, nightly, preview, and stable)
39 session_id: Option<String>, // Per app launch
40 metrics_id: Option<Arc<str>>, // Per logged-in user
41 release_channel: Option<&'static str>,
42 architecture: &'static str,
43 events_queue: Vec<EventWrapper>,
44 flush_events_task: Option<Task<()>>,
45 log_file: Option<File>,
46 is_staff: Option<bool>,
47 first_event_date_time: Option<Instant>,
48 event_coalescer: EventCoalescer,
49 max_queue_size: usize,
50 worktree_id_map: WorktreeIdMap,
51
52 os_name: String,
53 app_version: String,
54 os_version: Option<String>,
55}
56
57#[derive(Debug)]
58struct WorktreeIdMap(HashMap<String, ProjectCache>);
59
60#[derive(Debug)]
61struct ProjectCache {
62 name: String,
63 worktree_ids_reported: HashSet<WorktreeId>,
64}
65
66impl ProjectCache {
67 fn new(name: String) -> Self {
68 Self {
69 name,
70 worktree_ids_reported: HashSet::default(),
71 }
72 }
73}
74
75#[cfg(debug_assertions)]
76const MAX_QUEUE_LEN: usize = 5;
77
78#[cfg(not(debug_assertions))]
79const MAX_QUEUE_LEN: usize = 50;
80
81#[cfg(debug_assertions)]
82const FLUSH_INTERVAL: Duration = Duration::from_secs(1);
83
84#[cfg(not(debug_assertions))]
85const FLUSH_INTERVAL: Duration = Duration::from_secs(60 * 5);
86static ZED_CLIENT_CHECKSUM_SEED: Lazy<Option<Vec<u8>>> = Lazy::new(|| {
87 option_env!("ZED_CLIENT_CHECKSUM_SEED")
88 .map(|s| s.as_bytes().into())
89 .or_else(|| {
90 env::var("ZED_CLIENT_CHECKSUM_SEED")
91 .ok()
92 .map(|s| s.as_bytes().into())
93 })
94});
95
96pub fn os_name() -> String {
97 #[cfg(target_os = "macos")]
98 {
99 "macOS".to_string()
100 }
101 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
102 {
103 format!("Linux {}", gpui::guess_compositor())
104 }
105
106 #[cfg(target_os = "windows")]
107 {
108 "Windows".to_string()
109 }
110}
111
112/// Note: This might do blocking IO! Only call from background threads
113pub fn os_version() -> String {
114 #[cfg(target_os = "macos")]
115 {
116 use cocoa::base::nil;
117 use cocoa::foundation::NSProcessInfo;
118
119 unsafe {
120 let process_info = cocoa::foundation::NSProcessInfo::processInfo(nil);
121 let version = process_info.operatingSystemVersion();
122 gpui::SemanticVersion::new(
123 version.majorVersion as usize,
124 version.minorVersion as usize,
125 version.patchVersion as usize,
126 )
127 .to_string()
128 }
129 }
130 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
131 {
132 use std::path::Path;
133
134 let content = if let Ok(file) = std::fs::read_to_string(&Path::new("/etc/os-release")) {
135 file
136 } else if let Ok(file) = std::fs::read_to_string(&Path::new("/usr/lib/os-release")) {
137 file
138 } else {
139 log::error!("Failed to load /etc/os-release, /usr/lib/os-release");
140 "".to_string()
141 };
142 let mut name = "unknown".to_string();
143 let mut version = "unknown".to_string();
144
145 for line in content.lines() {
146 if line.starts_with("ID=") {
147 name = line.trim_start_matches("ID=").trim_matches('"').to_string();
148 }
149 if line.starts_with("VERSION_ID=") {
150 version = line
151 .trim_start_matches("VERSION_ID=")
152 .trim_matches('"')
153 .to_string();
154 }
155 }
156
157 format!("{} {}", name, version)
158 }
159
160 #[cfg(target_os = "windows")]
161 {
162 let mut info = unsafe { std::mem::zeroed() };
163 let status = unsafe { windows::Wdk::System::SystemServices::RtlGetVersion(&mut info) };
164 if status.is_ok() {
165 gpui::SemanticVersion::new(
166 info.dwMajorVersion as _,
167 info.dwMinorVersion as _,
168 info.dwBuildNumber as _,
169 )
170 .to_string()
171 } else {
172 "unknown".to_string()
173 }
174 }
175}
176
177impl Telemetry {
178 pub fn new(
179 clock: Arc<dyn SystemClock>,
180 client: Arc<HttpClientWithUrl>,
181 cx: &mut AppContext,
182 ) -> Arc<Self> {
183 let release_channel =
184 ReleaseChannel::try_global(cx).map(|release_channel| release_channel.display_name());
185
186 TelemetrySettings::register(cx);
187
188 let state = Arc::new(Mutex::new(TelemetryState {
189 settings: *TelemetrySettings::get_global(cx),
190 architecture: env::consts::ARCH,
191 release_channel,
192 system_id: None,
193 installation_id: None,
194 session_id: None,
195 metrics_id: None,
196 events_queue: Vec::new(),
197 flush_events_task: None,
198 log_file: None,
199 is_staff: None,
200 first_event_date_time: None,
201 event_coalescer: EventCoalescer::new(clock.clone()),
202 max_queue_size: MAX_QUEUE_LEN,
203 worktree_id_map: WorktreeIdMap(HashMap::from_iter([
204 (
205 "pnpm-lock.yaml".to_string(),
206 ProjectCache::new("pnpm".to_string()),
207 ),
208 (
209 "yarn.lock".to_string(),
210 ProjectCache::new("yarn".to_string()),
211 ),
212 (
213 "package.json".to_string(),
214 ProjectCache::new("node".to_string()),
215 ),
216 ])),
217
218 os_version: None,
219 os_name: os_name(),
220 app_version: release_channel::AppVersion::global(cx).to_string(),
221 }));
222 Self::log_file_path();
223
224 cx.background_executor()
225 .spawn({
226 let state = state.clone();
227 async move {
228 if let Some(tempfile) = File::create(Self::log_file_path()).log_err() {
229 state.lock().log_file = Some(tempfile);
230 }
231 }
232 })
233 .detach();
234
235 cx.observe_global::<SettingsStore>({
236 let state = state.clone();
237
238 move |cx| {
239 let mut state = state.lock();
240 state.settings = *TelemetrySettings::get_global(cx);
241 }
242 })
243 .detach();
244
245 // TODO: Replace all hardware stuff with nested SystemSpecs json
246 let this = Arc::new(Self {
247 clock,
248 http_client: client,
249 executor: cx.background_executor().clone(),
250 state,
251 });
252
253 // We should only ever have one instance of Telemetry, leak the subscription to keep it alive
254 // rather than store in TelemetryState, complicating spawn as subscriptions are not Send
255 std::mem::forget(cx.on_app_quit({
256 let this = this.clone();
257 move |_| this.shutdown_telemetry()
258 }));
259
260 this
261 }
262
263 #[cfg(any(test, feature = "test-support"))]
264 fn shutdown_telemetry(self: &Arc<Self>) -> impl Future<Output = ()> {
265 Task::ready(())
266 }
267
268 // Skip calling this function in tests.
269 // TestAppContext ends up calling this function on shutdown and it panics when trying to find the TelemetrySettings
270 #[cfg(not(any(test, feature = "test-support")))]
271 fn shutdown_telemetry(self: &Arc<Self>) -> impl Future<Output = ()> {
272 self.report_app_event("close".to_string());
273 // TODO: close final edit period and make sure it's sent
274 Task::ready(())
275 }
276
277 pub fn log_file_path() -> PathBuf {
278 paths::logs_dir().join("telemetry.log")
279 }
280
281 pub fn start(
282 self: &Arc<Self>,
283 system_id: Option<String>,
284 installation_id: Option<String>,
285 session_id: String,
286 cx: &AppContext,
287 ) {
288 let mut state = self.state.lock();
289 state.system_id = system_id.map(|id| id.into());
290 state.installation_id = installation_id.map(|id| id.into());
291 state.session_id = Some(session_id);
292 state.app_version = release_channel::AppVersion::global(cx).to_string();
293 state.os_name = os_name();
294 }
295
296 pub fn metrics_enabled(self: &Arc<Self>) -> bool {
297 let state = self.state.lock();
298 let enabled = state.settings.metrics;
299 drop(state);
300 enabled
301 }
302
303 pub fn set_authenticated_user_info(
304 self: &Arc<Self>,
305 metrics_id: Option<String>,
306 is_staff: bool,
307 ) {
308 let mut state = self.state.lock();
309
310 if !state.settings.metrics {
311 return;
312 }
313
314 let metrics_id: Option<Arc<str>> = metrics_id.map(|id| id.into());
315 state.metrics_id.clone_from(&metrics_id);
316 state.is_staff = Some(is_staff);
317 drop(state);
318 }
319
320 pub fn report_editor_event(
321 self: &Arc<Self>,
322 file_extension: Option<String>,
323 vim_mode: bool,
324 operation: &'static str,
325 copilot_enabled: bool,
326 copilot_enabled_for_language: bool,
327 is_via_ssh: bool,
328 ) {
329 let event = Event::Editor(EditorEvent {
330 file_extension,
331 vim_mode,
332 operation: operation.into(),
333 copilot_enabled,
334 copilot_enabled_for_language,
335 is_via_ssh,
336 });
337
338 self.report_event(event)
339 }
340
341 pub fn report_inline_completion_event(
342 self: &Arc<Self>,
343 provider: String,
344 suggestion_accepted: bool,
345 file_extension: Option<String>,
346 ) {
347 let event = Event::InlineCompletion(InlineCompletionEvent {
348 provider,
349 suggestion_accepted,
350 file_extension,
351 });
352
353 self.report_event(event)
354 }
355
356 pub fn report_assistant_event(self: &Arc<Self>, event: AssistantEvent) {
357 self.report_event(Event::Assistant(event));
358 }
359
360 pub fn report_call_event(
361 self: &Arc<Self>,
362 operation: &'static str,
363 room_id: Option<u64>,
364 channel_id: Option<ChannelId>,
365 ) {
366 let event = Event::Call(CallEvent {
367 operation: operation.to_string(),
368 room_id,
369 channel_id: channel_id.map(|cid| cid.0),
370 });
371
372 self.report_event(event)
373 }
374
375 pub fn report_app_event(self: &Arc<Self>, operation: String) -> Event {
376 let event = Event::App(AppEvent { operation });
377
378 self.report_event(event.clone());
379
380 event
381 }
382
383 pub fn report_setting_event(self: &Arc<Self>, setting: &'static str, value: String) {
384 let event = Event::Setting(SettingEvent {
385 setting: setting.to_string(),
386 value,
387 });
388
389 self.report_event(event)
390 }
391
392 pub fn report_extension_event(self: &Arc<Self>, extension_id: Arc<str>, version: Arc<str>) {
393 self.report_event(Event::Extension(ExtensionEvent {
394 extension_id,
395 version,
396 }))
397 }
398
399 pub fn log_edit_event(self: &Arc<Self>, environment: &'static str, is_via_ssh: bool) {
400 let mut state = self.state.lock();
401 let period_data = state.event_coalescer.log_event(environment);
402 drop(state);
403
404 if let Some((start, end, environment)) = period_data {
405 let event = Event::Edit(EditEvent {
406 duration: end
407 .saturating_duration_since(start)
408 .min(Duration::from_secs(60 * 60 * 24))
409 .as_millis() as i64,
410 environment: environment.to_string(),
411 is_via_ssh,
412 });
413
414 self.report_event(event);
415 }
416 }
417
418 pub fn report_action_event(self: &Arc<Self>, source: &'static str, action: String) {
419 let event = Event::Action(ActionEvent {
420 source: source.to_string(),
421 action,
422 });
423
424 self.report_event(event)
425 }
426
427 pub fn report_discovered_project_events(
428 self: &Arc<Self>,
429 worktree_id: WorktreeId,
430 updated_entries_set: &UpdatedEntriesSet,
431 ) {
432 let project_type_names: Vec<String> = {
433 let mut state = self.state.lock();
434 state
435 .worktree_id_map
436 .0
437 .iter_mut()
438 .filter_map(|(project_file_name, project_type_telemetry)| {
439 if project_type_telemetry
440 .worktree_ids_reported
441 .contains(&worktree_id)
442 {
443 return None;
444 }
445
446 let project_file_found = updated_entries_set.iter().any(|(path, _, _)| {
447 path.as_ref()
448 .file_name()
449 .and_then(|name| name.to_str())
450 .map(|name_str| name_str == project_file_name)
451 .unwrap_or(false)
452 });
453
454 if !project_file_found {
455 return None;
456 }
457
458 project_type_telemetry
459 .worktree_ids_reported
460 .insert(worktree_id);
461
462 Some(project_type_telemetry.name.clone())
463 })
464 .collect()
465 };
466
467 // Done on purpose to avoid calling `self.state.lock()` multiple times
468 for project_type_name in project_type_names {
469 self.report_app_event(format!("open {} project", project_type_name));
470 }
471 }
472
473 pub fn report_repl_event(
474 self: &Arc<Self>,
475 kernel_language: String,
476 kernel_status: String,
477 repl_session_id: String,
478 ) {
479 let event = Event::Repl(ReplEvent {
480 kernel_language,
481 kernel_status,
482 repl_session_id,
483 });
484
485 self.report_event(event)
486 }
487
488 fn report_event(self: &Arc<Self>, event: Event) {
489 let mut state = self.state.lock();
490
491 if !state.settings.metrics {
492 return;
493 }
494
495 if state.flush_events_task.is_none() {
496 let this = self.clone();
497 let executor = self.executor.clone();
498 state.flush_events_task = Some(self.executor.spawn(async move {
499 executor.timer(FLUSH_INTERVAL).await;
500 this.flush_events();
501 }));
502 }
503
504 let date_time = self.clock.utc_now();
505
506 let milliseconds_since_first_event = match state.first_event_date_time {
507 Some(first_event_date_time) => date_time
508 .saturating_duration_since(first_event_date_time)
509 .min(Duration::from_secs(60 * 60 * 24))
510 .as_millis() as i64,
511 None => {
512 state.first_event_date_time = Some(date_time);
513 0
514 }
515 };
516
517 let signed_in = state.metrics_id.is_some();
518 state.events_queue.push(EventWrapper {
519 signed_in,
520 milliseconds_since_first_event,
521 event,
522 });
523
524 if state.installation_id.is_some() && state.events_queue.len() >= state.max_queue_size {
525 drop(state);
526 self.flush_events();
527 }
528 }
529
530 pub fn metrics_id(self: &Arc<Self>) -> Option<Arc<str>> {
531 self.state.lock().metrics_id.clone()
532 }
533
534 pub fn installation_id(self: &Arc<Self>) -> Option<Arc<str>> {
535 self.state.lock().installation_id.clone()
536 }
537
538 pub fn is_staff(self: &Arc<Self>) -> Option<bool> {
539 self.state.lock().is_staff
540 }
541
542 fn build_request(
543 self: &Arc<Self>,
544 // We take in the JSON bytes buffer so we can reuse the existing allocation.
545 mut json_bytes: Vec<u8>,
546 event_request: EventRequestBody,
547 ) -> Result<Request<AsyncBody>> {
548 json_bytes.clear();
549 serde_json::to_writer(&mut json_bytes, &event_request)?;
550
551 let checksum = calculate_json_checksum(&json_bytes).unwrap_or("".to_string());
552
553 Ok(Request::builder()
554 .method(Method::POST)
555 .uri(
556 self.http_client
557 .build_zed_api_url("/telemetry/events", &[])?
558 .as_ref(),
559 )
560 .header("Content-Type", "application/json")
561 .header("x-zed-checksum", checksum)
562 .body(json_bytes.into())?)
563 }
564
565 pub fn flush_events(self: &Arc<Self>) {
566 let mut state = self.state.lock();
567 state.first_event_date_time = None;
568 let mut events = mem::take(&mut state.events_queue);
569 state.flush_events_task.take();
570 drop(state);
571 if events.is_empty() {
572 return;
573 }
574
575 let this = self.clone();
576 self.executor
577 .spawn(
578 async move {
579 let mut json_bytes = Vec::new();
580
581 if let Some(file) = &mut this.state.lock().log_file {
582 for event in &mut events {
583 json_bytes.clear();
584 serde_json::to_writer(&mut json_bytes, event)?;
585 file.write_all(&json_bytes)?;
586 file.write_all(b"\n")?;
587 }
588 }
589
590 let request_body = {
591 let state = this.state.lock();
592
593 EventRequestBody {
594 system_id: state.system_id.as_deref().map(Into::into),
595 installation_id: state.installation_id.as_deref().map(Into::into),
596 session_id: state.session_id.clone(),
597 metrics_id: state.metrics_id.as_deref().map(Into::into),
598 is_staff: state.is_staff,
599 app_version: state.app_version.clone(),
600 os_name: state.os_name.clone(),
601 os_version: state.os_version.clone(),
602 architecture: state.architecture.to_string(),
603
604 release_channel: state.release_channel.map(Into::into),
605 events,
606 }
607 };
608
609 let request = this.build_request(json_bytes, request_body)?;
610 let response = this.http_client.send(request).await?;
611 if response.status() != 200 {
612 log::error!("Failed to send events: HTTP {:?}", response.status());
613 }
614 anyhow::Ok(())
615 }
616 .log_err(),
617 )
618 .detach();
619 }
620}
621
622pub fn calculate_json_checksum(json: &impl AsRef<[u8]>) -> Option<String> {
623 let Some(checksum_seed) = &*ZED_CLIENT_CHECKSUM_SEED else {
624 return None;
625 };
626
627 let mut summer = Sha256::new();
628 summer.update(checksum_seed);
629 summer.update(json);
630 summer.update(checksum_seed);
631 let mut checksum = String::new();
632 for byte in summer.finalize().as_slice() {
633 use std::fmt::Write;
634 write!(&mut checksum, "{:02x}", byte).unwrap();
635 }
636
637 Some(checksum)
638}
639
640#[cfg(test)]
641mod tests {
642 use super::*;
643 use clock::FakeSystemClock;
644 use gpui::TestAppContext;
645 use http_client::FakeHttpClient;
646
647 #[gpui::test]
648 fn test_telemetry_flush_on_max_queue_size(cx: &mut TestAppContext) {
649 init_test(cx);
650 let clock = Arc::new(FakeSystemClock::new());
651 let http = FakeHttpClient::with_200_response();
652 let system_id = Some("system_id".to_string());
653 let installation_id = Some("installation_id".to_string());
654 let session_id = "session_id".to_string();
655
656 cx.update(|cx| {
657 let telemetry = Telemetry::new(clock.clone(), http, cx);
658
659 telemetry.state.lock().max_queue_size = 4;
660 telemetry.start(system_id, installation_id, session_id, cx);
661
662 assert!(is_empty_state(&telemetry));
663
664 let first_date_time = clock.utc_now();
665 let operation = "test".to_string();
666
667 let event = telemetry.report_app_event(operation.clone());
668 assert_eq!(
669 event,
670 Event::App(AppEvent {
671 operation: operation.clone(),
672 })
673 );
674 assert_eq!(telemetry.state.lock().events_queue.len(), 1);
675 assert!(telemetry.state.lock().flush_events_task.is_some());
676 assert_eq!(
677 telemetry.state.lock().first_event_date_time,
678 Some(first_date_time)
679 );
680
681 clock.advance(Duration::from_millis(100));
682
683 let event = telemetry.report_app_event(operation.clone());
684 assert_eq!(
685 event,
686 Event::App(AppEvent {
687 operation: operation.clone(),
688 })
689 );
690 assert_eq!(telemetry.state.lock().events_queue.len(), 2);
691 assert!(telemetry.state.lock().flush_events_task.is_some());
692 assert_eq!(
693 telemetry.state.lock().first_event_date_time,
694 Some(first_date_time)
695 );
696
697 clock.advance(Duration::from_millis(100));
698
699 let event = telemetry.report_app_event(operation.clone());
700 assert_eq!(
701 event,
702 Event::App(AppEvent {
703 operation: operation.clone(),
704 })
705 );
706 assert_eq!(telemetry.state.lock().events_queue.len(), 3);
707 assert!(telemetry.state.lock().flush_events_task.is_some());
708 assert_eq!(
709 telemetry.state.lock().first_event_date_time,
710 Some(first_date_time)
711 );
712
713 clock.advance(Duration::from_millis(100));
714
715 // Adding a 4th event should cause a flush
716 let event = telemetry.report_app_event(operation.clone());
717 assert_eq!(
718 event,
719 Event::App(AppEvent {
720 operation: operation.clone(),
721 })
722 );
723
724 assert!(is_empty_state(&telemetry));
725 });
726 }
727
728 #[gpui::test]
729 async fn test_telemetry_flush_on_flush_interval(
730 executor: BackgroundExecutor,
731 cx: &mut TestAppContext,
732 ) {
733 init_test(cx);
734 let clock = Arc::new(FakeSystemClock::new());
735 let http = FakeHttpClient::with_200_response();
736 let system_id = Some("system_id".to_string());
737 let installation_id = Some("installation_id".to_string());
738 let session_id = "session_id".to_string();
739
740 cx.update(|cx| {
741 let telemetry = Telemetry::new(clock.clone(), http, cx);
742 telemetry.state.lock().max_queue_size = 4;
743 telemetry.start(system_id, installation_id, session_id, cx);
744
745 assert!(is_empty_state(&telemetry));
746
747 let first_date_time = clock.utc_now();
748 let operation = "test".to_string();
749
750 let event = telemetry.report_app_event(operation.clone());
751 assert_eq!(
752 event,
753 Event::App(AppEvent {
754 operation: operation.clone(),
755 })
756 );
757 assert_eq!(telemetry.state.lock().events_queue.len(), 1);
758 assert!(telemetry.state.lock().flush_events_task.is_some());
759 assert_eq!(
760 telemetry.state.lock().first_event_date_time,
761 Some(first_date_time)
762 );
763
764 let duration = Duration::from_millis(1);
765
766 // Test 1 millisecond before the flush interval limit is met
767 executor.advance_clock(FLUSH_INTERVAL - duration);
768
769 assert!(!is_empty_state(&telemetry));
770
771 // Test the exact moment the flush interval limit is met
772 executor.advance_clock(duration);
773
774 assert!(is_empty_state(&telemetry));
775 });
776 }
777
778 // TODO:
779 // Test settings
780 // Update FakeHTTPClient to keep track of the number of requests and assert on it
781
782 fn init_test(cx: &mut TestAppContext) {
783 cx.update(|cx| {
784 let settings_store = SettingsStore::test(cx);
785 cx.set_global(settings_store);
786 });
787 }
788
789 fn is_empty_state(telemetry: &Telemetry) -> bool {
790 telemetry.state.lock().events_queue.is_empty()
791 && telemetry.state.lock().flush_events_task.is_none()
792 && telemetry.state.lock().first_event_date_time.is_none()
793 }
794}