Skip to content

Commit e55d84c

Browse files
committed
Analytics: add event for when we serve the web-viewer .wasm (#1379)
* Serve a `serve_wasm` analytics event when serving a .wasm file * silence warning on CI
1 parent 1e24498 commit e55d84c

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/re_sdk/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ all-features = true
1919
[features]
2020
default = ["demo", "glam", "image", "re_viewer"]
2121

22+
## Enable telemetry using our analytics SDK.
23+
analytics = ["re_web_server?/analytics", "re_viewer?/analytics"]
24+
2225
## Enable the `demo` module (helpers for Rerun examples).
2326
demo = []
2427

crates/re_web_server/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ all-features = true
3131
## will crash if you rey to run it!
3232
__ci = []
3333

34+
## Enable telemetry using our analytics SDK.
35+
analytics = ["dep:re_analytics"]
36+
3437

3538
[dependencies]
3639
re_log.workspace = true
@@ -44,6 +47,10 @@ tokio = { workspace = true, default-features = false, features = [
4447
"rt-multi-thread",
4548
] }
4649

50+
# Optional dependencies:
51+
re_analytics = { workspace = true, optional = true }
52+
53+
4754
[build-dependencies]
4855
glob = "0.3.0"
4956
cargo_metadata = "0.15"

crates/re_web_server/src/lib.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,40 @@
1111
use std::task::{Context, Poll};
1212

1313
use futures_util::future;
14-
use hyper::{server::conn::AddrIncoming, service::Service};
15-
use hyper::{Body, Request, Response};
14+
use hyper::{server::conn::AddrIncoming, service::Service, Body, Request, Response};
1615

17-
#[derive(Debug)]
18-
struct Svc;
16+
struct Svc {
17+
// NOTE: Optional because it is possible to have the `analytics` feature flag enabled
18+
// while at the same time opting-out of analytics at run-time.
19+
#[cfg(feature = "analytics")]
20+
analytics: Option<re_analytics::Analytics>,
21+
}
22+
23+
impl Svc {
24+
#[cfg(feature = "analytics")]
25+
fn new() -> Self {
26+
let analytics = match re_analytics::Analytics::new(std::time::Duration::from_secs(2)) {
27+
Ok(analytics) => Some(analytics),
28+
Err(err) => {
29+
re_log::error!(%err, "failed to initialize analytics SDK");
30+
None
31+
}
32+
};
33+
Self { analytics }
34+
}
35+
36+
#[cfg(not(feature = "analytics"))]
37+
fn new() -> Self {
38+
Self {}
39+
}
40+
41+
#[cfg(feature = "analytics")]
42+
fn on_serve_wasm(&self) {
43+
if let Some(analytics) = &self.analytics {
44+
analytics.record(re_analytics::Event::append("serve_wasm".into()));
45+
}
46+
}
47+
}
1948

2049
impl Service<Request<Body>> for Svc {
2150
type Response = Response<Body>;
@@ -28,6 +57,10 @@ impl Service<Request<Body>> for Svc {
2857

2958
#[cfg(feature = "__ci")]
3059
fn call(&mut self, _req: Request<Body>) -> Self::Future {
60+
if false {
61+
self.on_serve_wasm(); // to silence warning about the function being unused
62+
}
63+
3164
panic!("web_server compiled with '__ci' feature (or `--all-features`). DON'T DO THAT! It's only for the CI!");
3265
}
3366

@@ -39,8 +72,12 @@ impl Service<Request<Body>> for Svc {
3972
"/" | "/index.html" => &include_bytes!("../web_viewer/index.html")[..],
4073
"/favicon.ico" => &include_bytes!("../web_viewer/favicon.ico")[..],
4174
"/sw.js" => &include_bytes!("../web_viewer/sw.js")[..],
42-
"/re_viewer_bg.wasm" => &include_bytes!("../web_viewer/re_viewer_bg.wasm")[..],
4375
"/re_viewer.js" => &include_bytes!("../web_viewer/re_viewer.js")[..],
76+
"/re_viewer_bg.wasm" => {
77+
#[cfg(feature = "analytics")]
78+
self.on_serve_wasm();
79+
&include_bytes!("../web_viewer/re_viewer_bg.wasm")[..]
80+
}
4481
_ => {
4582
re_log::warn!("404 path: {}", req.uri().path());
4683
let body = Body::from(Vec::new());
@@ -67,7 +104,7 @@ impl<T> Service<T> for MakeSvc {
67104
}
68105

69106
fn call(&mut self, _: T) -> Self::Future {
70-
future::ok(Svc)
107+
future::ok(Svc::new())
71108
}
72109
}
73110

crates/rerun/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown"]
2222
default = ["analytics", "glam", "image", "server", "sdk"]
2323

2424
## Enable telemetry using our analytics SDK.
25-
analytics = ["dep:re_analytics", "re_viewer/analytics"]
25+
analytics = ["dep:re_analytics", "re_viewer/analytics", "re_sdk?/analytics"]
2626

2727
## Add support for some math operations using [`glam`](https://crates.io/crates/glam/).
2828
## Only relevant if feature `sdk` is enabled.

0 commit comments

Comments
 (0)