Skip to content

Commit

Permalink
chore(BOUN-1301): add more metrics to canister (#2925)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rikonor authored Dec 3, 2024
1 parent e618620 commit b12bef3
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion rs/boundary_node/anonymization/backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -97,6 +100,20 @@ thread_local! {
).unwrap()
});

static GAUGE_CANISTER_STABLE_BYTES_TOTAL: RefCell<Gauge> = 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<Gauge> = 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<Registry> = RefCell::new({
let r = Registry::new();

Expand Down Expand Up @@ -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
});
}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit b12bef3

Please sign in to comment.