Skip to content

Commit

Permalink
Refactor analytics: more ergonomic API (#1410)
Browse files Browse the repository at this point in the history
* Make with_prop more ergonomic

* Simplify event constructors

* Register events more ergonomic

* analytics is an acceptable label for CI
  • Loading branch information
emilk committed Mar 2, 2023
1 parent 705da72 commit c8502d7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
with:
mode: minimum
count: 1
labels: "🪳 bug, 🧑‍💻 dev experience, 📖 documentation, enhancement, examples, 📉 performance, 🐍 python API, ⛃ re_datastore, 📺 re_viewer, 🔺 re_renderer, ⛴ release, 🦀 rust SDK, 🔨 testing, ui, 🕸️ web"
labels: "📊 analytics, 🪳 bug, 🧑‍💻 dev experience, 📖 documentation, examples, 📉 performance, 🐍 python API, ⛃ re_datastore, 📺 re_viewer, 🔺 re_renderer, ⛴ release, 🦀 rust SDK, 🔨 testing, ui, 🕸️ web"
4 changes: 2 additions & 2 deletions crates/re_analytics/examples/end_to_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use std::time::Duration;
use re_analytics::{Analytics, Event};

fn input_filled_event(body: String) -> Event {
Event::append("input_filled".into()).with_prop("body".into(), body)
Event::append("input_filled").with_prop("body", body)
}

fn main() {
tracing_subscriber::fmt::init(); // log to stdout

let mut analytics = Analytics::new(Duration::from_secs(3)).unwrap();
analytics.register_append_property("application_id", "end_to_end_example".to_owned());
analytics.register_append_property("application_id", "end_to_end_example");
analytics.register_append_property("recording_id", uuid::Uuid::new_v4().to_string());

println!("any non-empty line written here will be sent as an analytics datapoint");
Expand Down
23 changes: 17 additions & 6 deletions crates/re_analytics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,30 @@ pub struct Event {
}

impl Event {
pub fn append(name: Cow<'static, str>) -> Self {
pub fn append(name: impl Into<Cow<'static, str>>) -> Self {
Self {
time_utc: OffsetDateTime::now_utc(),
kind: EventKind::Append,
name,
name: name.into(),
props: Default::default(),
}
}

pub fn update(name: Cow<'static, str>) -> Self {
pub fn update(name: impl Into<Cow<'static, str>>) -> Self {
Self {
time_utc: OffsetDateTime::now_utc(),
kind: EventKind::Update,
name,
name: name.into(),
props: Default::default(),
}
}

pub fn with_prop(mut self, name: Cow<'static, str>, value: impl Into<Property>) -> Self {
self.props.insert(name, value.into());
pub fn with_prop(
mut self,
name: impl Into<Cow<'static, str>>,
value: impl Into<Property>,
) -> Self {
self.props.insert(name.into(), value.into());
self
}
}
Expand Down Expand Up @@ -157,6 +161,13 @@ impl From<String> for Property {
}
}

impl From<&str> for Property {
#[inline]
fn from(value: &str) -> Self {
Self::String(value.to_owned())
}
}

// ---

const DISCLAIMER: &str = "
Expand Down
26 changes: 13 additions & 13 deletions crates/re_viewer/src/viewer_analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl ViewerAnalytics {
AppEnvironment::RerunCli { rust_version: _ } => "rerun_cli",
AppEnvironment::Web => "web_viewer",
};
self.register("app_env", app_env_str.to_owned());
self.register("app_env", app_env_str);

#[cfg(all(not(target_arch = "wasm32"), feature = "analytics"))]
if let Some(analytics) = &self.analytics {
Expand All @@ -94,12 +94,12 @@ impl ViewerAnalytics {
build_info.git_hash.to_owned()
};

let mut event = Event::update("update_metadata".into())
.with_prop("rerun_version".into(), build_info.version.to_owned())
.with_prop("target".into(), build_info.target_triple.to_owned())
.with_prop("git_hash".into(), git_hash)
.with_prop("debug".into(), cfg!(debug_assertions).to_owned()) // debug-build?
.with_prop("rerun_workspace".into(), std::env::var("IS_IN_RERUN_WORKSPACE").is_ok()) // proxy for "user checked out the project and built it from source"
let mut event = Event::update("update_metadata")
.with_prop("rerun_version", build_info.version)
.with_prop("target", build_info.target_triple)
.with_prop("git_hash", git_hash)
.with_prop("debug", cfg!(debug_assertions)) // debug-build?
.with_prop("rerun_workspace", std::env::var("IS_IN_RERUN_WORKSPACE").is_ok()) // proxy for "user checked out the project and built it from source"
;

// If we happen to know the Python or Rust version used on the _host machine_, i.e. the
Expand All @@ -110,26 +110,26 @@ impl ViewerAnalytics {
match &app_env {
AppEnvironment::RustSdk { rust_version }
| AppEnvironment::RerunCli { rust_version } => {
event = event.with_prop("rust_version".into(), rust_version.clone());
event = event.with_prop("rust_version", rust_version.clone());
}
_ => {}
}
if let AppEnvironment::PythonSdk(version) = app_env {
event = event.with_prop("python_version".into(), version.to_string());
event = event.with_prop("python_version", version.to_string());
}

// Append opt-in metadata.
// In practice this is the email of Rerun employees
// who register their emails with `rerun analytics email`.
// This is how we filter out employees from actual users!
for (name, value) in analytics.config().opt_in_metadata.clone() {
event = event.with_prop(name.into(), value);
event = event.with_prop(name, value);
}

analytics.record(event);
}

self.record(Event::append("viewer_started".into()));
self.record(Event::append("viewer_started"));
}

/// When we have loaded the start of a new recording.
Expand Down Expand Up @@ -187,10 +187,10 @@ impl ViewerAnalytics {
re_smart_channel::Source::WsClient { .. } => "ws_client", // spawn()
re_smart_channel::Source::TcpServer { .. } => "tcp_server", // connect()
};
self.register("data_source", data_source.to_owned());
self.register("data_source", data_source);
}

self.record(Event::append("open_recording".into()));
self.record(Event::append("open_recording"));
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/re_web_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Svc {
#[cfg(feature = "analytics")]
fn on_serve_wasm(&self) {
if let Some(analytics) = &self.analytics {
analytics.record(re_analytics::Event::append("serve_wasm".into()));
analytics.record(re_analytics::Event::append("serve_wasm"));
}
}
}
Expand Down

0 comments on commit c8502d7

Please sign in to comment.