1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//! Rust SDK for sending metrics from any Internet Computer canister to an
//! IC Metrics analytics canister.
//!
//! # Installation
//!
//! ```toml
//! [dependencies]
//! ic-analytics-sdk = "0.2.3"
//! ```
//!
//! # Quick start
//!
//! ## 1. Initialise the client
//!
//! [`AnalyticsClient`] holds the principal of your analytics canister. Create
//! it once and store it in a `thread_local!`.
//!
//! ```no_run
//! use ic_analytics_sdk::AnalyticsClient;
//! use candid::Principal;
//!
//! thread_local! {
//! static ANALYTICS: AnalyticsClient = AnalyticsClient::new(
//! Principal::from_text("YOUR-ANALYTICS-CANISTER-ID").unwrap()
//! );
//! }
//! ```
//!
//! Find your analytics canister ID on the IC Metrics dashboard after creating
//! an analytics canister for your Dapp.
//!
//! ## 2. Record a metric
//!
//! All recording is **fire-and-forget** — [`AnalyticsClient::record_metric`]
//! enqueues a one-way inter-canister call that does not block your canister's
//! execution.
//!
//! ```no_run
//! # use ic_analytics_sdk::{AnalyticsClient, Metric, MetricValue};
//! # use candid::Principal;
//! # thread_local! { static ANALYTICS: AnalyticsClient = AnalyticsClient::new(Principal::anonymous()); }
//!
//!
//! ANALYTICS.with(|a| {
//! a.record_metric(Metric {
//! key: "user_signups".to_string(),
//! name: "User Sign-ups".to_string(),
//! value: MetricValue::Counter(1),
//! })
//! .expect("record_metric failed");
//! });
//! ```
//!
//! # Metric types
//!
//! | Variant | Payload | Behaviour |
//! |---------|---------|-----------|
//! | [`MetricValue::Counter`] | Delta (positive or negative) | Accumulated running total |
//! | [`MetricValue::Gauge`] | Absolute value | Overwritten on each call |
//! | [`MetricValue::Histogram`] | (x, y) data point | Appended with each call |
//! | [`MetricValue::TimeSeries`] | Measured value | Histogram with IC timestamp (ms) as x, set automatically |
//! | [`MetricValue::Log`] | 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.
//!
//! ```no_run
//! # use ic_analytics_sdk::{AnalyticsClient, Metric, MetricValue};
//! # use candid::Principal;
//! # let a = AnalyticsClient::new(Principal::anonymous());
//! a.record_metric(Metric {
//! key: "transfers_total".to_string(),
//! name: "Total Transfers".to_string(),
//! value: MetricValue::Counter(1),
//! })?;
//! # Ok::<(), String>(())
//! ```
//!
//! ## Gauge
//!
//! Store the latest absolute value. Useful for memory usage, queue depth, etc.
//!
//! ```no_run
//! # use ic_analytics_sdk::{AnalyticsClient, Metric, MetricValue};
//! # use candid::Principal;
//! # let a = AnalyticsClient::new(Principal::anonymous());
//! # let heap_mb = 0.0_f64;
//! a.record_metric(Metric {
//! key: "heap_usage_mb".to_string(),
//! name: "Heap Usage (MB)".to_string(),
//! value: MetricValue::Gauge(heap_mb),
//! })?;
//! # Ok::<(), String>(())
//! ```
//!
//! ## TimeSeries
//!
//! Append a data point timestamped automatically with the current IC time
//! (milliseconds). Use this when x should always be the current time.
//!
//! ```no_run
//! # use ic_analytics_sdk::{AnalyticsClient, Metric, MetricValue};
//! # use candid::Principal;
//! # let a = AnalyticsClient::new(Principal::anonymous());
//! a.record_metric(Metric {
//! key: "response_latency_ms".to_string(),
//! name: "Response Latency (ms)".to_string(),
//! value: MetricValue::TimeSeries(42.5),
//! })?;
//! # Ok::<(), String>(())
//! ```
//!
//! ## Histogram
//!
//! Append an (x, y) data point with explicit values. Useful when x is
//! something other than the current time (e.g. request size vs latency).
//!
//! ```no_run
//! # use ic_analytics_sdk::{AnalyticsClient, Metric, MetricValue};
//! # use candid::Principal;
//! # let a = AnalyticsClient::new(Principal::anonymous());
//! # let request_bytes = 0_u64;
//! # let latency_ms = 0.0_f64;
//! a.record_metric(Metric {
//! key: "size_vs_latency".to_string(),
//! name: "Size vs Latency".to_string(),
//! value: MetricValue::Histogram { x: request_bytes as f64, y: latency_ms },
//! })?;
//! # Ok::<(), String>(())
//! ```
//!
//! ## Log
//!
//! Append a timestamped text entry. Useful for events, errors, or audit trails.
//!
//! ```no_run
//! # use ic_analytics_sdk::{AnalyticsClient, Metric, MetricValue};
//! # use candid::Principal;
//! # let a = AnalyticsClient::new(Principal::anonymous());
//! a.record_metric(Metric {
//! key: "canister_events".to_string(),
//! name: "Canister Events".to_string(),
//! value: MetricValue::Log("[INFO] Upgrade completed to v2.1.0".to_string()),
//! })?;
//! # Ok::<(), String>(())
//! ```
use ;
use ;
/// The value carried by an incoming metric record.
/// Determines how the metric is stored and aggregated.
/// An incoming metric record.
/// The aggregated metric as stored in the canister, keyed by `key`.
/// A page of log entries returned by `get_log_page`.
/// A page of histogram data points returned by `get_histogram_page`.
/// The result produced by running a transformer script.
/// Metadata returned when listing or fetching a transformer definition.
/// Client for recording metrics to an IC Metrics analytics canister.
///
/// Create once per canister and store in a `thread_local!`. All calls are
/// fire-and-forget — they enqueue a one-way inter-canister call and return
/// immediately without blocking execution.
///
/// # Example
///
/// ```no_run
/// use ic_analytics_sdk::AnalyticsClient;
/// use candid::Principal;
///
/// thread_local! {
/// static ANALYTICS: AnalyticsClient = AnalyticsClient::new(
/// Principal::from_text("YOUR-ANALYTICS-CANISTER-ID").unwrap()
/// );
/// }
/// ```