Skip to content

Commit a3bd577

Browse files
committed
Merge branch 'FI-736' into 'master'
feat(FI-736) [icrc index-ng] add metrics See merge request dfinity-lab/public/ic!12617
2 parents 6a2da47 + eb10cc4 commit a3bd577

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rs/rosetta-api/icrc1/index-ng/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ DEPENDENCIES = [
88
"//packages/icrc-ledger-types:icrc_ledger_types",
99
"//rs/rosetta-api/icrc1",
1010
"//rs/rosetta-api/ledger_core",
11+
"//rs/rust_canisters/http_types",
1112
"@crate_index//:candid",
1213
"@crate_index//:ciborium",
1314
"@crate_index//:ic-cdk",
1415
"@crate_index//:ic-cdk-timers",
16+
"@crate_index//:ic-metrics-encoder",
1517
"@crate_index//:num-traits",
1618
"@crate_index//:scopeguard",
1719
"@crate_index//:serde",

rs/rosetta-api/icrc1/index-ng/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ path = "src/main.rs"
1212
[dependencies]
1313
candid = "0.8.1"
1414
ciborium = "0.2"
15+
ic-canisters-http-types = { path = "../../../rust_canisters/http_types" }
1516
ic-cdk = { version = "0.6.0" }
1617
ic-cdk-macros = { version = "0.6.0" }
1718
ic-cdk-timers = "0.1.2"
1819
ic-crypto-sha = { path = "../../../crypto/sha" }
1920
ic-icrc1 = { path = "../" }
2021
ic-ledger-core = { path = "../../ledger_core" }
22+
ic-metrics-encoder = "1.1"
2123
ic-stable-structures = "0.5.3"
2224
icrc-ledger-types = { path = "../../../../packages/icrc-ledger-types" }
2325
num-traits = "0.2.14"

rs/rosetta-api/icrc1/index-ng/src/main.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use candid::{candid_method, Nat, Principal};
2+
use ic_canisters_http_types::{HttpRequest, HttpResponse, HttpResponseBuilder};
23
use ic_cdk::trap;
34
use ic_cdk_macros::{init, post_upgrade, query, update};
45
use ic_cdk_timers::TimerId;
@@ -93,6 +94,9 @@ struct State {
9394

9495
/// The maximum number of transactions returned by [get_blocks].
9596
max_blocks_per_response: u64,
97+
98+
// Last wait time in nanoseconds.
99+
pub last_wait_time: Duration,
96100
}
97101

98102
// NOTE: the default configuration is dysfunctional, but it's convenient to have
@@ -103,6 +107,7 @@ impl Default for State {
103107
is_build_index_running: false,
104108
ledger_id: Principal::management_canister(),
105109
max_blocks_per_response: DEFAULT_MAX_BLOCKS_PER_RESPONSE,
110+
last_wait_time: Duration::from_secs(0),
106111
}
107112
}
108113
}
@@ -312,6 +317,7 @@ pub async fn build_index() -> Result<(), String> {
312317
append_blocks(res.blocks);
313318
let wait_time = compute_wait_time(tx_indexed_count);
314319
ic_cdk::eprintln!("Indexed: {} waiting : {:?}", tx_indexed_count, wait_time);
320+
change_state(|mut state| state.last_wait_time = wait_time);
315321
ScopeGuard::into_inner(failure_guard);
316322
set_build_index_timer(wait_time);
317323
Ok(())
@@ -640,6 +646,64 @@ fn list_subaccounts(args: ListSubaccountsArgs) -> Vec<Subaccount> {
640646
})
641647
}
642648

649+
#[candid_method(query)]
650+
#[query]
651+
fn http_request(req: HttpRequest) -> HttpResponse {
652+
if req.path() == "/metrics" {
653+
let mut writer =
654+
ic_metrics_encoder::MetricsEncoder::new(vec![], ic_cdk::api::time() as i64 / 1_000_000);
655+
656+
match encode_metrics(&mut writer) {
657+
Ok(()) => HttpResponseBuilder::ok()
658+
.header("Content-Type", "text/plain; version=0.0.4")
659+
.with_body_and_content_length(writer.into_inner())
660+
.build(),
661+
Err(err) => {
662+
HttpResponseBuilder::server_error(format!("Failed to encode metrics: {}", err))
663+
.build()
664+
}
665+
}
666+
} else {
667+
HttpResponseBuilder::not_found().build()
668+
}
669+
}
670+
671+
pub fn encode_metrics(w: &mut ic_metrics_encoder::MetricsEncoder<Vec<u8>>) -> std::io::Result<()> {
672+
w.encode_gauge(
673+
"index_stable_memory_pages",
674+
ic_cdk::api::stable::stable_size() as f64,
675+
"Size of the stable memory allocated by this canister measured in 64K Wasm pages.",
676+
)?;
677+
w.encode_gauge(
678+
"index_stable_memory_bytes",
679+
(ic_cdk::api::stable::stable_size() * 64 * 1024) as f64,
680+
"Size of the stable memory allocated by this canister.",
681+
)?;
682+
683+
let cycle_balance = ic_cdk::api::canister_balance128() as f64;
684+
w.encode_gauge(
685+
"index_cycle_balance",
686+
cycle_balance,
687+
"Cycle balance on this canister.",
688+
)?;
689+
w.gauge_vec("cycle_balance", "Cycle balance on this canister.")?
690+
.value(&[("canister", "icrc1-index")], cycle_balance)?;
691+
692+
w.encode_gauge(
693+
"index_number_of_blocks",
694+
with_blocks(|blocks| blocks.len()) as f64,
695+
"Total number of blocks stored in the stable memory.",
696+
)?;
697+
w.encode_gauge(
698+
"index_last_wait_time",
699+
with_state(|state| state.last_wait_time)
700+
.as_nanos()
701+
.min(f64::MAX as u128) as f64,
702+
"Last amount of time waited between two transactions fetch.",
703+
)?;
704+
Ok(())
705+
}
706+
643707
fn main() {}
644708

645709
#[cfg(test)]

0 commit comments

Comments
 (0)