From b12bef3929842bc5223e8303755a6b7df03debd6 Mon Sep 17 00:00:00 2001 From: Or Ricon Date: Tue, 3 Dec 2024 11:40:01 -0500 Subject: [PATCH] chore(BOUN-1301): add more metrics to canister (#2925) This adds two more metrics to the anonymization canister: 1. size of stable memory in bytes 2. unix timestamp in seconds of the last time the canister upgrade --- .../anonymization/backend/src/lib.rs | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/rs/boundary_node/anonymization/backend/src/lib.rs b/rs/boundary_node/anonymization/backend/src/lib.rs index e73e54d26ad..e92fa3f45ff 100644 --- a/rs/boundary_node/anonymization/backend/src/lib.rs +++ b/rs/boundary_node/anonymization/backend/src/lib.rs @@ -6,7 +6,10 @@ use anonymization_interface::{ SubmitResponse, }; use candid::Principal; -use ic_cdk::{api::call::accept_message, caller, id, spawn, trap}; +use ic_cdk::{ + api::{call::accept_message, stable::WASM_PAGE_SIZE_IN_BYTES, time}, + caller, id, spawn, trap, +}; use ic_cdk_timers::set_timer_interval; use ic_nns_constants::REGISTRY_CANISTER_ID; use ic_stable_structures::{ @@ -97,6 +100,20 @@ thread_local! { ).unwrap() }); + static GAUGE_CANISTER_STABLE_BYTES_TOTAL: RefCell = RefCell::new({ + Gauge::new( + format!("{SERVICE_NAME}_canister_stable_bytes_total"), // name + "stable memory byte size", // help + ).unwrap() + }); + + static GAUGE_CANISTER_LAST_UPDATE_SECS: RefCell = RefCell::new({ + Gauge::new( + format!("{SERVICE_NAME}_last_update_secs"), // name + "timestamp in seconds of the last canister update", // help + ).unwrap() + }); + static METRICS_REGISTRY: RefCell = RefCell::new({ let r = Registry::new(); @@ -135,6 +152,16 @@ thread_local! { r.register(g).unwrap(); }); + GAUGE_CANISTER_STABLE_BYTES_TOTAL.with(|g| { + let g = Box::new(g.borrow().to_owned()); + r.register(g).unwrap(); + }); + + GAUGE_CANISTER_LAST_UPDATE_SECS.with(|g| { + let g = Box::new(g.borrow().to_owned()); + r.register(g).unwrap(); + }); + r }); } @@ -347,12 +374,18 @@ fn init(_arg: InitArg) { // Start timers timers(); + + // Set update time + GAUGE_CANISTER_LAST_UPDATE_SECS.with(|g| g.borrow_mut().set((time() as f64 / 1e9).trunc())); } #[ic_cdk::post_upgrade] fn post_upgrade() { // Start timers timers(); + + // Set update time + GAUGE_CANISTER_LAST_UPDATE_SECS.with(|g| g.borrow_mut().set((time() as f64 / 1e9).trunc())); } #[ic_cdk::inspect_message] @@ -440,6 +473,11 @@ fn http_request(request: HttpRequest) -> HttpResponse { GAUGE_CANISTER_CYCLES_BALANCE .with(|g| g.borrow_mut().set(ic_cdk::api::canister_balance() as f64)); + GAUGE_CANISTER_STABLE_BYTES_TOTAL.with(|g| { + g.borrow_mut() + .set((ic_cdk::api::stable::stable_size() * WASM_PAGE_SIZE_IN_BYTES) as f64) + }); + // Export metrics let bs = METRICS_REGISTRY.with(|r| { let mfs = r.borrow().gather();