Skip to content

Commit

Permalink
Add metric for number of successful prepare_id_alias calls (#2519)
Browse files Browse the repository at this point in the history
* Add metric for number of successful prepare_id_alias calls

This PR adds a metric for how many prepare_id_alias calls have been
handled. This number can be used to infer the number of VCs presented
using Internet Identity.

* Fix tests
  • Loading branch information
frederikrothenberger committed Jun 26, 2024
1 parent e6c56a0 commit 3f049ec
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/internet_identity/src/http/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ fn encode_metrics(w: &mut MetricsEncoder<Vec<u8>>) -> std::io::Result<()> {
"internet_identity_anchor_operations_counter",
usage_metrics.anchor_operation_counter as f64,
"The number of anchor operations since last upgrade",
)?;
w.encode_gauge(
"internet_identity_prepare_id_alias_counter",
usage_metrics.prepare_id_alias_counter as f64,
"The number of successful prepare_id_alias calls handled since last upgrade. For each VC presentation flow, exactly one prepare_id_alias is made.",
)
})?;
if let ArchiveState::Created { ref data, config } = state::archive_state() {
Expand Down
2 changes: 2 additions & 0 deletions src/internet_identity/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub struct UsageMetrics {
pub delegation_counter: u64,
// number of anchor operations (register, add, remove, update) since last upgrade
pub anchor_operation_counter: u64,
// number of prepare_id_alias calls since last upgrade
pub prepare_id_alias_counter: u64,
}

// The challenges we store and check against
Expand Down
5 changes: 5 additions & 0 deletions src/internet_identity/src/vc_mvp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ pub async fn prepare_id_alias(
sigs.add_signature(seed.as_ref(), vc_signing_input_hash(&issuer_signing_input));
});
update_root_hash();

state::usage_metrics_mut(|metrics| {
metrics.prepare_id_alias_counter += 1;
});

PreparedIdAlias {
canister_sig_pk_der: ByteBuf::from(canister_sig_pk.to_der()),
rp_id_alias_jwt: String::from_utf8(rp_signing_input).unwrap(),
Expand Down
36 changes: 34 additions & 2 deletions src/internet_identity/tests/integration/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use ic_response_verification::types::VerificationInfo;
use ic_response_verification::verify_request_response_pair;
use ic_test_state_machine_client::{CallError, StateMachine};
use internet_identity_interface::http_gateway::{HttpRequest, HttpResponse};
use internet_identity_interface::internet_identity::types::vc_mvp::PrepareIdAliasRequest;
use internet_identity_interface::internet_identity::types::{
AuthnMethodData, ChallengeAttempt, MetadataEntryV2,
AuthnMethodData, ChallengeAttempt, FrontendHostname, MetadataEntryV2,
};
use serde_bytes::ByteBuf;
use std::collections::HashMap;
Expand Down Expand Up @@ -136,7 +137,7 @@ fn should_set_cache_control_for_fonts() -> Result<(), CallError> {
Ok(())
}

/// Verifies that all expected metrics are available via the HTTP endpoint.
/// Verifies that expected metrics are available via the HTTP endpoint.
#[test]
fn ii_canister_serves_http_metrics() -> Result<(), CallError> {
let metrics = vec![
Expand All @@ -150,6 +151,7 @@ fn ii_canister_serves_http_metrics() -> Result<(), CallError> {
"internet_identity_inflight_challenges",
"internet_identity_users_in_registration_mode",
"internet_identity_buffered_archive_entries",
"internet_identity_prepare_id_alias_counter",
];
let env = env();
env.advance_time(Duration::from_secs(300)); // advance time to see it reflected on the metrics endpoint
Expand Down Expand Up @@ -721,6 +723,36 @@ fn should_list_aggregated_session_seconds_and_event_data_counters() -> Result<()
Ok(())
}

#[test]
fn should_get_different_id_alias_for_different_flows() -> Result<(), CallError> {
let env = env();
let canister_id = install_ii_canister(&env, II_WASM.clone());
let identity_number = flows::register_anchor(&env, canister_id);

let prepare_id_alias_req = PrepareIdAliasRequest {
identity_number,
relying_party: FrontendHostname::from("https://some-dapp.com"),
issuer: FrontendHostname::from("https://some-issuer-1.com"),
};

for _ in 0..3 {
api::vc_mvp::prepare_id_alias(
&env,
canister_id,
principal_1(),
prepare_id_alias_req.clone(),
)?
.expect("Got 'None' from prepare_id_alias");
}

assert_metric(
&get_metrics(&env, canister_id),
"internet_identity_prepare_id_alias_counter",
3f64,
);
Ok(())
}

fn verify_response_certification(
env: &StateMachine,
canister_id: CanisterId,
Expand Down

0 comments on commit 3f049ec

Please sign in to comment.