This commit is contained in:
Joseph T. Lyons 2024-01-06 14:41:35 -05:00
parent 3d1f522566
commit cdd5cb16ed
14 changed files with 348 additions and 258 deletions

View file

@ -5,7 +5,7 @@ use gpui::{serde_json, AppContext, AppMetadata, BackgroundExecutor, Task};
use lazy_static::lazy_static;
use parking_lot::Mutex;
use serde::Serialize;
use settings::Settings;
use settings::{Settings, SettingsStore};
use std::{env, io::Write, mem, path::PathBuf, sync::Arc, time::Duration};
use sysinfo::{
CpuRefreshKind, Pid, PidExt, ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt,
@ -17,10 +17,11 @@ use util::{channel::ReleaseChannel, TryFutureExt};
pub struct Telemetry {
http_client: Arc<dyn HttpClient>,
executor: BackgroundExecutor,
state: Mutex<TelemetryState>,
state: Arc<Mutex<TelemetryState>>,
}
struct TelemetryState {
settings: TelemetrySettings,
metrics_id: Option<Arc<str>>, // Per logged-in user
installation_id: Option<Arc<str>>, // Per app installation (different for dev, nightly, preview, and stable)
session_id: Option<Arc<str>>, // Per app launch
@ -139,45 +140,60 @@ impl Telemetry {
None
};
TelemetrySettings::register(cx);
let state = Arc::new(Mutex::new(TelemetryState {
settings: TelemetrySettings::get_global(cx).clone(),
app_metadata: cx.app_metadata(),
architecture: env::consts::ARCH,
release_channel,
installation_id: None,
metrics_id: None,
session_id: None,
clickhouse_events_queue: Default::default(),
flush_clickhouse_events_task: Default::default(),
log_file: None,
is_staff: None,
first_event_datetime: None,
}));
cx.observe_global::<SettingsStore>({
let state = state.clone();
move |cx| {
let mut state = state.lock();
state.settings = TelemetrySettings::get_global(cx).clone();
}
})
.detach();
// TODO: Replace all hardware stuff with nested SystemSpecs json
let this = Arc::new(Self {
http_client: client,
executor: cx.background_executor().clone(),
state: Mutex::new(TelemetryState {
app_metadata: cx.app_metadata(),
architecture: env::consts::ARCH,
release_channel,
installation_id: None,
metrics_id: None,
session_id: None,
clickhouse_events_queue: Default::default(),
flush_clickhouse_events_task: Default::default(),
log_file: None,
is_staff: None,
first_event_datetime: None,
}),
state,
});
// We should only ever have one instance of Telemetry, leak the subscription to keep it alive
// rather than store in TelemetryState, complicating spawn as subscriptions are not Send
std::mem::forget(cx.on_app_quit({
let this = this.clone();
move |cx| this.shutdown_telemetry(cx)
move |_| this.shutdown_telemetry()
}));
this
}
#[cfg(any(test, feature = "test-support"))]
fn shutdown_telemetry(self: &Arc<Self>, _: &mut AppContext) -> impl Future<Output = ()> {
fn shutdown_telemetry(self: &Arc<Self>) -> impl Future<Output = ()> {
Task::ready(())
}
// Skip calling this function in tests.
// TestAppContext ends up calling this function on shutdown and it panics when trying to find the TelemetrySettings
#[cfg(not(any(test, feature = "test-support")))]
fn shutdown_telemetry(self: &Arc<Self>, cx: &mut AppContext) -> impl Future<Output = ()> {
self.report_app_event("close", true, cx);
fn shutdown_telemetry(self: &Arc<Self>) -> impl Future<Output = ()> {
self.report_app_event("close", true);
Task::ready(())
}
@ -197,7 +213,7 @@ impl Telemetry {
drop(state);
let this = self.clone();
cx.spawn(|cx| async move {
cx.spawn(|_cx| async move {
// Avoiding calling `System::new_all()`, as there have been crashes related to it
let refresh_kind = RefreshKind::new()
.with_memory() // For memory usage
@ -226,11 +242,8 @@ impl Telemetry {
return;
};
cx.update(|cx| {
this.report_memory_event(process.memory(), process.virtual_memory(), cx);
this.report_cpu_event(process.cpu_usage(), system.cpus().len() as u32, cx);
})
.ok();
this.report_memory_event(process.memory(), process.virtual_memory());
this.report_cpu_event(process.cpu_usage(), system.cpus().len() as u32);
}
})
.detach();
@ -240,13 +253,13 @@ impl Telemetry {
self: &Arc<Self>,
metrics_id: Option<String>,
is_staff: bool,
cx: &AppContext,
) {
if !TelemetrySettings::get_global(cx).metrics {
let mut state = self.state.lock();
if !state.settings.metrics {
return;
}
let mut state = self.state.lock();
let metrics_id: Option<Arc<str>> = metrics_id.map(|id| id.into());
state.metrics_id = metrics_id.clone();
state.is_staff = Some(is_staff);
@ -260,7 +273,6 @@ impl Telemetry {
operation: &'static str,
copilot_enabled: bool,
copilot_enabled_for_language: bool,
cx: &AppContext,
) {
let event = ClickhouseEvent::Editor {
file_extension,
@ -271,7 +283,7 @@ impl Telemetry {
milliseconds_since_first_event: self.milliseconds_since_first_event(),
};
self.report_clickhouse_event(event, false, cx)
self.report_clickhouse_event(event, false)
}
pub fn report_copilot_event(
@ -279,7 +291,6 @@ impl Telemetry {
suggestion_id: Option<String>,
suggestion_accepted: bool,
file_extension: Option<String>,
cx: &AppContext,
) {
let event = ClickhouseEvent::Copilot {
suggestion_id,
@ -288,7 +299,7 @@ impl Telemetry {
milliseconds_since_first_event: self.milliseconds_since_first_event(),
};
self.report_clickhouse_event(event, false, cx)
self.report_clickhouse_event(event, false)
}
pub fn report_assistant_event(
@ -296,7 +307,6 @@ impl Telemetry {
conversation_id: Option<String>,
kind: AssistantKind,
model: &'static str,
cx: &AppContext,
) {
let event = ClickhouseEvent::Assistant {
conversation_id,
@ -305,7 +315,7 @@ impl Telemetry {
milliseconds_since_first_event: self.milliseconds_since_first_event(),
};
self.report_clickhouse_event(event, false, cx)
self.report_clickhouse_event(event, false)
}
pub fn report_call_event(
@ -313,7 +323,6 @@ impl Telemetry {
operation: &'static str,
room_id: Option<u64>,
channel_id: Option<u64>,
cx: &AppContext,
) {
let event = ClickhouseEvent::Call {
operation,
@ -322,29 +331,23 @@ impl Telemetry {
milliseconds_since_first_event: self.milliseconds_since_first_event(),
};
self.report_clickhouse_event(event, false, cx)
self.report_clickhouse_event(event, false)
}
pub fn report_cpu_event(
self: &Arc<Self>,
usage_as_percentage: f32,
core_count: u32,
cx: &AppContext,
) {
pub fn report_cpu_event(self: &Arc<Self>, usage_as_percentage: f32, core_count: u32) {
let event = ClickhouseEvent::Cpu {
usage_as_percentage,
core_count,
milliseconds_since_first_event: self.milliseconds_since_first_event(),
};
self.report_clickhouse_event(event, false, cx)
self.report_clickhouse_event(event, false)
}
pub fn report_memory_event(
self: &Arc<Self>,
memory_in_bytes: u64,
virtual_memory_in_bytes: u64,
cx: &AppContext,
) {
let event = ClickhouseEvent::Memory {
memory_in_bytes,
@ -352,36 +355,26 @@ impl Telemetry {
milliseconds_since_first_event: self.milliseconds_since_first_event(),
};
self.report_clickhouse_event(event, false, cx)
self.report_clickhouse_event(event, false)
}
pub fn report_app_event(
self: &Arc<Self>,
operation: &'static str,
immediate_flush: bool,
cx: &AppContext,
) {
pub fn report_app_event(self: &Arc<Self>, operation: &'static str, immediate_flush: bool) {
let event = ClickhouseEvent::App {
operation,
milliseconds_since_first_event: self.milliseconds_since_first_event(),
};
self.report_clickhouse_event(event, immediate_flush, cx)
self.report_clickhouse_event(event, immediate_flush)
}
pub fn report_setting_event(
self: &Arc<Self>,
setting: &'static str,
value: String,
cx: &AppContext,
) {
pub fn report_setting_event(self: &Arc<Self>, setting: &'static str, value: String) {
let event = ClickhouseEvent::Setting {
setting,
value,
milliseconds_since_first_event: self.milliseconds_since_first_event(),
};
self.report_clickhouse_event(event, false, cx)
self.report_clickhouse_event(event, false)
}
fn milliseconds_since_first_event(&self) -> i64 {
@ -398,17 +391,13 @@ impl Telemetry {
}
}
fn report_clickhouse_event(
self: &Arc<Self>,
event: ClickhouseEvent,
immediate_flush: bool,
cx: &AppContext,
) {
if !TelemetrySettings::get_global(cx).metrics {
fn report_clickhouse_event(self: &Arc<Self>, event: ClickhouseEvent, immediate_flush: bool) {
let mut state = self.state.lock();
if !state.settings.metrics {
return;
}
let mut state = self.state.lock();
let signed_in = state.metrics_id.is_some();
state
.clickhouse_events_queue