blazegram 0.4.2

Telegram bot framework: clean chats, zero garbage, declarative screens, pure Rust MTProto.
Documentation
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Framework metrics for observability.
//!
//! Provides atomic counters, duration histograms, and a global singleton.
//!
//! # Examples
//!
//! ```
//! use blazegram::metrics::metrics;
//!
//! metrics().inc_updates();
//! {
//!     let _t = metrics().timer("handler");
//!     // … do work …
//! } // duration recorded on drop
//!
//! println!("{}", metrics().summary());
//! ```

use std::sync::OnceLock;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};

use dashmap::DashMap;

/// Maximum samples per label in the duration ring buffer.
/// Keeps memory bounded while still providing reasonable percentile accuracy.
const MAX_DURATION_SAMPLES: usize = 10_000;

/// Central metrics store.
pub struct Metrics {
    /// Total updates processed.
    pub updates_total: AtomicU64,
    /// Total errors encountered.
    pub errors_total: AtomicU64,
    /// Total Telegram API calls made.
    pub api_calls_total: AtomicU64,
    /// API calls avoided (diff/cache optimisations).
    pub api_calls_saved: AtomicU64,
    /// Currently active chat count.
    pub active_chats: AtomicU64,
    /// Duration ring buffers: label → capped ring of durations in microseconds.
    durations_us: DashMap<&'static str, RingBuffer>,
    /// Total count and sum per label (never reset, for accurate _count/_sum in Prometheus).
    duration_totals: DashMap<&'static str, (u64, u64)>,
}

/// Simple ring buffer that evicts oldest entries when full.
struct RingBuffer {
    data: Vec<u64>,
    pos: usize,
    full: bool,
}

impl RingBuffer {
    fn new() -> Self {
        Self {
            data: Vec::with_capacity(256), // start small, grow to MAX
            pos: 0,
            full: false,
        }
    }

    fn push(&mut self, value: u64) {
        if self.data.len() < MAX_DURATION_SAMPLES {
            self.data.push(value);
        } else {
            self.data[self.pos] = value;
            self.pos = (self.pos + 1) % MAX_DURATION_SAMPLES;
            self.full = true;
        }
    }

    fn samples(&self) -> &[u64] {
        &self.data
    }

    fn len(&self) -> usize {
        self.data.len()
    }

    fn is_empty(&self) -> bool {
        self.data.is_empty()
    }
}

impl Metrics {
    /// Create a fresh `Metrics` instance (all counters zero).
    pub fn new() -> Self {
        Self {
            updates_total: AtomicU64::new(0),
            errors_total: AtomicU64::new(0),
            api_calls_total: AtomicU64::new(0),
            api_calls_saved: AtomicU64::new(0),
            active_chats: AtomicU64::new(0),
            durations_us: DashMap::new(),
            duration_totals: DashMap::new(),
        }
    }

    /// Increment the total-updates counter.
    pub fn inc_updates(&self) {
        self.updates_total.fetch_add(1, Ordering::Relaxed);
    }

    /// Increment the total-errors counter.
    pub fn inc_errors(&self) {
        self.errors_total.fetch_add(1, Ordering::Relaxed);
    }

    /// Increment the API-calls counter.
    pub fn inc_api_calls(&self) {
        self.api_calls_total.fetch_add(1, Ordering::Relaxed);
    }

    /// Increment the API-calls-saved counter.
    pub fn inc_api_saved(&self) {
        self.api_calls_saved.fetch_add(1, Ordering::Relaxed);
    }

    /// Set the active-chats gauge.
    pub fn set_active_chats(&self, n: u64) {
        self.active_chats.store(n, Ordering::Relaxed);
    }

    /// Record a duration sample under `label`.
    /// Ring buffer is capped at MAX_DURATION_SAMPLES to prevent unbounded memory growth.
    pub fn record_duration(&self, label: &'static str, duration: Duration) {
        let us = duration.as_micros() as u64;
        self.durations_us
            .entry(label)
            .or_insert_with(RingBuffer::new)
            .push(us);
        // Track accurate total count/sum for Prometheus exposition
        let mut totals = self.duration_totals.entry(label).or_insert((0, 0));
        totals.0 += 1; // count
        totals.1 += us; // sum
    }

    /// Return a [`Timer`] guard that records elapsed time on drop.
    #[must_use]
    pub fn timer(&self, label: &'static str) -> Timer<'_> {
        Timer {
            metrics: self,
            label,
            start: Instant::now(),
        }
    }

    /// Prometheus-compatible text exposition format.
    pub fn prometheus(&self) -> String {
        let mut out = String::with_capacity(512);

        write_prom_counter(
            &mut out,
            "bg_updates_total",
            "Total updates processed",
            self.updates_total.load(Ordering::Relaxed),
        );
        write_prom_counter(
            &mut out,
            "bg_errors_total",
            "Total errors",
            self.errors_total.load(Ordering::Relaxed),
        );
        write_prom_counter(
            &mut out,
            "bg_api_calls_total",
            "Total API calls",
            self.api_calls_total.load(Ordering::Relaxed),
        );
        write_prom_counter(
            &mut out,
            "bg_api_calls_saved_total",
            "API calls saved by diff/cache",
            self.api_calls_saved.load(Ordering::Relaxed),
        );
        write_prom_gauge(
            &mut out,
            "bg_active_chats",
            "Number of active chats",
            self.active_chats.load(Ordering::Relaxed),
        );

        // Duration summaries per label
        for entry in self.durations_us.iter() {
            let label = *entry.key();
            let ring = entry.value();
            if ring.is_empty() {
                continue;
            }
            let mut sorted: Vec<u64> = ring.samples().to_vec();
            sorted.sort_unstable();

            // Use accurate totals for _count/_sum
            let (total_count, total_sum_us) = self
                .duration_totals
                .get(label)
                .map(|r| *r.value())
                .unwrap_or((sorted.len() as u64, sorted.iter().sum()));

            let name = format!("bg_duration_{}", sanitize_prom(label));
            out.push_str(&format!("# HELP {} Duration of {label} in seconds\n", name));
            out.push_str(&format!("# TYPE {} summary\n", name));
            out.push_str(&format!(
                "{name}{{quantile=\"0.5\"}} {:.6}\n",
                percentile_sec(&sorted, 50)
            ));
            out.push_str(&format!(
                "{name}{{quantile=\"0.95\"}} {:.6}\n",
                percentile_sec(&sorted, 95)
            ));
            out.push_str(&format!(
                "{name}{{quantile=\"0.99\"}} {:.6}\n",
                percentile_sec(&sorted, 99)
            ));
            out.push_str(&format!(
                "{name}_sum {:.6}\n",
                total_sum_us as f64 / 1_000_000.0
            ));
            out.push_str(&format!("{name}_count {total_count}\n"));
        }

        out
    }

    /// Human-readable metrics summary.
    pub fn summary(&self) -> String {
        let mut out = String::with_capacity(512);

        out.push_str(&format!(
            "Updates: {} | Errors: {} | API calls: {} (saved: {}) | Active chats: {}\n",
            self.updates_total.load(Ordering::Relaxed),
            self.errors_total.load(Ordering::Relaxed),
            self.api_calls_total.load(Ordering::Relaxed),
            self.api_calls_saved.load(Ordering::Relaxed),
            self.active_chats.load(Ordering::Relaxed),
        ));

        for entry in self.durations_us.iter() {
            let label = *entry.key();
            let ring = entry.value();
            if ring.is_empty() {
                continue;
            }
            let mut sorted: Vec<u64> = ring.samples().to_vec();
            sorted.sort_unstable();
            let (total_count, total_sum_us) = self
                .duration_totals
                .get(label)
                .map(|r| *r.value())
                .unwrap_or((sorted.len() as u64, sorted.iter().sum()));
            let avg_us = if total_count > 0 {
                total_sum_us / total_count
            } else {
                0
            };

            out.push_str(&format!(
                "  {label}: count={total_count}, avg={avg_us}µs, \
                 p50={}µs, p95={}µs, p99={}µs (window={})
",
                percentile_us(&sorted, 50),
                percentile_us(&sorted, 95),
                percentile_us(&sorted, 99),
                ring.len(),
            ));
        }

        out
    }
}

impl Default for Metrics {
    fn default() -> Self {
        Self::new()
    }
}

// ─── Timer guard ───

/// A guard that records elapsed duration when dropped.
pub struct Timer<'a> {
    metrics: &'a Metrics,
    label: &'static str,
    start: Instant,
}

impl<'a> Timer<'a> {
    /// Elapsed time since the timer was created.
    pub fn elapsed(&self) -> Duration {
        self.start.elapsed()
    }
}

impl<'a> Drop for Timer<'a> {
    fn drop(&mut self) {
        self.metrics
            .record_duration(self.label, self.start.elapsed());
    }
}

// ─── Global singleton ───

static GLOBAL_METRICS: OnceLock<Metrics> = OnceLock::new();

/// Returns a reference to the global `Metrics` instance.
pub fn metrics() -> &'static Metrics {
    GLOBAL_METRICS.get_or_init(Metrics::new)
}

// ─── Helpers ───

fn percentile_us(sorted: &[u64], p: u32) -> u64 {
    if sorted.is_empty() {
        return 0;
    }
    let idx = ((p as f64 / 100.0) * (sorted.len() as f64 - 1.0)).round() as usize;
    sorted[idx.min(sorted.len() - 1)]
}

fn percentile_sec(sorted: &[u64], p: u32) -> f64 {
    percentile_us(sorted, p) as f64 / 1_000_000.0
}

fn sanitize_prom(s: &str) -> String {
    s.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '_' {
                c
            } else {
                '_'
            }
        })
        .collect()
}

fn write_prom_counter(out: &mut String, name: &str, help: &str, value: u64) {
    out.push_str(&format!("# HELP {name} {help}\n"));
    out.push_str(&format!("# TYPE {name} counter\n"));
    out.push_str(&format!("{name} {value}\n"));
}

fn write_prom_gauge(out: &mut String, name: &str, help: &str, value: u64) {
    out.push_str(&format!("# HELP {name} {help}\n"));
    out.push_str(&format!("# TYPE {name} gauge\n"));
    out.push_str(&format!("{name} {value}\n"));
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[test]
    fn test_counters() {
        let m = Metrics::new();
        m.inc_updates();
        m.inc_updates();
        m.inc_errors();
        m.inc_api_calls();
        m.inc_api_calls();
        m.inc_api_calls();
        m.inc_api_saved();
        m.set_active_chats(42);

        assert_eq!(m.updates_total.load(Ordering::Relaxed), 2);
        assert_eq!(m.errors_total.load(Ordering::Relaxed), 1);
        assert_eq!(m.api_calls_total.load(Ordering::Relaxed), 3);
        assert_eq!(m.api_calls_saved.load(Ordering::Relaxed), 1);
        assert_eq!(m.active_chats.load(Ordering::Relaxed), 42);
    }

    #[test]
    fn test_record_duration() {
        let m = Metrics::new();
        m.record_duration("test_op", Duration::from_micros(100));
        m.record_duration("test_op", Duration::from_micros(200));
        m.record_duration("test_op", Duration::from_micros(300));

        let ring = m.durations_us.get("test_op").unwrap();
        assert_eq!(ring.value().len(), 3);
        assert_eq!(ring.value().samples(), &[100u64, 200, 300]);
        // Check accurate totals
        let totals = m.duration_totals.get("test_op").unwrap();
        assert_eq!(*totals.value(), (3, 600));
    }

    #[test]
    fn test_timer_records() {
        let m = Metrics::new();
        {
            let _t = m.timer("sleep_test");
            std::thread::sleep(Duration::from_millis(5));
        }
        let ring = m.durations_us.get("sleep_test").unwrap();
        assert_eq!(ring.value().len(), 1);
        // Should be at least 4ms (4000µs) — generous lower bound
        assert!(
            ring.value().samples()[0] >= 4_000,
            "duration was {} µs",
            ring.value().samples()[0]
        );
    }

    #[test]
    fn test_percentile() {
        // 0..100 → values 0,1,2,...,99
        let sorted: Vec<u64> = (0..100).collect();
        // p50 → index round(0.50 * 99) = round(49.5) = 50 → value 50
        assert_eq!(percentile_us(&sorted, 50), 50);
        // p95 → index round(0.95 * 99) = round(94.05) = 94 → value 94
        assert_eq!(percentile_us(&sorted, 95), 94);
        // p99 → index round(0.99 * 99) = round(98.01) = 98 → value 98
        assert_eq!(percentile_us(&sorted, 99), 98);
    }

    #[test]
    fn test_percentile_single() {
        let sorted = vec![42];
        assert_eq!(percentile_us(&sorted, 50), 42);
        assert_eq!(percentile_us(&sorted, 99), 42);
    }

    #[test]
    fn test_percentile_empty() {
        let sorted: Vec<u64> = vec![];
        assert_eq!(percentile_us(&sorted, 50), 0);
    }

    #[test]
    fn test_prometheus_output() {
        let m = Metrics::new();
        m.inc_updates();
        m.record_duration("handler", Duration::from_micros(500));

        let prom = m.prometheus();
        assert!(prom.contains("bg_updates_total 1"));
        assert!(prom.contains("# TYPE bg_updates_total counter"));
        assert!(prom.contains("bg_errors_total 0"));
        assert!(prom.contains("bg_duration_handler"));
        assert!(prom.contains("quantile=\"0.5\""));
    }

    #[test]
    fn test_summary_output() {
        let m = Metrics::new();
        m.inc_updates();
        m.inc_updates();
        m.inc_errors();
        m.set_active_chats(5);
        m.record_duration("process", Duration::from_micros(100));
        m.record_duration("process", Duration::from_micros(200));

        let s = m.summary();
        assert!(s.contains("Updates: 2"));
        assert!(s.contains("Errors: 1"));
        assert!(s.contains("Active chats: 5"));
        assert!(s.contains("process:"));
        assert!(s.contains("p50="));
    }

    #[test]
    fn test_global_singleton() {
        let a = metrics();
        let b = metrics();
        assert!(std::ptr::eq(a, b));
    }

    #[test]
    fn test_sanitize_prom() {
        assert_eq!(sanitize_prom("hello-world.foo"), "hello_world_foo");
        assert_eq!(sanitize_prom("ok_name"), "ok_name");
    }
}