Skip to content

Commit

Permalink
feat(telemetry): add support for CI env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwht committed May 13, 2024
1 parent 93ee57c commit 4dda846
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
11 changes: 9 additions & 2 deletions crates/pop-telemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,15 @@ We take privacy seriously and are committed to protecting the anonymity of our u

## How to Opt-Out

If you prefer not to participate in anonymous usage metrics collection, you can completely disable telemetry by
installing Pop CLI with it disabled.
If you prefer not to participate in anonymous usage metrics collection, there are a
few ways you can opt out. We support the [DO_NOT_TRACK](https://consoledonottrack.com/) and CI environment variable
standards.

1. Set the `DO_NOT_TRACK` environment variable to `true` or `1`:
2. Set the `CI` environment variable to `true` or `1`:
3. Completely disable telemetry

Install Pop CLI with telemetry compiled out

```bash
cargo install --locked --no-default-features --features contract,parachain --git "https://github.com/r0gue-io/pop-cli"
Expand Down
40 changes: 29 additions & 11 deletions crates/pop-telemetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ impl Telemetry {
!config.opt_out.version.is_empty()
}

// Checks two env variables, CI & DO_NOT_TRACK. If either are set to true, disable telemetry
fn is_opt_out_from_env() -> bool {
env::var("DO_NOT_TRACK").unwrap_or("false".to_string()) == "true"
// CI first as it is more likely to be set
env::var("CI").unwrap_or_default() == "true"
|| env::var("CI").unwrap_or_default() == "1"
|| env::var("DO_NOT_TRACK").unwrap_or_default() == "true"
|| env::var("DO_NOT_TRACK").unwrap_or_default() == "1"
}

/// Check if the user has opted out of telemetry through two methods:
Expand Down Expand Up @@ -245,16 +250,6 @@ mod tests {
#[tokio::test]
async fn new_telemetry_works() {
let _ = env_logger::try_init();
// assert that no config file and false DO_NOT_TRACK sets opt-out to false
env::set_var("DO_NOT_TRACK", "false");
assert!(!Telemetry::init("".to_string(), &PathBuf::new()).opt_out);
// assert if DO_NOT_TRACK env var is set to false, opt-out is false
assert!(!Telemetry::init("".to_string(), &PathBuf::new()).opt_out);

// check that if DO_NOT_TRACK env var is set, opt-out is true
env::set_var("DO_NOT_TRACK", "true");
assert!(Telemetry::init("".to_string(), &PathBuf::new()).opt_out);
env::remove_var("DO_NOT_TRACK");

// Mock config file path function to return a temporary path
let temp_dir = TempDir::new().unwrap();
Expand Down Expand Up @@ -282,6 +277,29 @@ mod tests {
assert_eq!(tel.opt_out, expected_telemetry.opt_out);
}

#[test]
#[ignore]
/// ignore by default as to not mess up env variables for
/// remaining tests when running in CI
fn new_telemetry_env_vars_works() {
let _ = env_logger::try_init();

// assert that no config file, and env vars not existing sets opt-out to false
env::remove_var("DO_NOT_TRACK");
env::set_var("CI", "false");
assert!(!Telemetry::init("".to_string(), &PathBuf::new()).opt_out);

// assert that if DO_NOT_TRACK env var is set, opt-out is true
env::set_var("DO_NOT_TRACK", "true");
assert!(Telemetry::init("".to_string(), &PathBuf::new()).opt_out);
env::remove_var("DO_NOT_TRACK");

// assert that if CI env var is set, opt-out is true
env::set_var("CI", "true");
assert!(Telemetry::init("".to_string(), &PathBuf::new()).opt_out);
env::remove_var("CI");
}

#[tokio::test]
async fn test_record_cli_used() {
let _ = env_logger::try_init();
Expand Down

0 comments on commit 4dda846

Please sign in to comment.