canistergeekicrust

canistergeek_ic_rust is the open-source tool for Internet Computer to track your project canisters cycles and memory status and collect log messages.

canistergeek_ic_rust can be integrated into your canisters as rust library which exposes following modules: - canistergeek_ic_rust::monitor - public module that collects the data for specific canister by 5 minutes intervals. - canistergeek_ic_rust::logger - public module that collects log messages for specific canister.

canistergeek_ic_rust should be used together with Canistergeek-IC-JS - Javascript library that fetches the data from canisters, performs all necessary calculations and displays it on a webpage

Memory consumption

API change in 0.3.1 version

Starting from version 0.3.1 existing getCanistergeekInformation method will replace getCanisterLog method

API change in 0.3.0 version

Starting from version 0.3.0 your canister need to supported new API methods: - updateCanistergeekInformation method will replace collectCanisterMetrics method - getCanistergeekInformation method will replace getCanisterMetrics and getCanisterLog methods

New methods provide an opportunity to evolve API in the future.

Metrics

Collecting the data

Data can be collected in two ways: automatically and manually

  1. Manually by calling updateCanistergeekInformation public method of your canister.
  2. Automatically by calling canistergeek_ic_rust::update_information or canistergeek_ic_rust::monitor::collect_metrics in "update" methods in your canister to guarantee desired "Collect metrics" frequency.
    In some cases you may want to collect metrics in every "update" method to get the full picture in realtime and see how "update" methods influence canister price and capacity.

Update calls

Monitor collects the number of canister update calls

Cycles

Monitor collects how many cycles left at particular time using ic_cdk::api::canister_balance().

Memory

Monitor collects how many memory bytes the canister consumes at particular time using ic_cdk::api::stable::stable64_size() * WASM_PAGE_SIZE + core::arch::wasm32::memory_size(0) * WASM_PAGE_SIZE.

Heap Memory

Monitor collects how many heap memory bytes the canister consumes at particular time using core::arch::wasm32::memory_size(0) * WASM_PAGE_SIZE.

Logger

Collecting log messages

Log messages can be collected by calling canistergeek_ic_rust::logger::log_message(message: String); method in "update" methods in your canister.

Log messages

Logger collects time/message pairs with a maximum message length of 4096 characters.

Default number of messages (10000) can be overridden with corresponding method in realtime.

Installation

In file Cargo.toml your project, add dependency on crate: toml canistergeek_ic_rust = "0.3.1"

Usage

Implement public methods in the canister in order to query collected data and optionally force collecting the data

```rust

// CANISTERGEEK API

/// Returns canister information based on passed parameters. /// Called from browser. ///

[iccdkmacros::query(name = "getCanistergeekInformation")]

pub async fn getcanistergeekinformation(request: canistergeekicrust::apitype::GetInformationRequest) -> canistergeekicrust::apitype::GetInformationResponse<'static> { validatecaller(); canistergeekicrust::getinformation(request) }

/// Updates canister information based on passed parameters at current time. /// Called from browser or any canister update method. ///

[iccdkmacros::update(name = "updateCanistergeekInformation")]

pub async fn updatecanistergeekinformation(request: canistergeekicrust::apitype::UpdateInformationRequest) -> () { validatecaller(); canistergeekicrust::update_information(request); }

fn validate_caller() -> () { // limit access here! } ```

Adjust "update" methods

Call canistergeek_ic_rust::monitor::collect_metrics() (it is a shortcut for generic method canistergeek_ic_rust::update_information(canistergeek_ic_rust::api_type::UpdateInformationRequest {metrics=Some(canistergeek_ic_rust::api_type::CollectMetricsRequestType::normal)});) method in all "update" methods in your canister in order to automatically collect all data.

Add post/pre upgrade hooks

Implement pre/post upgrade hooks. This step is necessary to save collected data between canister upgrades.

```rust

[iccdkmacros::pre_upgrade]

fn preupgradefunction() { let monitorstabledata = canistergeekicrust::monitor::preupgradestabledata(); let loggerstabledata = canistergeekicrust::logger::preupgradestabledata(); iccdk::storage::stablesave((monitorstabledata, loggerstabledata)); }

[iccdkmacros::post_upgrade]

fn postupgradefunction() { let stabledata: Result<(canistergeekicrust::monitor::PostUpgradeStableData, canistergeekicrust::logger::PostUpgradeStableData), String> = iccdk::storage::stablerestore(); match stabledata { Ok((monitorstabledata, loggerstabledata)) => { canistergeekicrust::monitor::postupgradestabledata(monitorstabledata); canistergeekicrust::logger::postupgradestabledata(loggerstabledata); } Err(_) => {} } } ```

Add candid api declaration to did file

In your canister did file your_canister.did, add next declaration:

```candid ...

type UpdateCallsAggregatedData = vec nat64; type NumericEntity = record { avg: nat64; first: nat64; last: nat64; max: nat64; min: nat64; }; type Nanos = nat64; type MetricsGranularity = variant { daily; hourly; }; type LogMessagesData = record { message: text; timeNanos: Nanos; }; type HourlyMetricsData = record { canisterCycles: CanisterCyclesAggregatedData; canisterHeapMemorySize: CanisterHeapMemoryAggregatedData; canisterMemorySize: CanisterMemoryAggregatedData; timeMillis: int; updateCalls: UpdateCallsAggregatedData; }; type GetMetricsParameters = record { dateFromMillis: nat; dateToMillis: nat; granularity: MetricsGranularity; }; type GetLogMessagesParameters = record { count: nat32; filter: opt GetLogMessagesFilter; fromTimeNanos: opt Nanos; }; type GetLogMessagesFilter = record { analyzeCount: nat32; messageContains: opt text; messageRegex: opt text; }; type GetLatestLogMessagesParameters = record { count: nat32; filter: opt GetLogMessagesFilter; upToTimeNanos: opt Nanos; }; type DailyMetricsData = record { canisterCycles: NumericEntity; canisterHeapMemorySize: NumericEntity; canisterMemorySize: NumericEntity; timeMillis: int; updateCalls: nat64; }; type CanisterMetricsData = variant { daily: vec DailyMetricsData; hourly: vec HourlyMetricsData; }; type CanisterMetrics = record {data: CanisterMetricsData;}; type CanisterMemoryAggregatedData = vec nat64; type CanisterLogResponse = variant { messages: CanisterLogMessages; messagesInfo: CanisterLogMessagesInfo; }; type CanisterLogRequest = variant { getLatestMessages: GetLatestLogMessagesParameters; getMessages: GetLogMessagesParameters; getMessagesInfo; }; type CanisterLogMessagesInfo = record { count: nat32; features: vec opt CanisterLogFeature; firstTimeNanos: opt Nanos; lastTimeNanos: opt Nanos; }; type CanisterLogMessages = record { data: vec LogMessagesData; lastAnalyzedMessageTimeNanos: opt Nanos; }; type CanisterLogFeature = variant { filterMessageByContains; filterMessageByRegex; }; type CanisterHeapMemoryAggregatedData = vec nat64; type CanisterCyclesAggregatedData = vec nat64; type GetInformationRequest = record { version: bool; status: opt StatusRequest; metrics: opt MetricsRequest; logs: opt CanisterLogRequest; }; type GetInformationResponse = record { version: opt nat; status: opt StatusResponse; metrics: opt MetricsResponse; logs: opt CanisterLogResponse; }; type StatusRequest = record { cycles: bool; memorysize: bool; heapmemorysize: bool; }; type StatusResponse = record { cycles: opt nat64; memorysize: opt nat64; heapmemorysize: opt nat64; }; type MetricsRequest = record { parameters: GetMetricsParameters; }; type MetricsResponse = record { metrics: opt CanisterMetrics; }; type UpdateInformationRequest = record { metrics: opt CollectMetricsRequestType; }; type CollectMetricsRequestType = variant { normal; force; }; service : { ... updateCanistergeekInformation: (UpdateInformationRequest) -> (); getCanistergeekInformation: (GetInformationRequest) -> (GetInformationResponse) query; }

```

LIMIT ACCESS TO YOUR DATA

🔴🔴🔴 We highly recommend limiting access by checking caller principal 🔴🔴🔴

rust fn validate_caller() { match ic_cdk::export::Principal::from_text("hozae-racaq-aaaaa-aaaaa-c") { Ok(caller) if caller == ic_cdk::caller() => (), _ => ic_cdk::trap("Invalid caller") } }

Full Example

```rust

[iccdkmacros::pre_upgrade]

fn preupgradefunction() { let monitorstabledata = canistergeekicrust::monitor::preupgradestabledata(); let loggerstabledata = canistergeekicrust::logger::preupgradestabledata(); iccdk::storage::stablesave((monitorstabledata, loggerstabledata)); }

[iccdkmacros::post_upgrade]

fn postupgradefunction() { let stabledata: Result<(canistergeekicrust::monitor::PostUpgradeStableData, canistergeekicrust::logger::PostUpgradeStableData), String> = iccdk::storage::stablerestore(); match stabledata { Ok((monitorstabledata, loggerstabledata)) => { canistergeekicrust::monitor::postupgradestabledata(monitorstabledata); canistergeekicrust::logger::postupgradestabledata(loggerstabledata); } Err(_) => {} } }

[iccdkmacros::query(name = "getCanistergeekInformation")]

pub async fn getcanistergeekinformation(request: canistergeekicrust::apitype::GetInformationRequest) -> canistergeekicrust::apitype::GetInformationResponse<'static> { validatecaller(); canistergeekicrust::getinformation(request) }

[iccdkmacros::update(name = "updateCanistergeekInformation")]

pub async fn updatecanistergeekinformation(request: canistergeekicrust::apitype::UpdateInformationRequest) -> () { validatecaller(); canistergeekicrust::update_information(request); }

fn validatecaller() -> () { match iccdk::export::Principal::fromtext("hozae-racaq-aaaaa-aaaaa-c") { Ok(caller) if caller == iccdk::caller() => (), _ => ic_cdk::trap("Invalid caller") } }

[iccdkmacros::update(name = "doThis")]

pub async fn dothis() -> () { canistergeekicrust::monitor::collectmetrics(); canistergeekicrust::logger::logmessage(String::from("dothis")); // rest part of the your method... } ```