Move telemetry settings check into telemetry module

Joseph T. Lyons created

Change summary

crates/assistant/src/assistant_panel.rs     | 11 ---
crates/call/src/call.rs                     |  9 +--
crates/client/src/telemetry.rs              | 62 ++++++++--------------
crates/editor/src/editor.rs                 | 13 +---
crates/theme_selector/src/theme_selector.rs |  7 +-
crates/workspace/src/workspace.rs           |  5 -
crates/zed/src/main.rs                      | 11 +--
7 files changed, 41 insertions(+), 77 deletions(-)

Detailed changes

crates/assistant/src/assistant_panel.rs 🔗

@@ -16,7 +16,7 @@ use ai::{
 use ai::prompts::repository_context::PromptCodeSnippet;
 use anyhow::{anyhow, Result};
 use chrono::{DateTime, Local};
-use client::{telemetry::AssistantKind, TelemetrySettings};
+use client::telemetry::AssistantKind;
 use collections::{hash_map, HashMap, HashSet, VecDeque};
 use editor::{
     display_map::{
@@ -3527,12 +3527,5 @@ fn report_assistant_event(
         .default_open_ai_model
         .clone();
 
-    let telemetry_settings = TelemetrySettings::get_global(cx).clone();
-
-    telemetry.report_assistant_event(
-        telemetry_settings,
-        conversation_id,
-        assistant_kind,
-        model.full_name(),
-    )
+    telemetry.report_assistant_event(conversation_id, assistant_kind, model.full_name(), cx)
 }

crates/call/src/call.rs 🔗

@@ -5,7 +5,7 @@ pub mod room;
 use anyhow::{anyhow, Result};
 use audio::Audio;
 use call_settings::CallSettings;
-use client::{proto, Client, TelemetrySettings, TypedEnvelope, User, UserStore, ZED_ALWAYS_ACTIVE};
+use client::{proto, Client, TypedEnvelope, User, UserStore, ZED_ALWAYS_ACTIVE};
 use collections::HashSet;
 use futures::{channel::oneshot, future::Shared, Future, FutureExt};
 use gpui::{
@@ -480,9 +480,8 @@ pub fn report_call_event_for_room(
     cx: &mut AppContext,
 ) {
     let telemetry = client.telemetry();
-    let telemetry_settings = *TelemetrySettings::get_global(cx);
 
-    telemetry.report_call_event(telemetry_settings, operation, Some(room_id), channel_id)
+    telemetry.report_call_event(operation, Some(room_id), channel_id, cx)
 }
 
 pub fn report_call_event_for_channel(
@@ -495,13 +494,11 @@ pub fn report_call_event_for_channel(
 
     let telemetry = client.telemetry();
 
-    let telemetry_settings = *TelemetrySettings::get_global(cx);
-
     telemetry.report_call_event(
-        telemetry_settings,
         operation,
         room.map(|r| r.read(cx).id()),
         Some(channel_id),
+        cx,
     )
 }
 

crates/client/src/telemetry.rs 🔗

@@ -177,8 +177,7 @@ impl Telemetry {
     // 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 = ()> {
-        let telemetry_settings = TelemetrySettings::get_global(cx).clone();
-        self.report_app_event(telemetry_settings, "close", true);
+        self.report_app_event("close", true, cx);
         Task::ready(())
     }
 
@@ -227,24 +226,11 @@ impl Telemetry {
                     return;
                 };
 
-                let telemetry_settings = if let Ok(telemetry_settings) =
-                    cx.update(|cx| *TelemetrySettings::get_global(cx))
-                {
-                    telemetry_settings
-                } else {
-                    break;
-                };
-
-                this.report_memory_event(
-                    telemetry_settings,
-                    process.memory(),
-                    process.virtual_memory(),
-                );
-                this.report_cpu_event(
-                    telemetry_settings,
-                    process.cpu_usage(),
-                    system.cpus().len() as u32,
-                );
+                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();
             }
         })
         .detach();
@@ -269,12 +255,12 @@ impl Telemetry {
 
     pub fn report_editor_event(
         self: &Arc<Self>,
-        telemetry_settings: TelemetrySettings,
         file_extension: Option<String>,
         vim_mode: bool,
         operation: &'static str,
         copilot_enabled: bool,
         copilot_enabled_for_language: bool,
+        cx: &AppContext,
     ) {
         let event = ClickhouseEvent::Editor {
             file_extension,
@@ -285,15 +271,15 @@ impl Telemetry {
             milliseconds_since_first_event: self.milliseconds_since_first_event(),
         };
 
-        self.report_clickhouse_event(event, telemetry_settings, false)
+        self.report_clickhouse_event(event, false, cx)
     }
 
     pub fn report_copilot_event(
         self: &Arc<Self>,
-        telemetry_settings: TelemetrySettings,
         suggestion_id: Option<String>,
         suggestion_accepted: bool,
         file_extension: Option<String>,
+        cx: &AppContext,
     ) {
         let event = ClickhouseEvent::Copilot {
             suggestion_id,
@@ -302,15 +288,15 @@ impl Telemetry {
             milliseconds_since_first_event: self.milliseconds_since_first_event(),
         };
 
-        self.report_clickhouse_event(event, telemetry_settings, false)
+        self.report_clickhouse_event(event, false, cx)
     }
 
     pub fn report_assistant_event(
         self: &Arc<Self>,
-        telemetry_settings: TelemetrySettings,
         conversation_id: Option<String>,
         kind: AssistantKind,
         model: &'static str,
+        cx: &AppContext,
     ) {
         let event = ClickhouseEvent::Assistant {
             conversation_id,
@@ -319,15 +305,15 @@ impl Telemetry {
             milliseconds_since_first_event: self.milliseconds_since_first_event(),
         };
 
-        self.report_clickhouse_event(event, telemetry_settings, false)
+        self.report_clickhouse_event(event, false, cx)
     }
 
     pub fn report_call_event(
         self: &Arc<Self>,
-        telemetry_settings: TelemetrySettings,
         operation: &'static str,
         room_id: Option<u64>,
         channel_id: Option<u64>,
+        cx: &AppContext,
     ) {
         let event = ClickhouseEvent::Call {
             operation,
@@ -336,14 +322,14 @@ impl Telemetry {
             milliseconds_since_first_event: self.milliseconds_since_first_event(),
         };
 
-        self.report_clickhouse_event(event, telemetry_settings, false)
+        self.report_clickhouse_event(event, false, cx)
     }
 
     pub fn report_cpu_event(
         self: &Arc<Self>,
-        telemetry_settings: TelemetrySettings,
         usage_as_percentage: f32,
         core_count: u32,
+        cx: &AppContext,
     ) {
         let event = ClickhouseEvent::Cpu {
             usage_as_percentage,
@@ -351,14 +337,14 @@ impl Telemetry {
             milliseconds_since_first_event: self.milliseconds_since_first_event(),
         };
 
-        self.report_clickhouse_event(event, telemetry_settings, false)
+        self.report_clickhouse_event(event, false, cx)
     }
 
     pub fn report_memory_event(
         self: &Arc<Self>,
-        telemetry_settings: TelemetrySettings,
         memory_in_bytes: u64,
         virtual_memory_in_bytes: u64,
+        cx: &AppContext,
     ) {
         let event = ClickhouseEvent::Memory {
             memory_in_bytes,
@@ -366,28 +352,28 @@ impl Telemetry {
             milliseconds_since_first_event: self.milliseconds_since_first_event(),
         };
 
-        self.report_clickhouse_event(event, telemetry_settings, false)
+        self.report_clickhouse_event(event, false, cx)
     }
 
     pub fn report_app_event(
         self: &Arc<Self>,
-        telemetry_settings: TelemetrySettings,
         operation: &'static str,
         immediate_flush: bool,
+        cx: &AppContext,
     ) {
         let event = ClickhouseEvent::App {
             operation,
             milliseconds_since_first_event: self.milliseconds_since_first_event(),
         };
 
-        self.report_clickhouse_event(event, telemetry_settings, immediate_flush)
+        self.report_clickhouse_event(event, immediate_flush, cx)
     }
 
     pub fn report_setting_event(
         self: &Arc<Self>,
-        telemetry_settings: TelemetrySettings,
         setting: &'static str,
         value: String,
+        cx: &AppContext,
     ) {
         let event = ClickhouseEvent::Setting {
             setting,
@@ -395,7 +381,7 @@ impl Telemetry {
             milliseconds_since_first_event: self.milliseconds_since_first_event(),
         };
 
-        self.report_clickhouse_event(event, telemetry_settings, false)
+        self.report_clickhouse_event(event, false, cx)
     }
 
     fn milliseconds_since_first_event(&self) -> i64 {
@@ -415,10 +401,10 @@ impl Telemetry {
     fn report_clickhouse_event(
         self: &Arc<Self>,
         event: ClickhouseEvent,
-        telemetry_settings: TelemetrySettings,
         immediate_flush: bool,
+        cx: &AppContext,
     ) {
-        if !telemetry_settings.metrics {
+        if !TelemetrySettings::get_global(cx).metrics {
             return;
         }
 

crates/editor/src/editor.rs 🔗

@@ -24,7 +24,7 @@ use ::git::diff::DiffHunk;
 use aho_corasick::AhoCorasick;
 use anyhow::{anyhow, Context as _, Result};
 use blink_manager::BlinkManager;
-use client::{Client, Collaborator, ParticipantIndex, TelemetrySettings};
+use client::{Client, Collaborator, ParticipantIndex};
 use clock::ReplicaId;
 use collections::{BTreeMap, Bound, HashMap, HashSet, VecDeque};
 use convert_case::{Case, Casing};
@@ -8864,14 +8864,8 @@ impl Editor {
             .map(|a| a.to_string());
 
         let telemetry = project.read(cx).client().telemetry().clone();
-        let telemetry_settings = *TelemetrySettings::get_global(cx);
 
-        telemetry.report_copilot_event(
-            telemetry_settings,
-            suggestion_id,
-            suggestion_accepted,
-            file_extension,
-        )
+        telemetry.report_copilot_event(suggestion_id, suggestion_accepted, file_extension, cx)
     }
 
     #[cfg(any(test, feature = "test-support"))]
@@ -8909,7 +8903,6 @@ impl Editor {
             .raw_user_settings()
             .get("vim_mode")
             == Some(&serde_json::Value::Bool(true));
-        let telemetry_settings = *TelemetrySettings::get_global(cx);
         let copilot_enabled = all_language_settings(file, cx).copilot_enabled(None, None);
         let copilot_enabled_for_language = self
             .buffer
@@ -8919,12 +8912,12 @@ impl Editor {
 
         let telemetry = project.read(cx).client().telemetry().clone();
         telemetry.report_editor_event(
-            telemetry_settings,
             file_extension,
             vim_mode,
             operation,
             copilot_enabled,
             copilot_enabled_for_language,
+            cx,
         )
     }
 

crates/theme_selector/src/theme_selector.rs 🔗

@@ -1,4 +1,4 @@
-use client::{telemetry::Telemetry, TelemetrySettings};
+use client::telemetry::Telemetry;
 use feature_flags::FeatureFlagAppExt;
 use fs::Fs;
 use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
@@ -7,7 +7,7 @@ use gpui::{
     VisualContext, WeakView,
 };
 use picker::{Picker, PickerDelegate};
-use settings::{update_settings_file, Settings, SettingsStore};
+use settings::{update_settings_file, SettingsStore};
 use std::sync::Arc;
 use theme::{Theme, ThemeMeta, ThemeRegistry, ThemeSettings};
 use ui::{prelude::*, v_stack, ListItem, ListItemSpacing};
@@ -181,9 +181,8 @@ impl PickerDelegate for ThemeSelectorDelegate {
 
         let theme_name = cx.theme().name.clone();
 
-        let telemetry_settings = TelemetrySettings::get_global(cx).clone();
         self.telemetry
-            .report_setting_event(telemetry_settings, "theme", theme_name.to_string());
+            .report_setting_event("theme", theme_name.to_string(), cx);
 
         update_settings_file::<ThemeSettings>(self.fs.clone(), cx, move |settings| {
             settings.theme = Some(theme_name.to_string());

crates/workspace/src/workspace.rs 🔗

@@ -15,7 +15,7 @@ use anyhow::{anyhow, Context as _, Result};
 use call::ActiveCall;
 use client::{
     proto::{self, PeerId},
-    Client, Status, TelemetrySettings, TypedEnvelope, UserStore,
+    Client, Status, TypedEnvelope, UserStore,
 };
 use collections::{hash_map, HashMap, HashSet};
 use dock::{Dock, DockPosition, Panel, PanelButtons, PanelHandle};
@@ -1250,10 +1250,9 @@ impl Workspace {
     }
 
     pub fn open(&mut self, _: &Open, cx: &mut ViewContext<Self>) {
-        let telemetry_settings = TelemetrySettings::get_global(cx).clone();
         self.client()
             .telemetry()
-            .report_app_event(telemetry_settings, "open project", false);
+            .report_app_event("open project", false, cx);
         let paths = cx.prompt_for_paths(PathPromptOptions {
             files: true,
             directories: true,

crates/zed/src/main.rs 🔗

@@ -172,19 +172,16 @@ fn main() {
         .detach();
 
         client.telemetry().start(installation_id, session_id, cx);
-        let telemetry_settings = *client::TelemetrySettings::get_global(cx);
-        client.telemetry().report_setting_event(
-            telemetry_settings,
-            "theme",
-            cx.theme().name.to_string(),
-        );
+        client
+            .telemetry()
+            .report_setting_event("theme", cx.theme().name.to_string(), cx);
         let event_operation = match existing_installation_id_found {
             Some(false) => "first open",
             _ => "open",
         };
         client
             .telemetry()
-            .report_app_event(telemetry_settings, event_operation, true);
+            .report_app_event(event_operation, true, cx);
 
         let app_state = Arc::new(AppState {
             languages: languages.clone(),