1use crate::{TelemetrySettings, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL};
2use gpui::{executor::Background, serde_json, AppContext, 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: Arc<Background>,
15 state: Mutex<TelemetryState>,
16}
17
18#[derive(Default)]
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 app_version: Option<Arc<str>>,
24 release_channel: Option<&'static str>,
25 os_name: &'static str,
26 os_version: Option<Arc<str>>,
27 architecture: &'static str,
28 clickhouse_events_queue: Vec<ClickhouseEventWrapper>,
29 flush_clickhouse_events_task: Option<Task<()>>,
30 log_file: Option<NamedTempFile>,
31 is_staff: Option<bool>,
32}
33
34const CLICKHOUSE_EVENTS_URL_PATH: &'static str = "/api/events";
35
36lazy_static! {
37 static ref CLICKHOUSE_EVENTS_URL: String =
38 format!("{}{}", *ZED_SERVER_URL, CLICKHOUSE_EVENTS_URL_PATH);
39}
40
41#[derive(Serialize, Debug)]
42struct ClickhouseEventRequestBody {
43 token: &'static str,
44 installation_id: Option<Arc<str>>,
45 session_id: Option<Arc<str>>,
46 is_staff: Option<bool>,
47 app_version: Option<Arc<str>>,
48 os_name: &'static str,
49 os_version: Option<Arc<str>>,
50 architecture: &'static str,
51 release_channel: Option<&'static str>,
52 events: Vec<ClickhouseEventWrapper>,
53}
54
55#[derive(Serialize, Debug)]
56struct ClickhouseEventWrapper {
57 signed_in: bool,
58 #[serde(flatten)]
59 event: ClickhouseEvent,
60}
61
62#[derive(Serialize, Debug)]
63#[serde(rename_all = "snake_case")]
64pub enum AssistantKind {
65 Panel,
66 Inline,
67}
68
69#[derive(Serialize, Debug)]
70#[serde(tag = "type")]
71pub enum ClickhouseEvent {
72 Editor {
73 operation: &'static str,
74 file_extension: Option<String>,
75 vim_mode: bool,
76 copilot_enabled: bool,
77 copilot_enabled_for_language: bool,
78 },
79 Copilot {
80 suggestion_id: Option<String>,
81 suggestion_accepted: bool,
82 file_extension: Option<String>,
83 },
84 Call {
85 operation: &'static str,
86 room_id: Option<u64>,
87 channel_id: Option<u64>,
88 },
89 Assistant {
90 conversation_id: Option<String>,
91 kind: AssistantKind,
92 model: &'static str,
93 },
94 Cpu {
95 usage_as_percentage: f32,
96 core_count: u32,
97 },
98 Memory {
99 memory_in_bytes: u64,
100 virtual_memory_in_bytes: u64,
101 },
102}
103
104#[cfg(debug_assertions)]
105const MAX_QUEUE_LEN: usize = 1;
106
107#[cfg(not(debug_assertions))]
108const MAX_QUEUE_LEN: usize = 10;
109
110#[cfg(debug_assertions)]
111const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(1);
112
113#[cfg(not(debug_assertions))]
114const DEBOUNCE_INTERVAL: Duration = Duration::from_secs(30);
115
116impl Telemetry {
117 pub fn new(client: Arc<dyn HttpClient>, cx: &AppContext) -> Arc<Self> {
118 let platform = cx.platform();
119 let release_channel = if cx.has_global::<ReleaseChannel>() {
120 Some(cx.global::<ReleaseChannel>().display_name())
121 } else {
122 None
123 };
124 // TODO: Replace all hardware stuff with nested SystemSpecs json
125 let this = Arc::new(Self {
126 http_client: client,
127 executor: cx.background().clone(),
128 state: Mutex::new(TelemetryState {
129 os_name: platform.os_name().into(),
130 os_version: platform.os_version().ok().map(|v| v.to_string().into()),
131 architecture: env::consts::ARCH,
132 app_version: platform.app_version().ok().map(|v| v.to_string().into()),
133 release_channel,
134 installation_id: None,
135 metrics_id: None,
136 session_id: None,
137 clickhouse_events_queue: Default::default(),
138 flush_clickhouse_events_task: Default::default(),
139 log_file: None,
140 is_staff: None,
141 }),
142 });
143
144 this
145 }
146
147 pub fn log_file_path(&self) -> Option<PathBuf> {
148 Some(self.state.lock().log_file.as_ref()?.path().to_path_buf())
149 }
150
151 pub fn start(
152 self: &Arc<Self>,
153 installation_id: Option<String>,
154 session_id: String,
155 cx: &mut AppContext,
156 ) {
157 let mut state = self.state.lock();
158 state.installation_id = installation_id.map(|id| id.into());
159 state.session_id = Some(session_id.into());
160 let has_clickhouse_events = !state.clickhouse_events_queue.is_empty();
161 drop(state);
162
163 if has_clickhouse_events {
164 self.flush_clickhouse_events();
165 }
166
167 let this = self.clone();
168 cx.spawn(|mut cx| async move {
169 let mut system = System::new_all();
170 system.refresh_all();
171
172 loop {
173 // Waiting some amount of time before the first query is important to get a reasonable value
174 // https://docs.rs/sysinfo/0.29.10/sysinfo/trait.ProcessExt.html#tymethod.cpu_usage
175 const DURATION_BETWEEN_SYSTEM_EVENTS: Duration = Duration::from_secs(60);
176 smol::Timer::after(DURATION_BETWEEN_SYSTEM_EVENTS).await;
177
178 system.refresh_memory();
179 system.refresh_processes();
180
181 let current_process = Pid::from_u32(std::process::id());
182 let Some(process) = system.processes().get(¤t_process) else {
183 let process = current_process;
184 log::error!("Failed to find own process {process:?} in system process table");
185 // TODO: Fire an error telemetry event
186 return;
187 };
188
189 let memory_event = ClickhouseEvent::Memory {
190 memory_in_bytes: process.memory(),
191 virtual_memory_in_bytes: process.virtual_memory(),
192 };
193
194 let cpu_event = ClickhouseEvent::Cpu {
195 usage_as_percentage: process.cpu_usage(),
196 core_count: system.cpus().len() as u32,
197 };
198
199 let telemetry_settings = cx.update(|cx| *settings::get::<TelemetrySettings>(cx));
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 !settings::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.app_version.clone(),
297 os_name: state.os_name,
298 os_version: state.os_version.clone(),
299 architecture: state.architecture,
300
301 release_channel: state.release_channel,
302 events,
303 };
304 json_bytes.clear();
305 serde_json::to_writer(&mut json_bytes, &request_body)?;
306 }
307
308 this.http_client
309 .post_json(CLICKHOUSE_EVENTS_URL.as_str(), json_bytes.into())
310 .await?;
311 anyhow::Ok(())
312 }
313 .log_err(),
314 )
315 .detach();
316 }
317}