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

Analytics: add event for when we serve the web-viewer .wasm #1379

Merged
merged 2 commits into from
Feb 22, 2023
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
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/re_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ all-features = true
[features]
default = ["demo", "glam", "image", "re_viewer"]

## Enable telemetry using our analytics SDK.
analytics = ["re_web_server?/analytics", "re_viewer?/analytics"]

## Enable the `demo` module (helpers for Rerun examples).
demo = []

Expand Down
7 changes: 7 additions & 0 deletions crates/re_web_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ all-features = true
## will crash if you rey to run it!
__ci = []

## Enable telemetry using our analytics SDK.
analytics = ["dep:re_analytics"]


[dependencies]
re_log.workspace = true
Expand All @@ -44,6 +47,10 @@ tokio = { workspace = true, default-features = false, features = [
"rt-multi-thread",
] }

# Optional dependencies:
re_analytics = { workspace = true, optional = true }


[build-dependencies]
glob = "0.3.0"
cargo_metadata = "0.15"
49 changes: 43 additions & 6 deletions crates/re_web_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,40 @@
use std::task::{Context, Poll};

use futures_util::future;
use hyper::{server::conn::AddrIncoming, service::Service};
use hyper::{Body, Request, Response};
use hyper::{server::conn::AddrIncoming, service::Service, Body, Request, Response};

#[derive(Debug)]
struct Svc;
struct Svc {
// NOTE: Optional because it is possible to have the `analytics` feature flag enabled
// while at the same time opting-out of analytics at run-time.
#[cfg(feature = "analytics")]
analytics: Option<re_analytics::Analytics>,
}

impl Svc {
#[cfg(feature = "analytics")]
fn new() -> Self {
let analytics = match re_analytics::Analytics::new(std::time::Duration::from_secs(2)) {
Ok(analytics) => Some(analytics),
Err(err) => {
re_log::error!(%err, "failed to initialize analytics SDK");
None
}
};
Self { analytics }
}

#[cfg(not(feature = "analytics"))]
fn new() -> Self {
Self {}
}

#[cfg(feature = "analytics")]
fn on_serve_wasm(&self) {
if let Some(analytics) = &self.analytics {
analytics.record(re_analytics::Event::append("serve_wasm".into()));
}
}
}

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

#[cfg(feature = "__ci")]
fn call(&mut self, _req: Request<Body>) -> Self::Future {
if false {
self.on_serve_wasm(); // to silence warning about the function being unused
}

panic!("web_server compiled with '__ci' feature (or `--all-features`). DON'T DO THAT! It's only for the CI!");
}

Expand All @@ -39,8 +72,12 @@ impl Service<Request<Body>> for Svc {
"/" | "/index.html" => &include_bytes!("../web_viewer/index.html")[..],
"/favicon.ico" => &include_bytes!("../web_viewer/favicon.ico")[..],
"/sw.js" => &include_bytes!("../web_viewer/sw.js")[..],
"/re_viewer_bg.wasm" => &include_bytes!("../web_viewer/re_viewer_bg.wasm")[..],
"/re_viewer.js" => &include_bytes!("../web_viewer/re_viewer.js")[..],
"/re_viewer_bg.wasm" => {
#[cfg(feature = "analytics")]
self.on_serve_wasm();
&include_bytes!("../web_viewer/re_viewer_bg.wasm")[..]
}
_ => {
re_log::warn!("404 path: {}", req.uri().path());
let body = Body::from(Vec::new());
Expand All @@ -67,7 +104,7 @@ impl<T> Service<T> for MakeSvc {
}

fn call(&mut self, _: T) -> Self::Future {
future::ok(Svc)
future::ok(Svc::new())
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rerun/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown"]
default = ["analytics", "glam", "image", "server", "sdk"]

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

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