spate-core 0.4.0

Engine for the Spate framework: records, operator chains, source/sink abstractions, checkpointing, backpressure, config, metrics, and the pipeline runtime. Applications should depend on the `spate` facade crate instead.
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! The runtime's wrapper around the feed it hands a source.
//!
//! [`MeteredFeed`] forwards every call to the backend unchanged and records
//! the calls that receive, renew, settle, hand back and publish units under
//! `spate_feed_*`, so every backend is counted the same way with no code of
//! its own. [`DeadLetterSampler`] publishes the depth of the feed's
//! dead-letter destination.

use super::{
    Confirmation, Delivery, DepthOf, DepthReading, Feed, FeedError, FeedErrorKind, FeedMetricsCtx,
    FeedWaker, Receipt, Unit,
};
use crate::clock::{SharedClock, Stopwatch};
use crate::metrics::{FeedMetrics, FeedOp};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// The default for
/// [`RuntimeOptions::dead_letter_read_interval`](crate::pipeline::RuntimeOptions::dead_letter_read_interval):
/// once a minute.
pub(crate) const DEAD_LETTER_READ_INTERVAL: Duration = Duration::from_secs(60);

/// A feed that records the unit and publish calls made through it.
///
/// Each call reaches the backend once, with the same arguments, and returns
/// what the backend returned.
pub(crate) struct MeteredFeed {
    inner: Arc<dyn Feed>,
    metrics: Arc<FeedMetrics>,
    clock: SharedClock,
}

impl MeteredFeed {
    /// Wrap `inner`, timing its calls on `clock`.
    pub(crate) fn new(
        inner: Arc<dyn Feed>,
        metrics: FeedMetrics,
        clock: SharedClock,
    ) -> MeteredFeed {
        MeteredFeed {
            inner,
            metrics: Arc::new(metrics),
            clock,
        }
    }

    fn call<T>(
        &self,
        op: FeedOp,
        call: impl FnOnce() -> Result<T, FeedError>,
    ) -> Result<T, FeedError> {
        let watch = Stopwatch::start(&*self.clock);
        let result = call();
        self.metrics.call(op, watch.elapsed(&*self.clock));
        if let Err(e) = &result {
            self.metrics.error(op, e.kind);
        }
        result
    }

    /// Record a call that settles a delivery. The source lets go of it once
    /// the feed accepts the call or answers Gone, and the ack rule has it make
    /// no later call with that receipt, so a plain count stays exact.
    fn settle(
        &self,
        op: FeedOp,
        accepted: fn(&FeedMetrics),
        call: impl FnOnce() -> Result<(), FeedError>,
    ) -> Result<(), FeedError> {
        let result = self.call(op, call);
        match &result {
            Ok(()) => {
                accepted(&self.metrics);
                self.metrics.released();
            }
            Err(e) if e.kind == FeedErrorKind::Gone => self.metrics.released(),
            Err(_) => {}
        }
        result
    }
}

impl Feed for MeteredFeed {
    fn backend(&self) -> &str {
        self.inner.backend()
    }

    fn set_waker(&self, waker: FeedWaker) {
        self.inner.set_waker(waker);
    }

    fn attach_metrics(&self, cx: FeedMetricsCtx) {
        self.inner.attach_metrics(cx);
    }

    fn receive(&self, max: usize) -> Result<Vec<Delivery>, FeedError> {
        let deliveries = self.call(FeedOp::Receive, || self.inner.receive(max))?;
        self.metrics.received(deliveries.len());
        Ok(deliveries)
    }

    fn renew(&self, receipt: Receipt) -> Result<(), FeedError> {
        let result = self.call(FeedOp::Renew, || self.inner.renew(receipt));
        match &result {
            Ok(()) => self.metrics.renewed(),
            Err(e) if e.kind == FeedErrorKind::Gone => self.metrics.released(),
            Err(_) => {}
        }
        result
    }

    fn acknowledge(&self, receipt: Receipt) -> Result<(), FeedError> {
        self.settle(FeedOp::Acknowledge, FeedMetrics::acknowledged, || {
            self.inner.acknowledge(receipt)
        })
    }

    fn fail(&self, receipt: Receipt, reason: &str) -> Result<(), FeedError> {
        self.settle(FeedOp::Fail, FeedMetrics::failed, || {
            self.inner.fail(receipt, reason)
        })
    }

    fn publish(&self, unit: Unit) -> Confirmation {
        let watch = Stopwatch::start(&*self.clock);
        let confirmation = self.inner.publish(unit);
        self.metrics
            .call(FeedOp::Publish, watch.elapsed(&*self.clock));
        let metrics = Arc::clone(&self.metrics);
        let clock = Arc::clone(&self.clock);
        confirmation.on_settle(move |outcome| {
            metrics.publish_settled(outcome, watch.elapsed(&*clock));
        })
    }

    fn read_depth(&self, of: DepthOf) -> Option<DepthReading> {
        self.inner.read_depth(of)
    }

    fn hands_back(&self) -> bool {
        self.inner.hands_back()
    }

    fn stop_receiving(&self) {
        self.inner.stop_receiving();
    }

    fn hand_back(&self, receipt: Receipt) -> Result<(), FeedError> {
        self.settle(FeedOp::HandBack, FeedMetrics::handed_back, || {
            self.inner.hand_back(receipt)
        })
    }

    fn close(&self, within: Duration) -> Option<Confirmation> {
        self.inner.close(within)
    }
}

/// Reads the depth of a feed's dead-letter destination every interval and
/// publishes each reading that settles as `spate_feed_dead_letter_units`.
///
/// A read that fails leaves the gauge at its last reading. A backend that
/// cannot count leaves it absent.
#[derive(Debug)]
pub(crate) struct DeadLetterSampler {
    interval: Duration,
    // `None` once the next read would fall past what an `Instant` holds.
    next: Option<Instant>,
    pending: Option<DepthReading>,
}

impl DeadLetterSampler {
    /// A sampler whose first read is due at `now`.
    pub(crate) fn new(interval: Duration, now: Instant) -> DeadLetterSampler {
        DeadLetterSampler {
            interval,
            next: Some(now),
            pending: None,
        }
    }

    /// Publish the pending reading if it has settled, and start the next one
    /// when it is due and none is pending. Never waits.
    pub(crate) fn sample(&mut self, feed: &MeteredFeed, now: Instant) {
        if let Some(reading) = &mut self.pending {
            match reading.try_outcome() {
                None => return,
                Some(Ok(depth)) => feed.metrics.set_dead_letters(
                    depth
                        .visible
                        .saturating_add(depth.delayed)
                        .saturating_add(depth.in_flight),
                ),
                Some(Err(e)) => tracing::warn!(
                    feed = feed.backend(),
                    error = %e,
                    "could not read the depth of the feed's dead-letter destination; \
                     `spate_feed_dead_letter_units` keeps its last reading"
                ),
            }
            self.pending = None;
        }
        if self.next.is_some_and(|due| now >= due) {
            self.next = now.checked_add(self.interval);
            self.pending = feed.read_depth(DepthOf::DeadLetters);
        }
    }
}

#[cfg(all(test, not(loom)))]
mod tests {
    use super::*;
    use crate::clock::TestClock;
    use crate::feed::{Confirmer, Depth, DepthReporter, confirmation, depth_reading};
    use crate::metrics::{ComponentLabels, Meter};
    use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusRecorder};
    use std::collections::VecDeque;
    use std::sync::Mutex;

    /// A backend that logs every call, answers the calls that return a
    /// `Result` from a script of refusals, and leaves each publish, close and,
    /// when it `counts`, depth read for the test to settle.
    #[derive(Default)]
    struct Fake {
        calls: Mutex<Vec<String>>,
        refusals: Mutex<VecDeque<FeedErrorKind>>,
        deliveries: Mutex<Vec<Delivery>>,
        confirmers: Mutex<Vec<Confirmer>>,
        reporters: Mutex<Vec<DepthReporter>>,
        counts: bool,
        // Moved by each renewal and publish, so a test can see what the call
        // took.
        call_takes: Mutex<Option<(Arc<TestClock>, Duration)>>,
    }

    impl Fake {
        fn log(&self, call: String) {
            self.calls.lock().unwrap().push(call);
        }

        fn answer(&self) -> Result<(), FeedError> {
            match self.refusals.lock().unwrap().pop_front() {
                Some(kind) => Err(FeedError::new(kind, "refused by the fake")),
                None => Ok(()),
            }
        }

        fn refuse(&self, kind: FeedErrorKind) {
            self.refusals.lock().unwrap().push_back(kind);
        }

        fn deliver(&self, receipts: impl IntoIterator<Item = u64>) {
            self.deliveries.lock().unwrap().extend(
                receipts
                    .into_iter()
                    .map(|n| Delivery::new(Unit::new(format!("u{n}"), Vec::new()), Receipt(n), 1)),
            );
        }

        fn take_time(&self) {
            if let Some((clock, by)) = &*self.call_takes.lock().unwrap() {
                clock.advance(*by);
            }
        }

        fn take_confirmer(&self) -> Confirmer {
            self.confirmers.lock().unwrap().remove(0)
        }

        fn take_reporter(&self) -> DepthReporter {
            self.reporters.lock().unwrap().remove(0)
        }
    }

    impl Feed for Fake {
        fn backend(&self) -> &str {
            "fake"
        }

        fn set_waker(&self, _waker: FeedWaker) {
            self.log("set_waker".into());
        }

        fn attach_metrics(&self, cx: FeedMetricsCtx) {
            self.log(format!(
                "attach_metrics {} {}",
                cx.meter.is_some(),
                cx.per_partition_detail
            ));
        }

        fn receive(&self, max: usize) -> Result<Vec<Delivery>, FeedError> {
            self.log(format!("receive {max}"));
            self.answer()?;
            Ok(std::mem::take(&mut *self.deliveries.lock().unwrap()))
        }

        fn renew(&self, receipt: Receipt) -> Result<(), FeedError> {
            self.log(format!("renew {}", receipt.0));
            self.take_time();
            self.answer()
        }

        fn acknowledge(&self, receipt: Receipt) -> Result<(), FeedError> {
            self.log(format!("acknowledge {}", receipt.0));
            self.answer()
        }

        fn fail(&self, receipt: Receipt, reason: &str) -> Result<(), FeedError> {
            self.log(format!("fail {} {reason}", receipt.0));
            self.answer()
        }

        fn publish(&self, unit: Unit) -> Confirmation {
            self.log(format!("publish {}", unit.id));
            self.take_time();
            let (confirmer, confirmation) = confirmation();
            self.confirmers.lock().unwrap().push(confirmer);
            confirmation
        }

        fn read_depth(&self, of: DepthOf) -> Option<DepthReading> {
            self.log(format!("read_depth {of:?}"));
            self.counts.then(|| {
                let (reporter, reading) = depth_reading();
                self.reporters.lock().unwrap().push(reporter);
                reading
            })
        }

        fn hands_back(&self) -> bool {
            self.log("hands_back".into());
            true
        }

        fn stop_receiving(&self) {
            self.log("stop_receiving".into());
        }

        fn hand_back(&self, receipt: Receipt) -> Result<(), FeedError> {
            self.log(format!("hand_back {}", receipt.0));
            self.answer()
        }

        fn close(&self, within: Duration) -> Option<Confirmation> {
            self.log(format!("close {within:?}"));
            let (confirmer, confirmation) = confirmation();
            self.confirmers.lock().unwrap().push(confirmer);
            Some(confirmation)
        }
    }

    /// A metered fake whose handles record into a recorder of the test's
    /// own, under `pipeline`.
    struct Rig {
        feed: MeteredFeed,
        fake: Arc<Fake>,
        clock: Arc<TestClock>,
        recorder: PrometheusRecorder,
        pipeline: &'static str,
    }

    impl Rig {
        fn new(pipeline: &'static str) -> Rig {
            Rig::with_fake(pipeline, Fake::default())
        }

        fn with_fake(pipeline: &'static str, fake: Fake) -> Rig {
            let recorder = PrometheusBuilder::new().build_recorder();
            let fake = Arc::new(fake);
            let clock = TestClock::frozen();
            let feed = metrics::with_local_recorder(&recorder, || {
                let labels = ComponentLabels::new(pipeline, "feed", "fake");
                let metrics = FeedMetrics::try_new(&labels).expect("free series");
                MeteredFeed::new(
                    Arc::clone(&fake) as Arc<dyn Feed>,
                    metrics,
                    Arc::clone(&clock) as SharedClock,
                )
            });
            Rig {
                feed,
                fake,
                clock,
                recorder,
                pipeline,
            }
        }

        /// The rendered value of `name` carrying the standard labels and
        /// then `extra`, such as `,op="renew"`; `None` when absent.
        fn value(&self, name: &str, extra: &str) -> Option<f64> {
            let series = format!(
                "{name}{{pipeline=\"{}\",component=\"feed\",component_type=\"fake\"{extra}}} ",
                self.pipeline
            );
            let text = self.recorder.handle().render();
            text.lines()
                .find_map(|line| line.strip_prefix(&series))
                .map(|value| value.parse().expect("a number"))
        }

        fn counter(&self, name: &str) -> f64 {
            self.value(name, "").unwrap_or(0.0)
        }

        fn errors(&self, op: &str, error_type: &str) -> f64 {
            let extra = format!(",op=\"{op}\",error_type=\"{error_type}\"");
            self.value("spate_feed_errors_total", &extra).unwrap_or(0.0)
        }

        fn held(&self) -> Option<f64> {
            self.value("spate_feed_held_units", "")
        }

        fn calls_timed(&self, op: &str) -> f64 {
            let extra = format!(",op=\"{op}\"");
            self.value("spate_feed_call_duration_seconds_count", &extra)
                .unwrap_or(0.0)
        }
    }

    /// Pins that every call reaches the backend once, with its arguments,
    /// and returns the backend's answer unchanged, including the answers of
    /// methods the trait gives defaults for.
    #[test]
    fn every_call_reaches_the_backend_once_with_its_answer() {
        let rig = Rig::new("metered-forwards");
        let (waker, _park) = crate::feed::wake_channel();
        rig.fake.deliver([7]);

        assert_eq!(rig.feed.backend(), "fake");
        rig.feed.attach_metrics(
            FeedMetricsCtx::new(Some(Meter::new("p", "feed", "fake"))).with_partition_detail(true),
        );
        rig.feed.set_waker(waker);
        let delivered = rig.feed.receive(4).unwrap();
        assert_eq!(delivered[0].receipt, Receipt(7));
        rig.fake.refuse(FeedErrorKind::Retryable);
        let refused = rig.feed.renew(Receipt(7)).unwrap_err();
        assert_eq!(
            refused,
            FeedError::new(FeedErrorKind::Retryable, "refused by the fake")
        );
        rig.feed.acknowledge(Receipt(7)).unwrap();
        rig.feed.fail(Receipt(8), "unreadable").unwrap();
        drop(rig.feed.publish(Unit::new("day-1", Vec::new())));
        assert!(rig.feed.read_depth(DepthOf::Feed).is_none());
        assert!(rig.feed.hands_back());
        rig.feed.stop_receiving();
        rig.feed.hand_back(Receipt(9)).unwrap();
        assert!(rig.feed.close(Duration::from_secs(2)).is_some());

        assert_eq!(
            *rig.fake.calls.lock().unwrap(),
            [
                "attach_metrics true true",
                "set_waker",
                "receive 4",
                "renew 7",
                "acknowledge 7",
                "fail 8 unreadable",
                "publish day-1",
                "read_depth Feed",
                "hands_back",
                "stop_receiving",
                "hand_back 9",
                "close 2s",
            ]
        );
    }

    /// Pins that the held gauge counts each delivery from its receipt until
    /// the feed accepts a call that settles it or answers Gone.
    #[test]
    fn the_held_gauge_counts_each_delivery_until_it_settles() {
        let rig = Rig::new("metered-held");
        assert_eq!(rig.held(), Some(0.0));
        assert!(rig.feed.receive(8).unwrap().is_empty());
        assert_eq!(rig.counter("spate_feed_units_received_total"), 0.0);

        rig.fake.deliver(1..=5);
        rig.feed.receive(8).unwrap();
        assert_eq!(rig.counter("spate_feed_units_received_total"), 5.0);
        assert_eq!(rig.held(), Some(5.0));

        rig.feed.acknowledge(Receipt(1)).unwrap();
        rig.feed.renew(Receipt(2)).unwrap();
        assert_eq!(rig.held(), Some(4.0), "a renewal keeps the delivery");
        rig.fake.refuse(FeedErrorKind::Gone);
        rig.feed.renew(Receipt(2)).unwrap_err();
        assert_eq!(rig.held(), Some(3.0));

        rig.feed.fail(Receipt(3), "unreadable").unwrap();
        rig.feed.hand_back(Receipt(4)).unwrap();
        assert_eq!(rig.held(), Some(1.0));

        assert_eq!(rig.counter("spate_feed_units_acknowledged_total"), 1.0);
        assert_eq!(rig.counter("spate_feed_units_failed_total"), 1.0);
        assert_eq!(rig.counter("spate_feed_units_handed_back_total"), 1.0);
        assert_eq!(rig.counter("spate_feed_renewals_total"), 1.0);
        assert_eq!(rig.errors("renew", "gone"), 1.0);
    }

    /// Pins that an answer other than Gone keeps the delivery held and counts
    /// nothing as settled, and that each error lands under its call and
    /// kind.
    #[test]
    fn a_refused_call_keeps_the_delivery_and_counts_its_kind() {
        let rig = Rig::new("metered-refused");
        rig.fake.deliver([1]);
        rig.feed.receive(8).unwrap();

        rig.fake.refuse(FeedErrorKind::Retryable);
        rig.feed.acknowledge(Receipt(1)).unwrap_err();
        rig.fake.refuse(FeedErrorKind::Refused);
        rig.feed.fail(Receipt(1), "unreadable").unwrap_err();
        rig.fake.refuse(FeedErrorKind::Fatal);
        rig.feed.hand_back(Receipt(1)).unwrap_err();
        rig.fake.refuse(FeedErrorKind::Retryable);
        rig.feed.renew(Receipt(1)).unwrap_err();
        rig.fake.refuse(FeedErrorKind::Fatal);
        rig.feed.receive(8).unwrap_err();

        assert_eq!(rig.held(), Some(1.0));
        assert_eq!(rig.errors("acknowledge", "retryable"), 1.0);
        assert_eq!(rig.errors("fail", "refused"), 1.0);
        assert_eq!(rig.errors("hand_back", "fatal"), 1.0);
        assert_eq!(rig.errors("renew", "retryable"), 1.0);
        assert_eq!(rig.errors("receive", "fatal"), 1.0);
        assert_eq!(rig.errors("receive", "retryable"), 0.0);
        for counter in [
            "spate_feed_units_acknowledged_total",
            "spate_feed_units_failed_total",
            "spate_feed_units_handed_back_total",
            "spate_feed_renewals_total",
        ] {
            assert_eq!(rig.counter(counter), 0.0, "{counter}");
        }

        rig.fake.refuse(FeedErrorKind::Gone);
        rig.feed.hand_back(Receipt(1)).unwrap_err();
        assert_eq!(rig.held(), Some(0.0), "Gone lets go of the delivery");
        assert_eq!(rig.counter("spate_feed_units_handed_back_total"), 0.0);
    }

    /// Pins that every recorded call is timed under its own `op`, across the
    /// backend's call, and that a publish dropped unseen counts nothing.
    #[test]
    fn every_call_is_timed_under_its_op() {
        let rig = Rig::new("metered-timed");
        *rig.fake.call_takes.lock().unwrap() =
            Some((Arc::clone(&rig.clock), Duration::from_millis(2)));
        rig.feed.receive(1).unwrap();
        rig.feed.receive(1).unwrap();
        rig.feed.renew(Receipt(1)).unwrap();
        rig.feed.acknowledge(Receipt(1)).unwrap();
        rig.feed.fail(Receipt(2), "x").unwrap();
        rig.feed.hand_back(Receipt(3)).unwrap();
        drop(rig.feed.publish(Unit::new("u", Vec::new())));

        assert_eq!(rig.calls_timed("receive"), 2.0);
        for op in ["renew", "acknowledge", "fail", "hand_back", "publish"] {
            assert_eq!(rig.calls_timed(op), 1.0, "{op}");
        }
        for op in ["renew", "publish"] {
            let extra = format!(",op=\"{op}\"");
            assert_eq!(
                rig.value("spate_feed_call_duration_seconds_sum", &extra),
                Some(0.002),
                "{op}"
            );
        }
        assert_eq!(rig.counter("spate_feed_units_published_total"), 0.0);
    }

    /// Pins that a publish whose backend drops its confirmer unsettled counts
    /// as a retryable publish error, on a check and on an await.
    #[test]
    fn a_publish_the_backend_drops_counts_as_a_retryable_error() {
        let rig = Rig::new("metered-dropped");
        let mut checked = rig.feed.publish(Unit::new("a", Vec::new()));
        drop(rig.fake.take_confirmer());
        let err = checked.try_outcome().expect("settled").unwrap_err();
        assert_eq!(err.kind, FeedErrorKind::Retryable);
        assert_eq!(rig.errors("publish", "retryable"), 1.0);

        let awaited = rig.feed.publish(Unit::new("b", Vec::new()));
        drop(rig.fake.take_confirmer());
        let rt = tokio::runtime::Builder::new_current_thread()
            .build()
            .unwrap();
        rt.block_on(awaited).unwrap_err();
        assert_eq!(rig.errors("publish", "retryable"), 2.0);
    }

    /// Pins that a publish is counted once, by outcome, when a check or an
    /// await first finds its confirmation settled, timed on the feed's clock,
    /// and that the caller sees the backend's outcome.
    #[test]
    fn a_publish_is_counted_when_its_confirmation_settles() {
        let rig = Rig::new("metered-publish");
        let mut checked = rig.feed.publish(Unit::new("a", Vec::new()));
        assert!(checked.try_outcome().is_none());
        assert_eq!(
            rig.value("spate_feed_publish_duration_seconds_count", ""),
            Some(0.0)
        );
        rig.clock.advance(Duration::from_secs(3));
        rig.fake.take_confirmer().confirm();
        assert!(matches!(checked.try_outcome(), Some(Ok(()))));
        assert!(matches!(checked.try_outcome(), Some(Ok(()))));
        assert_eq!(rig.counter("spate_feed_units_published_total"), 1.0);
        assert_eq!(
            rig.value("spate_feed_publish_duration_seconds_sum", ""),
            Some(3.0)
        );

        let awaited = rig.feed.publish(Unit::new("b", Vec::new()));
        rig.fake
            .take_confirmer()
            .reject(FeedError::new(FeedErrorKind::Refused, "too large"));
        let rt = tokio::runtime::Builder::new_current_thread()
            .build()
            .unwrap();
        let err = rt.block_on(awaited).unwrap_err();
        assert_eq!(err, FeedError::new(FeedErrorKind::Refused, "too large"));

        assert_eq!(rig.counter("spate_feed_units_published_total"), 1.0);
        assert_eq!(rig.errors("publish", "refused"), 1.0);
        assert_eq!(
            rig.value("spate_feed_publish_duration_seconds_count", ""),
            Some(2.0)
        );
    }
    fn dead_letters(n: u64) -> Depth {
        let mut depth = Depth::new("the dead letters", true);
        depth.visible = n;
        depth.delayed = 2;
        depth.in_flight = 1;
        depth.unsent = 100;
        depth
    }

    /// A metered fake that counts and leaves its depth reads pending.
    fn counting(pipeline: &'static str) -> Rig {
        Rig::with_fake(
            pipeline,
            Fake {
                counts: true,
                ..Fake::default()
            },
        )
    }

    /// Run one sample at `t0 + at` under the rig's recorder, where the gauge
    /// registers on its first reading.
    fn sample(rig: &Rig, sampler: &mut DeadLetterSampler, t0: Instant, at: Duration) {
        metrics::with_local_recorder(&rig.recorder, || sampler.sample(&rig.feed, t0 + at));
    }

    fn dead_letter_reads(rig: &Rig) -> usize {
        rig.fake
            .calls
            .lock()
            .unwrap()
            .iter()
            .filter(|call| *call == "read_depth DeadLetters")
            .count()
    }

    /// Pins the sampler's cadence: a read starts when due and only while
    /// none is pending, and a settled reading is published as the visible,
    /// delayed and in-flight units it counts.
    #[test]
    fn the_dead_letter_sampler_publishes_each_settled_reading() {
        let rig = counting("metered-dead-letters");
        let gauge = || rig.value("spate_feed_dead_letter_units", "");
        let t0 = Instant::now();
        let mut sampler = DeadLetterSampler::new(Duration::from_secs(60), t0);

        sample(&rig, &mut sampler, t0, Duration::ZERO);
        assert_eq!(dead_letter_reads(&rig), 1);
        sample(&rig, &mut sampler, t0, Duration::from_secs(61));
        assert_eq!(
            dead_letter_reads(&rig),
            1,
            "no second read while one is pending"
        );
        assert_eq!(gauge(), None, "absent until a reading settles");

        rig.fake.take_reporter().report(Ok(dead_letters(4)));
        sample(&rig, &mut sampler, t0, Duration::from_secs(62));
        assert_eq!(gauge(), Some(7.0));
        assert_eq!(
            dead_letter_reads(&rig),
            2,
            "the overdue read starts once the first settles"
        );

        rig.fake
            .take_reporter()
            .report(Err(FeedError::new(FeedErrorKind::Retryable, "throttled")));
        sample(&rig, &mut sampler, t0, Duration::from_secs(63));
        assert_eq!(gauge(), Some(7.0), "a failed read keeps the last reading");
        sample(&rig, &mut sampler, t0, Duration::from_secs(121));
        assert_eq!(
            dead_letter_reads(&rig),
            2,
            "the next read waits out the interval"
        );
        sample(&rig, &mut sampler, t0, Duration::from_secs(122));
        assert_eq!(dead_letter_reads(&rig), 3);
    }

    /// Pins that an interval too long for an `Instant` reads once, at
    /// startup, and never again, rather than overflowing, and that a zero
    /// interval reads again whenever no read is pending.
    #[test]
    fn the_interval_bounds_the_dead_letter_reads() {
        let rig = counting("metered-dead-letters-bounds");
        let t0 = Instant::now();
        let mut once = DeadLetterSampler::new(Duration::MAX, t0);
        sample(&rig, &mut once, t0, Duration::ZERO);
        rig.fake.take_reporter().report(Ok(dead_letters(0)));
        sample(&rig, &mut once, t0, Duration::from_secs(1));
        sample(&rig, &mut once, t0, Duration::from_secs(86_400));
        assert_eq!(rig.value("spate_feed_dead_letter_units", ""), Some(3.0));
        assert_eq!(dead_letter_reads(&rig), 1, "one read, at startup");

        let mut always = DeadLetterSampler::new(Duration::ZERO, t0);
        for _ in 0..3 {
            sample(&rig, &mut always, t0, Duration::ZERO);
            rig.fake.take_reporter().report(Ok(dead_letters(0)));
        }
        assert_eq!(dead_letter_reads(&rig), 4);
    }

    /// Pins that a backend that cannot count leaves the gauge absent, and
    /// is asked again each interval.
    #[test]
    fn a_feed_that_cannot_count_leaves_the_dead_letter_gauge_absent() {
        let rig = Rig::new("metered-no-depth");
        let t0 = Instant::now();
        let mut sampler = DeadLetterSampler::new(Duration::from_secs(60), t0);
        for at in [0, 1, 60] {
            sample(&rig, &mut sampler, t0, Duration::from_secs(at));
        }
        assert_eq!(dead_letter_reads(&rig), 2);
        assert_eq!(rig.value("spate_feed_dead_letter_units", ""), None);
    }
}