ic-analytics-sdk
Rust SDK for sending metrics from any Internet Computer canister to an ICAnalytics analytics canister.
Installation
Add the crate to your canister's Cargo.toml:
[]
= "0.2.2"
Or with cargo add:
Quick start
1. Initialise the client
AnalyticsClient holds the principal of your analytics canister. Create it
once and store it in a thread_local!.
use AnalyticsClient;
use Principal;
thread_local!
Find your analytics canister ID on the ICAnalytics dashboard after creating an analytics canister for your Dapp.
2. Record a metric
All recording is fire-and-forget — record_metric enqueues a one-way
inter-canister call that does not block your canister's execution.
use ;
ANALYTICS.with;
Metric types
| Variant | Payload | Behaviour |
|---|---|---|
Counter(i64) |
Delta (positive or negative) | Accumulated running total |
Gauge(f64) |
Absolute value | Overwritten on each call |
Histogram { x: f64, y: f64 } |
(x, y) data point | Appended with each call; use the canister timestamp as x for time-series data |
Log(String) |
Text entry | Appended with canister timestamp |
The key field is the stable identifier for a metric. The name field is the
human-readable label shown in the dashboard. The key is fixed on first write —
changing the metric type for an existing key will trap.
Examples
Counter
Increment a running total. Pass a negative delta to decrement.
a.record_metric?;
Gauge
Store the latest absolute value. Useful for memory usage, queue depth, etc.
let heap_mb = as f64 / 1_048_576.0;
a.record_metric?;
Histogram
Append an (x, y) data point. For time-series use, pass the canister timestamp
(nanoseconds) as x and the measured value as y. Useful for request latency,
throughput, or any trended data.
let now = time as f64; // nanoseconds since epoch
a.record_metric?;
Log
Append a timestamped text entry. Useful for events, errors, or audit trails.
a.record_metric?;
Full example
use Principal;
use ;
use ;
use RefCell;
thread_local!
API Reference
AnalyticsClient
Metric
MetricValue