Skip to content

Commit

Permalink
Add telemetry::event! (#22146)
Browse files Browse the repository at this point in the history
CC @JosephTLyons

Release Notes:

- N/A
  • Loading branch information
ConradIrwin authored Dec 17, 2024
1 parent b17f208 commit 7425d24
Show file tree
Hide file tree
Showing 24 changed files with 179 additions and 140 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ members = [
"crates/tab_switcher",
"crates/task",
"crates/tasks_ui",
"crates/telemetry",
"crates/telemetry_events",
"crates/terminal",
"crates/terminal_view",
Expand Down Expand Up @@ -305,6 +306,7 @@ supermaven_api = { path = "crates/supermaven_api" }
tab_switcher = { path = "crates/tab_switcher" }
task = { path = "crates/task" }
tasks_ui = { path = "crates/tasks_ui" }
telemetry = { path = "crates/telemetry" }
telemetry_events = { path = "crates/telemetry_events" }
terminal = { path = "crates/terminal" }
terminal_view = { path = "crates/terminal_view" }
Expand Down
1 change: 1 addition & 0 deletions crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ tokio-socks = { version = "0.5.2", default-features = false, features = ["future
url.workspace = true
util.workspace = true
worktree.workspace = true
telemetry.workspace = true

[dev-dependencies]
clock = { workspace = true, features = ["test-support"] }
Expand Down
76 changes: 19 additions & 57 deletions crates/client/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::{ChannelId, TelemetrySettings};
use anyhow::Result;
use clock::SystemClock;
use collections::{HashMap, HashSet};
use futures::Future;
use futures::channel::mpsc;
use futures::{Future, StreamExt};
use gpui::{AppContext, BackgroundExecutor, Task};
use http_client::{self, AsyncBody, HttpClient, HttpClientWithUrl, Method, Request};
use once_cell::sync::Lazy;
Expand All @@ -17,9 +18,8 @@ use std::io::Write;
use std::time::Instant;
use std::{env, mem, path::PathBuf, sync::Arc, time::Duration};
use telemetry_events::{
ActionEvent, AppEvent, AssistantEvent, CallEvent, EditEvent, EditorEvent, Event,
EventRequestBody, EventWrapper, ExtensionEvent, InlineCompletionEvent, InlineCompletionRating,
InlineCompletionRatingEvent, ReplEvent, SettingEvent,
AppEvent, AssistantEvent, CallEvent, EditEvent, Event, EventRequestBody, EventWrapper,
InlineCompletionEvent, InlineCompletionRating, InlineCompletionRatingEvent, SettingEvent,
};
use util::{ResultExt, TryFutureExt};
use worktree::{UpdatedEntriesSet, WorktreeId};
Expand Down Expand Up @@ -245,14 +245,28 @@ impl Telemetry {
})
.detach();

// TODO: Replace all hardware stuff with nested SystemSpecs json
let this = Arc::new(Self {
clock,
http_client: client,
executor: cx.background_executor().clone(),
state,
});

let (tx, mut rx) = mpsc::unbounded();
::telemetry::init(tx);

cx.background_executor()
.spawn({
let this = Arc::downgrade(&this);
async move {
while let Some(event) = rx.next().await {
let Some(state) = this.upgrade() else { break };
state.report_event(Event::Flexible(event))
}
}
})
.detach();

// 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({
Expand Down Expand Up @@ -320,27 +334,6 @@ impl Telemetry {
drop(state);
}

pub fn report_editor_event(
self: &Arc<Self>,
file_extension: Option<String>,
vim_mode: bool,
operation: &'static str,
copilot_enabled: bool,
copilot_enabled_for_language: bool,
is_via_ssh: bool,
) {
let event = Event::Editor(EditorEvent {
file_extension,
vim_mode,
operation: operation.into(),
copilot_enabled,
copilot_enabled_for_language,
is_via_ssh,
});

self.report_event(event)
}

pub fn report_inline_completion_event(
self: &Arc<Self>,
provider: String,
Expand Down Expand Up @@ -410,13 +403,6 @@ impl Telemetry {
self.report_event(event)
}

pub fn report_extension_event(self: &Arc<Self>, extension_id: Arc<str>, version: Arc<str>) {
self.report_event(Event::Extension(ExtensionEvent {
extension_id,
version,
}))
}

pub fn log_edit_event(self: &Arc<Self>, environment: &'static str, is_via_ssh: bool) {
let mut state = self.state.lock();
let period_data = state.event_coalescer.log_event(environment);
Expand All @@ -436,15 +422,6 @@ impl Telemetry {
}
}

pub fn report_action_event(self: &Arc<Self>, source: &'static str, action: String) {
let event = Event::Action(ActionEvent {
source: source.to_string(),
action,
});

self.report_event(event)
}

pub fn report_discovered_project_events(
self: &Arc<Self>,
worktree_id: WorktreeId,
Expand Down Expand Up @@ -491,21 +468,6 @@ impl Telemetry {
}
}

pub fn report_repl_event(
self: &Arc<Self>,
kernel_language: String,
kernel_status: String,
repl_session_id: String,
) {
let event = Event::Repl(ReplEvent {
kernel_language,
kernel_status,
repl_session_id,
});

self.report_event(event)
}

fn report_event(self: &Arc<Self>, event: Event) {
let mut state = self.state.lock();

Expand Down
4 changes: 4 additions & 0 deletions crates/collab/src/api/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,10 @@ fn for_snowflake(
"Kernel Status Changed".to_string(),
serde_json::to_value(e).unwrap(),
),
Event::Flexible(e) => (
e.event_type.clone(),
serde_json::to_value(&e.event_properties).unwrap(),
),
};

if let serde_json::Value::Object(ref mut map) = event_properties {
Expand Down
1 change: 1 addition & 0 deletions crates/command_palette/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ settings.workspace = true
theme.workspace = true
ui.workspace = true
util.workspace = true
telemetry.workspace = true
workspace.workspace = true
zed_actions.workspace = true

Expand Down
31 changes: 10 additions & 21 deletions crates/command_palette/src/command_palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
time::Duration,
};

use client::{parse_zed_link, telemetry::Telemetry};
use client::parse_zed_link;
use collections::HashMap;
use command_palette_hooks::{
CommandInterceptResult, CommandPaletteFilter, CommandPaletteInterceptor,
Expand Down Expand Up @@ -63,18 +63,12 @@ impl CommandPalette {
let Some(previous_focus_handle) = cx.focused() else {
return;
};
let telemetry = workspace.client().telemetry().clone();
workspace.toggle_modal(cx, move |cx| {
CommandPalette::new(previous_focus_handle, telemetry, query, cx)
CommandPalette::new(previous_focus_handle, query, cx)
});
}

fn new(
previous_focus_handle: FocusHandle,
telemetry: Arc<Telemetry>,
query: &str,
cx: &mut ViewContext<Self>,
) -> Self {
fn new(previous_focus_handle: FocusHandle, query: &str, cx: &mut ViewContext<Self>) -> Self {
let filter = CommandPaletteFilter::try_global(cx);

let commands = cx
Expand All @@ -92,12 +86,8 @@ impl CommandPalette {
})
.collect();

let delegate = CommandPaletteDelegate::new(
cx.view().downgrade(),
commands,
telemetry,
previous_focus_handle,
);
let delegate =
CommandPaletteDelegate::new(cx.view().downgrade(), commands, previous_focus_handle);

let picker = cx.new_view(|cx| {
let picker = Picker::uniform_list(delegate, cx);
Expand Down Expand Up @@ -133,7 +123,6 @@ pub struct CommandPaletteDelegate {
commands: Vec<Command>,
matches: Vec<StringMatch>,
selected_ix: usize,
telemetry: Arc<Telemetry>,
previous_focus_handle: FocusHandle,
updating_matches: Option<(
Task<()>,
Expand Down Expand Up @@ -167,7 +156,6 @@ impl CommandPaletteDelegate {
fn new(
command_palette: WeakView<CommandPalette>,
commands: Vec<Command>,
telemetry: Arc<Telemetry>,
previous_focus_handle: FocusHandle,
) -> Self {
Self {
Expand All @@ -176,7 +164,6 @@ impl CommandPaletteDelegate {
matches: vec![],
commands,
selected_ix: 0,
telemetry,
previous_focus_handle,
updating_matches: None,
}
Expand Down Expand Up @@ -367,9 +354,11 @@ impl PickerDelegate for CommandPaletteDelegate {
let action_ix = self.matches[self.selected_ix].candidate_id;
let command = self.commands.swap_remove(action_ix);

self.telemetry
.report_action_event("command palette", command.name.clone());

telemetry::event!(
"Action Invoked",
source = "command palette",
action = command.name
);
self.matches.clear();
self.commands.clear();
HitCounts::update_global(cx, |hit_counts, _cx| {
Expand Down
1 change: 1 addition & 0 deletions crates/editor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ smol.workspace = true
snippet.workspace = true
sum_tree.workspace = true
task.workspace = true
telemetry.workspace = true
text.workspace = true
time.workspace = true
time_format.workspace = true
Expand Down
13 changes: 6 additions & 7 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ impl Editor {
}
}

this.report_editor_event("open", None, cx);
this.report_editor_event("Editor Opened", None, cx);
this
}

Expand Down Expand Up @@ -12568,7 +12568,7 @@ impl Editor {

fn report_editor_event(
&self,
operation: &'static str,
event_type: &'static str,
file_extension: Option<String>,
cx: &AppContext,
) {
Expand Down Expand Up @@ -12605,15 +12605,14 @@ impl Editor {
.show_inline_completions;

let project = project.read(cx);
let telemetry = project.client().telemetry().clone();
telemetry.report_editor_event(
telemetry::event!(
event_type,
file_extension,
vim_mode,
operation,
copilot_enabled,
copilot_enabled_for_language,
project.is_via_ssh(),
)
is_via_ssh = project.is_via_ssh(),
);
}

/// Copy the highlighted chunks to the clipboard as JSON. The format is an array of lines,
Expand Down
4 changes: 2 additions & 2 deletions crates/editor/src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ impl Item for Editor {
project: Model<Project>,
cx: &mut ViewContext<Self>,
) -> Task<Result<()>> {
self.report_editor_event("save", None, cx);
self.report_editor_event("Editor Saved", None, cx);
let buffers = self.buffer().clone().read(cx).all_buffers();
let buffers = buffers
.into_iter()
Expand Down Expand Up @@ -805,7 +805,7 @@ impl Item for Editor {
.path
.extension()
.map(|a| a.to_string_lossy().to_string());
self.report_editor_event("save", file_extension, cx);
self.report_editor_event("Editor Saved", file_extension, cx);

project.update(cx, |project, cx| project.save_buffer_as(buffer, path, cx))
}
Expand Down
1 change: 1 addition & 0 deletions crates/extension_host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ serde_json.workspace = true
serde_json_lenient.workspace = true
settings.workspace = true
task.workspace = true
telemetry.workspace = true
tempfile.workspace = true
toml.workspace = true
url.workspace = true
Expand Down
Loading

0 comments on commit 7425d24

Please sign in to comment.