nexo-poller 0.2.0

Generic polling runtime: cron schedules, retries, ack semantics.
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! Prometheus surface for the poller subsystem. Self-contained:
//! `main.rs` concatenates `render_prometheus()` into the same body
//! served at `/metrics` — same pattern as `nexo_auth::telemetry`.

use std::sync::atomic::{AtomicI64, AtomicU64, Ordering};
use std::sync::LazyLock;

use dashmap::DashMap;

const LATENCY_BUCKETS_MS: [u64; 8] = [50, 100, 250, 500, 1000, 2500, 5000, 10000];

#[derive(Clone, Debug, Hash, Eq, PartialEq)]
struct TickKey {
    kind: &'static str,
    agent: String,
    job_id: String,
    status: &'static str, // "ok" | "transient" | "permanent" | "skipped"
}

#[derive(Clone, Debug, Hash, Eq, PartialEq)]
struct LatencyKey {
    kind: &'static str,
    agent: String,
    job_id: String,
}

#[derive(Clone, Debug, Hash, Eq, PartialEq)]
struct ItemsKey {
    kind: &'static str,
    agent: String,
    job_id: String,
}

struct Histogram {
    buckets: [AtomicU64; 8],
    count: AtomicU64,
    sum_ms: AtomicU64,
}

impl Histogram {
    fn new() -> Self {
        Self {
            buckets: std::array::from_fn(|_| AtomicU64::new(0)),
            count: AtomicU64::new(0),
            sum_ms: AtomicU64::new(0),
        }
    }
    fn observe(&self, ms: u64) {
        self.count.fetch_add(1, Ordering::Relaxed);
        self.sum_ms.fetch_add(ms, Ordering::Relaxed);
        for (i, upper) in LATENCY_BUCKETS_MS.iter().enumerate() {
            if ms <= *upper {
                self.buckets[i].fetch_add(1, Ordering::Relaxed);
            }
        }
    }
}

static TICKS: LazyLock<DashMap<TickKey, AtomicU64>> = LazyLock::new(DashMap::new);
static LATENCY: LazyLock<DashMap<LatencyKey, Histogram>> = LazyLock::new(DashMap::new);
static ITEMS_SEEN: LazyLock<DashMap<ItemsKey, AtomicU64>> = LazyLock::new(DashMap::new);
static ITEMS_DISPATCHED: LazyLock<DashMap<ItemsKey, AtomicU64>> = LazyLock::new(DashMap::new);
static CONSEC_ERR: LazyLock<DashMap<String, AtomicI64>> = LazyLock::new(DashMap::new);
static BREAKER: LazyLock<DashMap<String, AtomicI64>> = LazyLock::new(DashMap::new);
static LEASE_TAKEOVERS: LazyLock<DashMap<String, AtomicU64>> = LazyLock::new(DashMap::new);

/// Phase 96 follow-up — generic named counter store the
/// `PollerHost::metric_inc` API + the daemon-side reverse-RPC
/// handler fan out into. Lets a subprocess poller emit arbitrary
/// per-plugin metrics that show up in `/metrics` without the
/// daemon needing per-kind hardcoded counters.
///
/// Key shape: `(name, sorted_label_pairs)`. Names + labels are
/// sanitized to the Prometheus naming alphabet
/// (`[a-zA-Z_:][a-zA-Z0-9_:]*` for names,
/// `[a-zA-Z_][a-zA-Z0-9_]*` for label keys) — bad inputs get
/// `_` substituted. Label value escaping reuses [`escape`].
static PLUGIN_COUNTERS: LazyLock<DashMap<PluginCounterKey, AtomicU64>> =
    LazyLock::new(DashMap::new);

/// Per-counter label cardinality cap. Beyond this, the
/// excess labels are silently dropped — a single misbehaving
/// plugin cannot blow up the daemon's `/metrics` payload.
const PLUGIN_COUNTER_MAX_LABELS: usize = 10;

#[derive(Clone, Debug, Hash, Eq, PartialEq)]
struct PluginCounterKey {
    name: String,
    /// Sorted-by-key pairs so two equivalent labelings hash to
    /// the same counter regardless of insertion order in the JSON
    /// object the plugin sent.
    labels: Vec<(String, String)>,
}

#[derive(Copy, Clone, Debug)]
pub enum BreakerState {
    Closed = 0,
    HalfOpen = 1,
    Open = 2,
}

pub fn inc_tick(kind: &'static str, agent: &str, job_id: &str, status: &'static str) {
    TICKS
        .entry(TickKey {
            kind,
            agent: agent.to_string(),
            job_id: job_id.to_string(),
            status,
        })
        .or_insert_with(|| AtomicU64::new(0))
        .fetch_add(1, Ordering::Relaxed);
}

pub fn observe_latency(kind: &'static str, agent: &str, job_id: &str, ms: u64) {
    LATENCY
        .entry(LatencyKey {
            kind,
            agent: agent.to_string(),
            job_id: job_id.to_string(),
        })
        .or_insert_with(Histogram::new)
        .observe(ms);
}

pub fn add_items_seen(kind: &'static str, agent: &str, job_id: &str, n: u32) {
    ITEMS_SEEN
        .entry(ItemsKey {
            kind,
            agent: agent.to_string(),
            job_id: job_id.to_string(),
        })
        .or_insert_with(|| AtomicU64::new(0))
        .fetch_add(n as u64, Ordering::Relaxed);
}

pub fn add_items_dispatched(kind: &'static str, agent: &str, job_id: &str, n: u32) {
    ITEMS_DISPATCHED
        .entry(ItemsKey {
            kind,
            agent: agent.to_string(),
            job_id: job_id.to_string(),
        })
        .or_insert_with(|| AtomicU64::new(0))
        .fetch_add(n as u64, Ordering::Relaxed);
}

pub fn set_consecutive_errors(job_id: &str, count: i64) {
    CONSEC_ERR
        .entry(job_id.to_string())
        .or_insert_with(|| AtomicI64::new(0))
        .store(count, Ordering::Relaxed);
}

pub fn set_breaker_state(job_id: &str, state: BreakerState) {
    BREAKER
        .entry(job_id.to_string())
        .or_insert_with(|| AtomicI64::new(0))
        .store(state as i64, Ordering::Relaxed);
}

pub fn inc_lease_takeover(job_id: &str) {
    LEASE_TAKEOVERS
        .entry(job_id.to_string())
        .or_insert_with(|| AtomicU64::new(0))
        .fetch_add(1, Ordering::Relaxed);
}

/// Phase 96 follow-up — generic named counter fan-out for
/// `PollerHost::metric_inc`. `name` is sanitized to the
/// Prometheus name alphabet, `labels` must be a JSON object whose
/// string values become label values; non-object inputs are
/// silently treated as labelless. Counter increments by 1 per call;
/// the host could later expose `inc_by(n)` if a use case justifies.
///
/// Cardinality is capped at [`PLUGIN_COUNTER_MAX_LABELS`] labels
/// per counter to prevent a misbehaving plugin from blowing up the
/// `/metrics` payload. Excess labels (sorted lexicographically) are
/// silently dropped.
pub fn inc_named_counter(name: &str, labels: &serde_json::Value) {
    let key = build_plugin_counter_key(name, labels);
    PLUGIN_COUNTERS
        .entry(key)
        .or_insert_with(|| AtomicU64::new(0))
        .fetch_add(1, Ordering::Relaxed);
}

fn build_plugin_counter_key(name: &str, labels: &serde_json::Value) -> PluginCounterKey {
    let mut pairs: Vec<(String, String)> = match labels {
        serde_json::Value::Object(map) => map
            .iter()
            .map(|(k, v)| {
                let key = sanitize_label_name(k);
                let val = match v {
                    serde_json::Value::String(s) => s.clone(),
                    serde_json::Value::Null => String::new(),
                    other => other.to_string(),
                };
                (key, val)
            })
            .collect(),
        _ => Vec::new(),
    };
    pairs.sort_by(|a, b| a.0.cmp(&b.0));
    if pairs.len() > PLUGIN_COUNTER_MAX_LABELS {
        pairs.truncate(PLUGIN_COUNTER_MAX_LABELS);
    }
    PluginCounterKey {
        name: sanitize_counter_name(name),
        labels: pairs,
    }
}

/// Prometheus counter name alphabet: `[a-zA-Z_:][a-zA-Z0-9_:]*`.
/// Replace invalid chars with `_`. Empty input → `_invalid_`.
fn sanitize_counter_name(input: &str) -> String {
    if input.is_empty() {
        return "_invalid_".to_string();
    }
    let mut out = String::with_capacity(input.len());
    for (i, c) in input.chars().enumerate() {
        let ok = if i == 0 {
            c.is_ascii_alphabetic() || c == '_' || c == ':'
        } else {
            c.is_ascii_alphanumeric() || c == '_' || c == ':'
        };
        out.push(if ok { c } else { '_' });
    }
    out
}

/// Prometheus label name alphabet: `[a-zA-Z_][a-zA-Z0-9_]*`.
/// Label names starting with `__` are reserved; we replace the
/// leading `_` with `x` so well-meaning plugins can't reach the
/// reserved namespace.
fn sanitize_label_name(input: &str) -> String {
    if input.is_empty() {
        return "_invalid_".to_string();
    }
    let mut out = String::with_capacity(input.len());
    for (i, c) in input.chars().enumerate() {
        let ok = if i == 0 {
            c.is_ascii_alphabetic() || c == '_'
        } else {
            c.is_ascii_alphanumeric() || c == '_'
        };
        out.push(if ok { c } else { '_' });
    }
    if out.starts_with("__") {
        out.replace_range(0..1, "x");
    }
    out
}

fn escape(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        match c {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '\n' => out.push_str("\\n"),
            _ => out.push(c),
        }
    }
    out
}

pub fn render_prometheus() -> String {
    let mut out = String::new();

    out.push_str("# HELP poller_ticks_total Total poller ticks by kind/agent/job/status.\n");
    out.push_str("# TYPE poller_ticks_total counter\n");
    {
        let mut rows: Vec<_> = TICKS
            .iter()
            .map(|e| (e.key().clone(), e.value().load(Ordering::Relaxed)))
            .collect();
        rows.sort_by(|a, b| {
            (a.0.kind, &a.0.agent, &a.0.job_id, a.0.status).cmp(&(
                b.0.kind,
                &b.0.agent,
                &b.0.job_id,
                b.0.status,
            ))
        });
        if rows.is_empty() {
            out.push_str("poller_ticks_total{agent=\"\",job_id=\"\",kind=\"\",status=\"\"} 0\n");
        }
        for (k, v) in rows {
            out.push_str(&format!(
                "poller_ticks_total{{agent=\"{}\",job_id=\"{}\",kind=\"{}\",status=\"{}\"}} {v}\n",
                escape(&k.agent),
                escape(&k.job_id),
                k.kind,
                k.status,
            ));
        }
    }

    out.push_str("# HELP poller_latency_ms Tick wall-clock duration histogram.\n");
    out.push_str("# TYPE poller_latency_ms histogram\n");
    {
        let mut keys: Vec<_> = LATENCY.iter().map(|e| e.key().clone()).collect();
        keys.sort_by(|a, b| (a.kind, &a.agent, &a.job_id).cmp(&(b.kind, &b.agent, &b.job_id)));
        if keys.is_empty() {
            for upper in LATENCY_BUCKETS_MS.iter() {
                out.push_str(&format!(
                    "poller_latency_ms_bucket{{agent=\"\",job_id=\"\",kind=\"\",le=\"{upper}\"}} 0\n"
                ));
            }
            out.push_str(
                "poller_latency_ms_bucket{agent=\"\",job_id=\"\",kind=\"\",le=\"+Inf\"} 0\n",
            );
            out.push_str("poller_latency_ms_sum{agent=\"\",job_id=\"\",kind=\"\"} 0\n");
            out.push_str("poller_latency_ms_count{agent=\"\",job_id=\"\",kind=\"\"} 0\n");
        }
        for k in keys {
            let Some(h) = LATENCY.get(&k) else { continue };
            let agent = escape(&k.agent);
            let job_id = escape(&k.job_id);
            for (i, upper) in LATENCY_BUCKETS_MS.iter().enumerate() {
                out.push_str(&format!(
                    "poller_latency_ms_bucket{{agent=\"{agent}\",job_id=\"{job_id}\",kind=\"{}\",le=\"{upper}\"}} {}\n",
                    k.kind,
                    h.buckets[i].load(Ordering::Relaxed)
                ));
            }
            let count = h.count.load(Ordering::Relaxed);
            out.push_str(&format!(
                "poller_latency_ms_bucket{{agent=\"{agent}\",job_id=\"{job_id}\",kind=\"{}\",le=\"+Inf\"}} {count}\n",
                k.kind
            ));
            out.push_str(&format!(
                "poller_latency_ms_sum{{agent=\"{agent}\",job_id=\"{job_id}\",kind=\"{}\"}} {}\n",
                k.kind,
                h.sum_ms.load(Ordering::Relaxed),
            ));
            out.push_str(&format!(
                "poller_latency_ms_count{{agent=\"{agent}\",job_id=\"{job_id}\",kind=\"{}\"}} {count}\n",
                k.kind,
            ));
        }
    }

    for (name, help, store) in [
        (
            "poller_items_seen_total",
            "Items the source returned this tick (whether dispatched or not).",
            &*ITEMS_SEEN,
        ),
        (
            "poller_items_dispatched_total",
            "Items the runner published downstream this tick.",
            &*ITEMS_DISPATCHED,
        ),
    ] {
        out.push_str(&format!("# HELP {name} {help}\n"));
        out.push_str(&format!("# TYPE {name} counter\n"));
        let mut rows: Vec<_> = store
            .iter()
            .map(|e| (e.key().clone(), e.value().load(Ordering::Relaxed)))
            .collect();
        rows.sort_by(|a, b| {
            (a.0.kind, &a.0.agent, &a.0.job_id).cmp(&(b.0.kind, &b.0.agent, &b.0.job_id))
        });
        if rows.is_empty() {
            out.push_str(&format!("{name}{{agent=\"\",job_id=\"\",kind=\"\"}} 0\n"));
        }
        for (k, v) in rows {
            out.push_str(&format!(
                "{name}{{agent=\"{}\",job_id=\"{}\",kind=\"{}\"}} {v}\n",
                escape(&k.agent),
                escape(&k.job_id),
                k.kind,
            ));
        }
    }

    out.push_str("# HELP poller_consecutive_errors Consecutive failures since last success.\n");
    out.push_str("# TYPE poller_consecutive_errors gauge\n");
    {
        let mut rows: Vec<_> = CONSEC_ERR
            .iter()
            .map(|e| (e.key().clone(), e.value().load(Ordering::Relaxed)))
            .collect();
        rows.sort_by(|a, b| a.0.cmp(&b.0));
        if rows.is_empty() {
            out.push_str("poller_consecutive_errors{job_id=\"\"} 0\n");
        }
        for (id, v) in rows {
            out.push_str(&format!(
                "poller_consecutive_errors{{job_id=\"{}\"}} {v}\n",
                escape(&id)
            ));
        }
    }

    out.push_str("# HELP poller_breaker_state 0=closed,1=half-open,2=open per job.\n");
    out.push_str("# TYPE poller_breaker_state gauge\n");
    {
        let mut rows: Vec<_> = BREAKER
            .iter()
            .map(|e| (e.key().clone(), e.value().load(Ordering::Relaxed)))
            .collect();
        rows.sort_by(|a, b| a.0.cmp(&b.0));
        if rows.is_empty() {
            out.push_str("poller_breaker_state{job_id=\"\"} 0\n");
        }
        for (id, v) in rows {
            out.push_str(&format!(
                "poller_breaker_state{{job_id=\"{}\"}} {v}\n",
                escape(&id)
            ));
        }
    }

    out.push_str(
        "# HELP poller_lease_takeovers_total Times another worker took over an expired lease.\n",
    );
    out.push_str("# TYPE poller_lease_takeovers_total counter\n");
    {
        let mut rows: Vec<_> = LEASE_TAKEOVERS
            .iter()
            .map(|e| (e.key().clone(), e.value().load(Ordering::Relaxed)))
            .collect();
        rows.sort_by(|a, b| a.0.cmp(&b.0));
        if rows.is_empty() {
            out.push_str("poller_lease_takeovers_total{job_id=\"\"} 0\n");
        }
        for (id, v) in rows {
            out.push_str(&format!(
                "poller_lease_takeovers_total{{job_id=\"{}\"}} {v}\n",
                escape(&id)
            ));
        }
    }

    // Phase 96 follow-up — emit every plugin-side counter fanned in
    // via `PollerHost::metric_inc`. Grouped by name so consumers of
    // the textual `/metrics` output see one HELP/TYPE pair per
    // counter family. Counters with empty store are not emitted
    // (they'd require a HELP without any sample lines).
    {
        let mut grouped: std::collections::BTreeMap<String, Vec<(Vec<(String, String)>, u64)>> =
            std::collections::BTreeMap::new();
        for entry in PLUGIN_COUNTERS.iter() {
            let key = entry.key().clone();
            let v = entry.value().load(Ordering::Relaxed);
            grouped
                .entry(key.name.clone())
                .or_default()
                .push((key.labels, v));
        }
        for (name, mut rows) in grouped {
            rows.sort_by(|a, b| a.0.cmp(&b.0));
            out.push_str(&format!(
                "# HELP {name} Plugin-emitted counter (via PollerHost::metric_inc).\n"
            ));
            out.push_str(&format!("# TYPE {name} counter\n"));
            for (labels, v) in rows {
                if labels.is_empty() {
                    out.push_str(&format!("{name} {v}\n"));
                } else {
                    let label_str = labels
                        .iter()
                        .map(|(k, val)| format!("{k}=\"{}\"", escape(val)))
                        .collect::<Vec<_>>()
                        .join(",");
                    out.push_str(&format!("{name}{{{label_str}}} {v}\n"));
                }
            }
        }
    }

    out
}

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

    #[test]
    fn render_includes_every_metric_when_empty() {
        let body = render_prometheus();
        for name in [
            "poller_ticks_total",
            "poller_latency_ms",
            "poller_items_seen_total",
            "poller_items_dispatched_total",
            "poller_consecutive_errors",
            "poller_breaker_state",
            "poller_lease_takeovers_total",
        ] {
            assert!(body.contains(&format!("# TYPE {name}")), "missing {name}");
        }
    }

    #[test]
    fn ticks_render_with_labels() {
        inc_tick("gmail", "ana", "ana_leads", "ok");
        inc_tick("gmail", "ana", "ana_leads", "ok");
        inc_tick("rss", "kate", "kate_blog", "transient");
        let body = render_prometheus();
        assert!(body.contains(
            "poller_ticks_total{agent=\"ana\",job_id=\"ana_leads\",kind=\"gmail\",status=\"ok\"} 2"
        ));
        assert!(body.contains(
            "poller_ticks_total{agent=\"kate\",job_id=\"kate_blog\",kind=\"rss\",status=\"transient\"} 1"
        ));
    }

    #[test]
    fn sanitize_counter_name_replaces_invalid_chars() {
        assert_eq!(
            sanitize_counter_name("gmail.tick.total"),
            "gmail_tick_total"
        );
        assert_eq!(sanitize_counter_name("gmail tick"), "gmail_tick");
        assert_eq!(sanitize_counter_name("9bad_lead"), "_bad_lead");
        assert_eq!(sanitize_counter_name(""), "_invalid_");
        assert_eq!(sanitize_counter_name("ok_name"), "ok_name");
        assert_eq!(
            sanitize_counter_name("namespace:counter"),
            "namespace:counter"
        );
    }

    #[test]
    fn sanitize_label_name_disallows_reserved_prefix() {
        assert_eq!(sanitize_label_name("__reserved"), "x_reserved");
        assert_eq!(sanitize_label_name("ok"), "ok");
        assert_eq!(sanitize_label_name("with.dot"), "with_dot");
        assert_eq!(sanitize_label_name(""), "_invalid_");
    }

    #[test]
    fn plugin_counter_increments_under_same_label_set() {
        inc_named_counter(
            "test_plugin_counter_one",
            &serde_json::json!({ "kind": "rss", "agent": "ana" }),
        );
        inc_named_counter(
            "test_plugin_counter_one",
            &serde_json::json!({ "agent": "ana", "kind": "rss" }),
        );
        // Differently-ordered keys collapse to the same key — both
        // ticks land on a single counter at value 2.
        let key = build_plugin_counter_key(
            "test_plugin_counter_one",
            &serde_json::json!({ "kind": "rss", "agent": "ana" }),
        );
        let v = PLUGIN_COUNTERS
            .get(&key)
            .map(|e| e.value().load(Ordering::Relaxed))
            .unwrap_or(0);
        assert_eq!(v, 2);
    }

    #[test]
    fn plugin_counter_label_cardinality_capped() {
        let mut labels = serde_json::Map::new();
        for i in 0..50 {
            labels.insert(format!("k{i}"), serde_json::json!(format!("v{i}")));
        }
        let key = build_plugin_counter_key(
            "test_plugin_counter_cap",
            &serde_json::Value::Object(labels),
        );
        assert_eq!(key.labels.len(), PLUGIN_COUNTER_MAX_LABELS);
        // Sorted-lex truncation keeps the first 10 keys (k0,k1,...,k17
        // with leading-zero sort caveat — k0,k1,k10..k17 actually,
        // because k10 < k2 lexically).
        assert!(key.labels.iter().any(|(k, _)| k == "k0"));
    }

    #[test]
    fn plugin_counter_renders_with_labels() {
        inc_named_counter(
            "test_plugin_counter_render",
            &serde_json::json!({ "channel": "whatsapp" }),
        );
        inc_named_counter(
            "test_plugin_counter_render",
            &serde_json::json!({ "channel": "telegram" }),
        );
        inc_named_counter(
            "test_plugin_counter_render",
            &serde_json::json!({ "channel": "whatsapp" }),
        );
        let body = render_prometheus();
        assert!(body.contains("# TYPE test_plugin_counter_render counter"));
        assert!(body.contains("test_plugin_counter_render{channel=\"whatsapp\"} 2"));
        assert!(body.contains("test_plugin_counter_render{channel=\"telegram\"} 1"));
    }

    #[test]
    fn plugin_counter_renders_labelless() {
        inc_named_counter("test_plugin_counter_no_labels", &serde_json::Value::Null);
        let body = render_prometheus();
        assert!(body.contains("test_plugin_counter_no_labels 1"));
    }

    #[test]
    fn plugin_counter_escapes_label_value_quotes() {
        inc_named_counter(
            "test_plugin_counter_escape",
            &serde_json::json!({ "topic": "with\"quote" }),
        );
        let body = render_prometheus();
        assert!(body.contains("test_plugin_counter_escape{topic=\"with\\\"quote\"} 1"));
    }

    #[test]
    fn breaker_gauge_round_trips() {
        set_breaker_state("ana_leads", BreakerState::Open);
        let body = render_prometheus();
        assert!(body.contains("poller_breaker_state{job_id=\"ana_leads\"} 2"));
    }

    #[test]
    fn escape_quotes_and_backslashes() {
        assert_eq!(escape(r#"a"b\c"#), r#"a\"b\\c"#);
    }
}