Skip to content

fix: Add known extensions to telemetry #4280

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions e2e/tests-dfx/telemetry.bash
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ teardown() {
}

@test "telemetry reprocesses extension commands" {
local log
local log n
log=$(dfx info telemetry-log-path)
assert_command dfx extension install nns --version 0.3.1
assert_command dfx nns import
assert_command jq -se 'last | .command == "extension run" and (.parameters | any(.name == "name"))' "$log"
assert_command jq -se 'last | .command == "extension run" and (.parameters | any(.name == "@extension_name" and .value == "nns"))' "$log"
n=$(jq -sr 'map(select(has("command"))) | length' "$log")
assert_command dfx nns help
# shellcheck disable=SC2016
assert_command jq -se '(map(select(has("command"))) | length) == $n' --argjson n "$n" "$log"
}

@test "concurrent commands do not corrupt the log file" {
Expand Down
5 changes: 5 additions & 0 deletions src/dfx/src/commands/extension/run.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::lib::environment::Environment;
use crate::lib::error::DfxResult;
use crate::lib::telemetry::Telemetry;
use clap::Parser;
use std::ffi::OsString;

#[derive(Parser, Debug)]
pub struct RunOpts {
/// Specifies the name of the extension to run.
#[arg(id = "@extension_name", value_name = "NAME")]
name: OsString,
/// Specifies the parameters to pass to the extension.
#[arg(allow_hyphen_values = true)]
Expand All @@ -23,6 +25,9 @@ impl From<Vec<OsString>> for RunOpts {
}

pub fn exec(env: &dyn Environment, opts: RunOpts) -> DfxResult<()> {
if opts.params.iter().any(|p| p == "--help" || p == "help") {
Telemetry::mark_current_command_likely_noise();
}
let mgr = env.get_extension_manager();
let project_root = env
.get_config()?
Expand Down
38 changes: 27 additions & 11 deletions src/dfx/src/lib/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub struct Telemetry {
network_type: Option<NetworkType>,
allowlisted_canisters: BTreeSet<Principal>,
week: Option<String>,
skip: bool,
publish: bool,
}

Expand Down Expand Up @@ -239,6 +240,9 @@ impl Telemetry {

pub fn append_current_command_timestamped(exit_code: i32) -> DfxResult<()> {
try_with_telemetry(|telemetry| {
if telemetry.skip {
return Ok(());
}
let reject = telemetry.last_reject.as_ref();
let call_site = telemetry.last_operation.as_ref().map(|o| match o {
Operation::Call { method, canister } => {
Expand Down Expand Up @@ -272,6 +276,10 @@ impl Telemetry {
})
}

pub fn mark_current_command_likely_noise() {
with_telemetry(|t| t.skip = true);
}

pub fn maybe_publish() -> DfxResult {
try_with_telemetry(|telemetry| {
if telemetry.publish && (Self::check_send_time()? || Self::check_file_size()?) {
Expand Down Expand Up @@ -656,18 +664,26 @@ fn get_sanitized_arguments(matches: &ArgMatches, command: &Command) -> Vec<Argum
_ => continue, // ValueSource isn't exhaustive
};

let possible_values = command
.get_arguments()
.find(|arg| arg.get_id() == *id)
.map(|arg| arg.get_possible_values());

let sanitized_value = match (possible_values, matches.try_get_one::<String>(id)) {
(Some(possible_values), Ok(Some(s)))
if possible_values.iter().any(|pv| pv.matches(s, true)) =>
{
Some(s.clone())
let sanitized_value = if *id == "@extension_name" {
match matches.try_get_one::<OsString>(id) {
Ok(Some(s)) if s == "sns" || s == "nns" => {
Some(s.to_str().unwrap().to_string())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why we would not just log the extension being installed always?

Copy link
Contributor Author

@adamspofford-dfinity adamspofford-dfinity Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because users can define their own extensions, and we're trying to avoid inspecting user-defined strings (a pathological case, but so are extensions generally). If you want to capture Azle usage it can be manually added

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: record the extension name from the manifest during dfx extension install, here:

https://github.com/dfinity/sdk/blob/master/src/dfx-core/src/extension/manager/install.rs#L48

}
_ => None,
}
} else {
let possible_values = command
.get_arguments()
.find(|arg| arg.get_id() == *id)
.map(|arg| arg.get_possible_values());
match (possible_values, matches.try_get_one::<String>(id)) {
(Some(possible_values), Ok(Some(s)))
if possible_values.iter().any(|pv| pv.matches(s, true)) =>
{
Some(s.clone())
}
_ => None,
}
_ => None,
};

let argument = Argument {
Expand Down