Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate more events to telemetry::event! macro #24102

Merged
merged 17 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

104 changes: 40 additions & 64 deletions crates/client/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod event_coalescer;
use crate::TelemetrySettings;
use anyhow::Result;
use clock::SystemClock;
use collections::{HashMap, HashSet};
use futures::channel::mpsc;
use futures::{Future, StreamExt};
use gpui::{App, BackgroundExecutor, Task};
Expand All @@ -12,14 +11,13 @@ use parking_lot::Mutex;
use release_channel::ReleaseChannel;
use settings::{Settings, SettingsStore};
use sha2::{Digest, Sha256};
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::Write;
use std::sync::LazyLock;
use std::time::Instant;
use std::{env, mem, path::PathBuf, sync::Arc, time::Duration};
use telemetry_events::{
AppEvent, AssistantEvent, AssistantPhase, EditEvent, Event, EventRequestBody, EventWrapper,
};
use telemetry_events::{AssistantEvent, AssistantPhase, Event, EventRequestBody, EventWrapper};
use util::{ResultExt, TryFutureExt};
use worktree::{UpdatedEntriesSet, WorktreeId};

Expand Down Expand Up @@ -285,7 +283,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>) -> impl Future<Output = ()> {
self.report_app_event("close".to_string());
telemetry::event!("App Closed");
// TODO: close final edit period and make sure it's sent
Task::ready(())
}
Expand Down Expand Up @@ -355,30 +353,23 @@ impl Telemetry {
);
}

pub fn report_app_event(self: &Arc<Self>, operation: String) -> Event {
let event = Event::App(AppEvent { operation });

self.report_event(event.clone());

event
}

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);
drop(state);

if let Some((start, end, environment)) = period_data {
let event = Event::Edit(EditEvent {
duration: end
.saturating_duration_since(start)
.min(Duration::from_secs(60 * 60 * 24))
.as_millis() as i64,
environment: environment.to_string(),
is_via_ssh,
});

self.report_event(event);
let duration_milliseconds = end
.saturating_duration_since(start)
.min(Duration::from_secs(60 * 60 * 24))
.as_millis() as i64;

telemetry::event!(
"Editor Edited",
duration_milliseconds = duration_milliseconds,
environment = environment.to_string(),
is_via_ssh = is_via_ssh
);
}
}

Expand Down Expand Up @@ -422,9 +413,8 @@ impl Telemetry {
.collect()
};

// Done on purpose to avoid calling `self.state.lock()` multiple times
for project_type_name in project_type_names {
self.report_app_event(format!("open {} project", project_type_name));
telemetry::event!("Project Opened", project_type = project_type_name);
}
}

Expand Down Expand Up @@ -590,6 +580,7 @@ mod tests {
use clock::FakeSystemClock;
use gpui::TestAppContext;
use http_client::FakeHttpClient;
use telemetry_events::FlexibleEvent;

#[gpui::test]
fn test_telemetry_flush_on_max_queue_size(cx: &mut TestAppContext) {
Expand All @@ -609,15 +600,17 @@ mod tests {
assert!(is_empty_state(&telemetry));

let first_date_time = clock.utc_now();
let operation = "test".to_string();
let event_properties = HashMap::from_iter([(
"test_key".to_string(),
serde_json::Value::String("test_value".to_string()),
)]);

let event = telemetry.report_app_event(operation.clone());
assert_eq!(
event,
Event::App(AppEvent {
operation: operation.clone(),
})
);
let event = FlexibleEvent {
event_type: "test".to_string(),
event_properties,
};

telemetry.report_event(Event::Flexible(event.clone()));
assert_eq!(telemetry.state.lock().events_queue.len(), 1);
assert!(telemetry.state.lock().flush_events_task.is_some());
assert_eq!(
Expand All @@ -627,13 +620,7 @@ mod tests {

clock.advance(Duration::from_millis(100));

let event = telemetry.report_app_event(operation.clone());
assert_eq!(
event,
Event::App(AppEvent {
operation: operation.clone(),
})
);
telemetry.report_event(Event::Flexible(event.clone()));
assert_eq!(telemetry.state.lock().events_queue.len(), 2);
assert!(telemetry.state.lock().flush_events_task.is_some());
assert_eq!(
Expand All @@ -643,13 +630,7 @@ mod tests {

clock.advance(Duration::from_millis(100));

let event = telemetry.report_app_event(operation.clone());
assert_eq!(
event,
Event::App(AppEvent {
operation: operation.clone(),
})
);
telemetry.report_event(Event::Flexible(event.clone()));
assert_eq!(telemetry.state.lock().events_queue.len(), 3);
assert!(telemetry.state.lock().flush_events_task.is_some());
assert_eq!(
Expand All @@ -660,14 +641,7 @@ mod tests {
clock.advance(Duration::from_millis(100));

// Adding a 4th event should cause a flush
let event = telemetry.report_app_event(operation.clone());
assert_eq!(
event,
Event::App(AppEvent {
operation: operation.clone(),
})
);

telemetry.report_event(Event::Flexible(event));
assert!(is_empty_state(&telemetry));
});
}
Expand All @@ -690,17 +664,19 @@ mod tests {
telemetry.start(system_id, installation_id, session_id, cx);

assert!(is_empty_state(&telemetry));

let first_date_time = clock.utc_now();
let operation = "test".to_string();

let event = telemetry.report_app_event(operation.clone());
assert_eq!(
event,
Event::App(AppEvent {
operation: operation.clone(),
})
);
let event_properties = HashMap::from_iter([(
"test_key".to_string(),
serde_json::Value::String("test_value".to_string()),
)]);

let event = FlexibleEvent {
event_type: "test".to_string(),
event_properties,
};

telemetry.report_event(Event::Flexible(event));
assert_eq!(telemetry.state.lock().events_queue.len(), 1);
assert!(telemetry.state.lock().flush_events_task.is_some());
assert_eq!(
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 @@ -495,6 +495,10 @@ fn for_snowflake(
body.events.into_iter().flat_map(move |event| {
let timestamp =
first_event_at + Duration::milliseconds(event.milliseconds_since_first_event);
// We will need to double check, but I believe all of the events that
// are being transformed here are now migrated over to use the
// telemetry::event! macro, as of this commit so this code can go away
// when we feel enough users have upgraded past this point.
let (event_type, mut event_properties) = match &event.event {
Event::Editor(e) => (
match e.operation.as_str() {
Expand Down
2 changes: 1 addition & 1 deletion crates/diagnostics/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ impl Item for ProjectDiagnosticsEditor {
}

fn telemetry_event_text(&self) -> Option<&'static str> {
Some("project diagnostics")
Some("Project Diagnostics Opened")
}

fn for_each_project_item(
Expand Down
2 changes: 1 addition & 1 deletion crates/editor/src/git/project_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ impl Item for ProjectDiffEditor {
}

fn telemetry_event_text(&self) -> Option<&'static str> {
Some("project diagnostics")
Some("Project Diagnostics Opened")
}

fn for_each_project_item(
Expand Down
2 changes: 1 addition & 1 deletion crates/extensions_ui/src/extensions_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ impl Item for ExtensionsPage {
}

fn telemetry_event_text(&self) -> Option<&'static str> {
Some("extensions page")
Some("Extensions Page Opened")
}

fn show_toolbar(&self) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion crates/markdown_preview/src/markdown_preview_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ impl Item for MarkdownPreviewView {
}

fn telemetry_event_text(&self) -> Option<&'static str> {
Some("markdown preview")
Some("Markdown Preview Opened")
}

fn to_item_events(_event: &Self::Event, _f: impl FnMut(workspace::item::ItemEvent)) {}
Expand Down
7 changes: 4 additions & 3 deletions crates/recent_projects/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,32 @@ doctest = false
[dependencies]
anyhow.workspace = true
auto_update.workspace = true
release_channel.workspace = true
editor.workspace = true
extension_host.workspace = true
file_finder.workspace = true
futures.workspace = true
fuzzy.workspace = true
gpui.workspace = true
log.workspace = true
language.workspace = true
log.workspace = true
markdown.workspace = true
menu.workspace = true
ordered-float.workspace = true
paths.workspace = true
picker.workspace = true
project.workspace = true
release_channel.workspace = true
remote.workspace = true
schemars.workspace = true
serde.workspace = true
settings.workspace = true
smol.workspace = true
task.workspace = true
telemetry.workspace = true
theme.workspace = true
ui.workspace = true
util.workspace = true
workspace.workspace = true
paths.workspace = true
zed_actions.workspace = true

[dev-dependencies]
Expand Down
23 changes: 3 additions & 20 deletions crates/recent_projects/src/remote_servers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,8 @@ impl ProjectPicker {
});

cx.new(|cx| {
let workspace = Workspace::new(
None,
project.clone(),
app_state.clone(),
window,
cx,
);

workspace
.client()
.telemetry()
.report_app_event("create ssh project".to_string());

workspace
telemetry::event!("SSH Project Created");
Workspace::new(None, project.clone(), app_state.clone(), window, cx)
})
})
.log_err();
Expand Down Expand Up @@ -420,12 +408,7 @@ impl RemoteServerProjects {
match connection.await {
Some(Some(client)) => this
.update(&mut cx, |this, cx| {
let _ = this.workspace.update(cx, |workspace, _| {
workspace
.client()
.telemetry()
.report_app_event("create ssh server".to_string())
});
telemetry::event!("SSH Server Created");
this.retained_connections.push(client);
this.add_ssh_server(connection_options, cx);
this.mode = Mode::default_mode(cx);
Expand Down
2 changes: 1 addition & 1 deletion crates/repl/src/repl_sessions_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl Item for ReplSessionsPage {
}

fn telemetry_event_text(&self) -> Option<&'static str> {
Some("repl sessions")
Some("REPL Session Started")
}

fn show_toolbar(&self) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion crates/search/src/project_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ impl Item for ProjectSearchView {
}

fn telemetry_event_text(&self) -> Option<&'static str> {
Some("project search")
Some("Project Search Opened")
}

fn for_each_project_item(
Expand Down
Loading
Loading