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

feat: instrument dfx for telemetry #4149

Merged
merged 20 commits into from
Mar 21, 2025
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
10 changes: 5 additions & 5 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ needless_lifetimes = "allow"
candid = "0.10.11"
candid_parser = "0.1.4"
dfx-core = { path = "src/dfx-core", version = "0.1.0" }
ic-agent = { version = "0.39", git = "https://github.com/dfinity/agent-rs", rev = "9ebf6314ce2fcb36772c7d81d6d414b4628d6101" }
ic-agent = { version = "0.39", git = "https://github.com/dfinity/agent-rs", rev = "7752c33016f708fb84b56ff99d91a7c380a8b550" }
ic-asset = { path = "src/canisters/frontend/ic-asset", version = "0.22.0" }
ic-cdk = "0.13.1"
ic-identity-hsm = { version = "0.39", git = "https://github.com/dfinity/agent-rs", rev = "9ebf6314ce2fcb36772c7d81d6d414b4628d6101" }
ic-utils = { version = "0.39", git = "https://github.com/dfinity/agent-rs", rev = "9ebf6314ce2fcb36772c7d81d6d414b4628d6101" }
ic-identity-hsm = { version = "0.39", git = "https://github.com/dfinity/agent-rs", rev = "7752c33016f708fb84b56ff99d91a7c380a8b550" }
ic-utils = { version = "0.39", git = "https://github.com/dfinity/agent-rs", rev = "7752c33016f708fb84b56ff99d91a7c380a8b550" }

aes-gcm = { version = "0.10.3", features = ["std"] }
anyhow = "1.0.56"
Expand Down
91 changes: 85 additions & 6 deletions e2e/tests-dfx/telemetry.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ setup() {
standard_setup

dfx_new

export DFX_TELEMETRY=local
}

teardown() {
Expand Down Expand Up @@ -45,21 +47,21 @@ teardown() {
assert_eq local
}

@test "telemetry is collected" {
@test "command-line args are collected" {
local expected_platform log n
case "$(uname)" in
Darwin) expected_platform=macos;;
Linux) expected_platform=linux;;
*) fail 'unknown platform';;
esac
log=$(dfx info telemetry-log-path)
assert_command env DFX_TELEMETRY=local dfx identity get-principal
assert_command dfx identity get-principal
assert_command jq -se 'last | .command == "identity get-principal" and .platform == "'"$expected_platform"'"
and .exit_code == 0 and (.parameters | length == 0)' "$log"
n=$(jq -sr length "$log")
assert_command_fail env DFX_TELEMETRY=local DFX_NETWORK=ic dfx identity get-platypus
assert_command_fail env DFX_NETWORK=ic dfx identity get-platypus
assert_command jq -se "length == $n" "$log"
assert_command_fail env DFX_TELEMETRY=local DFX_NETWORK=ic dfx identity get-principal --identity platypus
assert_command_fail env DFX_NETWORK=ic dfx identity get-principal --identity platypus
assert_command jq -se 'length == '$((n+1))' and (last | .command == "identity get-principal" and .exit_code == 255 and
(.parameters | any(.name == "network" and .source == "environment")
and any(.name == "identity" and .source == "command-line")))' "$log"
Expand All @@ -69,7 +71,7 @@ teardown() {
local log
log=$(dfx info telemetry-log-path)
assert_command dfx extension install nns --version 0.3.1
assert_command env DFX_TELEMETRY=local dfx nns import
assert_command dfx nns import
assert_command jq -se 'last | .command == "extension run" and (.parameters | any(.name == "name"))' "$log"
}

Expand All @@ -78,8 +80,85 @@ teardown() {
log=$(dfx info telemetry-log-path)
dfx identity get-principal # initialize it first
for _ in {0..100}; do
assert_command env DFX_TELEMETRY=local dfx identity get-principal &
assert_command dfx identity get-principal &
done
wait
assert_command jq -se '.[-101:-1] | all(.command == "identity get-principal") and length == 100' "$log"
}

@test "the last replica error is collected" {
dfx_new_assets
local log wallet
log=$(dfx info telemetry-log-path)
# explicit call, known canister (ICP ledger)
dfx_start
assert_command_fail dfx canister call ryjl3-tyaaa-aaaaa-aaaba-cai name
assert_command jq -se 'last | .replica_error_call_site == "name" and .replica_error_code == "IC0301"' "$log"
# implicit call, wallet canister
wallet=$(dfx identity get-wallet)
dfx canister stop "$wallet"
dfx canister delete "$wallet" --no-withdrawal -y
assert_command_fail dfx canister create e2e_project_backend
assert_command jq -se 'last | .replica_error_call_site == "wallet_api_version" and .replica_error_code == "IC0301"' "$log"
# call to unknown canister
dfx canister create --all --no-wallet
assert_command_fail dfx canister call e2e_project_backend greet
assert_command jq -se 'last | .replica_error_call_site == "<user-specified canister method>" and .replica_error_code == "IC0537"' "$log"
# call to assets canister
install_asset wasm
dfx build e2e_project_frontend
dfx canister install "$(dfx canister id e2e_project_frontend)" --wasm identity/main.wasm
assert_command_fail dfx canister install e2e_project_frontend --mode upgrade --no-asset-upgrade -y
assert_command jq -se 'last | .replica_error_call_site == "list" and .replica_error_code == "IC0536"' "$log"
}

@test "network information is collected" {
local log
log=$(dfx info telemetry-log-path)
dfx_start
dfx identity get-wallet
assert_command jq -se 'last.network_type == "local-shared"' "$log"
assert_command_fail dfx identity get-wallet --ic
assert_command jq -se 'last.network_type == "ic"' "$log"
setup_actuallylocal_project_network
dfx identity get-wallet --network actuallylocal
assert_command jq -se 'last.network_type == "unknown-configured"' "$log"
setup_ephemeral_project_network
dfx identity get-wallet --network ephemeral
assert_command jq -se 'last.network_type == "project-local"' "$log"
assert_command_fail dfx identity get-wallet --playground
assert_command jq -se 'last.network_type == "playground"' "$log"
assert_command_fail dfx identity get-wallet --network "https://example.com"
assert_command jq -se 'last.network_type == "unknown-url"' "$log"
}

@test "project structure is collected" {
local log
log=$(dfx info telemetry-log-path)
dfx_start
dfx deploy
assert_command jq -se 'last.project_canisters == [{type: "motoko"}]' "$log"
dfx_new_frontend
dfx deploy
assert_command jq -se 'last.project_canisters | sort_by(.type) == [{type: "assets"}, {type: "motoko"}]' "$log"
install_asset deps/app
dfx deploy || true
assert_command jq -se 'last.project_canisters | sort_by(.type) == [{type: "motoko"}, {type: "pull"}, {type: "pull"}]' "$log"
}

@test "sender information is collected" {
local log
log=$(dfx info telemetry-log-path)
dfx_start
dfx canister create --all
assert_command jq -se 'last | .cycles_host == "cycles-wallet" and .identity_type == "plaintext"' "$log"
dfx canister delete --all -y
(
export DFX_CI_MOCK_KEYRING_LOCATION="$MOCK_KEYRING_LOCATION";
dfx identity new alice
dfx canister create --all --no-wallet --identity alice
)
assert_command jq -se 'last | .cycles_host == null and .identity_type == "keyring"' "$log"
dfx cycles balance --ic --identity anonymous
assert_command jq -se 'last | .cycles_host == "cycles-ledger" and .identity_type == "anonymous"' "$log"
}
4 changes: 4 additions & 0 deletions e2e/utils/_.bash
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ setup_actuallylocal_project_network() {
jq '.networks.actuallylocal.providers=["http://127.0.0.1:'"$webserver_port"'"]' dfx.json | sponge dfx.json
}

setup_ephemeral_project_network() {
jq ".networks.ephemeral.bind=\"127.0.0.1:$(get_webserver_port)\"" dfx.json | sponge dfx.json
}

setup_actuallylocal_shared_network() {
webserver_port=$(get_webserver_port)
[ ! -f "$E2E_NETWORKS_JSON" ] && echo "{}" >"$E2E_NETWORKS_JSON"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ pub(crate) async fn get_assets_properties(
}
// older canisters don't have get_assets_properties method
// therefore we can break the loop
Err(AgentError::UncertifiedReject(RejectResponse { reject_message, .. }))
if reject_message
.contains(&format!("has no query method '{GET_ASSET_PROPERTIES}'"))
|| reject_message.contains("query method does not exist") =>
Err(AgentError::UncertifiedReject {
reject: RejectResponse { reject_message, .. },
..
}) if reject_message
.contains(&format!("has no query method '{GET_ASSET_PROPERTIES}'"))
|| reject_message.contains("query method does not exist") =>
{
break;
}
Expand Down
3 changes: 3 additions & 0 deletions src/dfx-core/src/config/model/network_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct NetworkDescriptor {
pub providers: Vec<String>,
pub r#type: NetworkTypeDescriptor,
pub is_ic: bool,
pub is_ad_hoc: bool,
pub local_server_descriptor: Option<LocalServerDescriptor>,
}

Expand Down Expand Up @@ -71,6 +72,7 @@ impl NetworkDescriptor {
providers: vec![DEFAULT_IC_GATEWAY.to_string()],
r#type: NetworkTypeDescriptor::Persistent,
is_ic: true,
is_ad_hoc: false,
local_server_descriptor: None,
}
}
Expand Down Expand Up @@ -121,6 +123,7 @@ impl NetworkDescriptor {
canister_timeout_seconds: MOTOKO_PLAYGROUND_CANISTER_TIMEOUT_SECONDS,
},
is_ic: true,
is_ad_hoc: false,
local_server_descriptor: None,
}
}
Expand Down
34 changes: 27 additions & 7 deletions src/dfx-core/src/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ pub struct Identity {

/// Inner implementation of this identity.
inner: Box<dyn ic_agent::Identity + Sync + Send>,

identity_type: IdentityType,
}

impl Identity {
Expand All @@ -85,13 +87,14 @@ impl Identity {
name: ANONYMOUS_IDENTITY_NAME.to_string(),
inner: Box::new(AnonymousIdentity {}),
insecure: false,
identity_type: IdentityType::Anonymous,
}
}

fn basic(
name: &str,
pem_content: &[u8],
was_encrypted: bool,
identity_type: IdentityType,
) -> Result<Self, LoadPemIdentityError> {
let inner = Box::new(
BasicIdentity::from_pem(pem_content)
Expand All @@ -101,14 +104,15 @@ impl Identity {
Ok(Self {
name: name.to_string(),
inner,
insecure: !was_encrypted,
insecure: identity_type == IdentityType::Plaintext,
identity_type,
})
}

fn secp256k1(
name: &str,
pem_content: &[u8],
was_encrypted: bool,
identity_type: IdentityType,
) -> Result<Self, LoadPemIdentityError> {
let inner = Box::new(
Secp256k1Identity::from_pem(pem_content)
Expand All @@ -118,7 +122,8 @@ impl Identity {
Ok(Self {
name: name.to_string(),
inner,
insecure: !was_encrypted,
insecure: identity_type == IdentityType::Plaintext,
identity_type,
})
}

Expand All @@ -139,6 +144,7 @@ impl Identity {
name: name.to_string(),
inner,
insecure: false,
identity_type: IdentityType::Hsm,
})
}

Expand All @@ -151,11 +157,11 @@ impl Identity {
if let Some(hsm) = config.hsm {
Identity::hardware(name, hsm).map_err(NewIdentityError::NewHardwareIdentityFailed)
} else {
let (pem_content, was_encrypted) =
let (pem_content, identity_type) =
pem_safekeeping::load_pem(log, locations, name, &config)
.map_err(NewIdentityError::LoadPemFailed)?;
Identity::secp256k1(name, &pem_content, was_encrypted)
.or_else(|e| Identity::basic(name, &pem_content, was_encrypted).map_err(|_| e))
Identity::secp256k1(name, &pem_content, identity_type)
.or_else(|e| Identity::basic(name, &pem_content, identity_type).map_err(|_| e))
.map_err(NewIdentityError::LoadPemIdentityFailed)
}
}
Expand All @@ -166,6 +172,10 @@ impl Identity {
&self.name
}

pub fn identity_type(&self) -> IdentityType {
self.identity_type
}

/// Logs all wallets that are configured in a WalletGlobalConfig.
pub fn display_linked_wallets(
logger: &Logger,
Expand Down Expand Up @@ -304,6 +314,16 @@ impl AsRef<Identity> for Identity {
}
}

#[derive(Serialize, Copy, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum IdentityType {
Keyring,
Plaintext,
EncryptedLocal,
Hsm,
Anonymous,
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum CallSender {
SelectedId,
Expand Down
17 changes: 12 additions & 5 deletions src/dfx-core/src/identity/pem_safekeeping.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::identity_manager::EncryptionConfiguration;
use super::IdentityConfiguration;
use super::{IdentityConfiguration, IdentityType};
use crate::error::identity::WritePemContentError;
use crate::error::{
encryption::{
Expand Down Expand Up @@ -32,7 +32,7 @@ pub(crate) fn load_pem(
locations: &IdentityFileLocations,
identity_name: &str,
identity_config: &IdentityConfiguration,
) -> Result<(Vec<u8>, bool), LoadPemError> {
) -> Result<(Vec<u8>, IdentityType), LoadPemError> {
if identity_config.hsm.is_some() {
unreachable!("Cannot load pem content for an HSM identity.")
} else if identity_config.keyring_identity_suffix.is_some() {
Expand All @@ -42,7 +42,7 @@ pub(crate) fn load_pem(
);
let pem = keyring_mock::load_pem_from_keyring(identity_name)
.map_err(|err| LoadFromKeyringFailed(Box::new(identity_name.to_string()), err))?;
Ok((pem, true))
Ok((pem, IdentityType::Keyring))
} else {
let pem_path = locations.get_identity_pem_path(identity_name, identity_config);
load_pem_from_file(&pem_path, Some(identity_config))
Expand Down Expand Up @@ -83,12 +83,19 @@ pub(crate) fn save_pem(
pub fn load_pem_from_file(
path: &Path,
config: Option<&IdentityConfiguration>,
) -> Result<(Vec<u8>, bool), LoadPemFromFileError> {
) -> Result<(Vec<u8>, IdentityType), LoadPemFromFileError> {
let content = crate::fs::read(path)?;

let (content, was_encrypted) = maybe_decrypt_pem(content.as_slice(), config)
.map_err(|err| DecryptPemFileFailed(path.to_path_buf(), err))?;
Ok((content, was_encrypted))
Ok((
content,
if was_encrypted {
IdentityType::EncryptedLocal
} else {
IdentityType::Plaintext
},
))
}

/// Transparently handles all complexities regarding pem file encryption, including prompting the user for the password.
Expand Down
Loading