zebrad 7.2.0

The Zcash Foundation's independent, consensus-compatible implementation of a Zcash node
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
//! Shapes every metric for Sentry, which stores each value it receives.
//!
//! Recording a counter or gauge only updates atomics in a sharded registry: there is no lock on a
//! kept handle, no allocation for an existing label set, and no call into the Sentry SDK. A
//! histogram value takes a short lock for its label set's sample. Once per
//! [`FLUSH_INTERVAL`], [`SentryPolicy::flush_if`] turns what was recorded into one batch of
//! [`SentryMetric`]s and gives it to an adapter that sends it:
//!
//! - each counter's increase since it was last sent, for each label set that increased;
//! - each gauge's latest value, for each label set whose value changed since it was last sent, or
//!   that hasn't been sent for [`GAUGE_REFRESH_FLUSHES`] flushes;
//! - at most [`HISTOGRAM_CAP`] of each histogram's values for each label set, chosen uniformly at
//!   random from the values recorded since the previous flush. Each label set only keeps that
//!   sample, so its memory doesn't grow with how often it is recorded.
//!
//! A batch holds at most [`FLUSH_BUDGET`] metrics. When more are due, counters go first, then
//! gauges that changed, then gauges due for a refresh, then histogram values. Counters and gauges
//! that don't fit stay due and take turns in later flushes, so a counter's increase is delayed,
//! not lost. Histogram values that don't fit are dropped.
//!
//! A flush sends nothing while the Sentry transport is still sending what earlier flushes queued.
//! Its counters and gauges stay due, and its histogram values are dropped, so metrics never take
//! more than their share of a slow or stalled transport's queue.
//!
//! The policy keeps at most [`LABEL_SET_CAP`] label sets for each metric name, and doesn't send
//! the values recorded for any others, so a label that keeps taking new values can't grow the
//! policy's memory or volume without bound. Other backends keep every label set.
//!
//! The `zebrad.sentry.metrics.dropped.total` counter counts the due values left out of a full
//! flush or of a flush the transport wasn't ready for, and the values recorded for label sets over
//! the cap. The policy records it through the `metrics` facade, so every backend receives it, and
//! logs a warning when it starts dropping values.
//!
//! The histograms in [`SENT_DIRECTLY`] are skipped, because their callers send each value to Sentry
//! themselves.

use std::{
    cell::Cell,
    collections::HashMap,
    mem,
    ops::AddAssign,
    sync::{atomic::Ordering, Arc, Mutex, MutexGuard, PoisonError},
    time::{Duration, Instant},
};

use metrics::{
    atomics::AtomicU64, Counter, CounterFn, Gauge, GaugeFn, Histogram, HistogramFn, Key, KeyName,
    Metadata, Recorder, SharedString, Unit,
};
use metrics_util::registry::{Registry, Storage};

/// How often the policy sends what was recorded to Sentry.
///
/// Each counter and gauge label set sends at most one value per interval, so the volume Sentry
/// stores is inversely proportional to this.
pub(crate) const FLUSH_INTERVAL: Duration = Duration::from_secs(60);

/// The most values one histogram label set sends per [`FLUSH_INTERVAL`].
///
/// An initial sync records some histograms for every block, faster than Sentry's transport can
/// send them. Histograms recorded about once per block near the tip stay under this cap.
pub(crate) const HISTOGRAM_CAP: usize = 20;

/// The most metrics the Sentry SDK puts in one envelope. Its batcher queues the envelope for
/// sending as soon as it holds this many, on the thread that captured the last one.
pub(crate) const ENVELOPE_METRICS: usize = 100;

/// How many envelopes the Sentry SDK's transport queues for sending. Events, logs, sessions, and
/// metrics share this queue, and the SDK drops an envelope that arrives while it is full.
pub(crate) const TRANSPORT_QUEUE_ENVELOPES: usize = 30;

/// The most metrics one flush sends.
///
/// 15 envelopes of 100 metrics is half of the 30-envelope transport queue. A flush only sends once
/// the transport has sent what earlier flushes queued, so metrics hold at most these 15 places,
/// plus at exit one partial envelope left by the flush before. At least 14 places stay free for
/// events, logs, and sessions, even while the transport sends nothing.
pub(crate) const FLUSH_BUDGET: usize = TRANSPORT_QUEUE_ENVELOPES / 2 * ENVELOPE_METRICS;

/// The most label sets the policy keeps for one metric name.
///
/// Zebra's labels take a fixed set of values, and every metric stays under this cap on a busy
/// node. A metric that reaches it has a label that keeps taking new values.
pub(crate) const LABEL_SET_CAP: usize = 256;

/// How many flushes an unchanged gauge waits before it is sent again, so that Sentry charts show
/// steady values: every 5 minutes.
pub(crate) const GAUGE_REFRESH_FLUSHES: u64 = 5;

/// The least time between two warnings that the policy dropped values.
const DROP_WARNING_INTERVAL: Duration = Duration::from_secs(60 * 60);

/// Histograms that the policy doesn't send, because their callers send each value to Sentry with
/// attributes that would make a new Prometheus series for every value, such as a block height.
pub(crate) const SENT_DIRECTLY: &[&str] = &["zcash.chain.tip.block_delay_seconds"];

/// A value for the adapter to send to Sentry.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct SentryMetric {
    /// How Sentry stores the value.
    pub(crate) metric_type: MetricType,
    /// The metric's name, as the code records it.
    pub(crate) name: String,
    pub(crate) value: f64,
    /// The unit the metric was described with.
    pub(crate) unit: Option<Unit>,
    /// The metric's labels.
    pub(crate) attributes: Vec<(String, AttributeValue)>,
}

/// How Sentry stores a [`SentryMetric`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum MetricType {
    /// A counter's increase since it was last sent.
    Counter,
    /// A gauge's latest value.
    Gauge,
    /// One recorded histogram value.
    Distribution,
}

/// The value of a [`SentryMetric`] attribute.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub(crate) enum AttributeValue {
    String(String),
}

/// A [`Recorder`] that keeps every metric for [`SentryPolicy::flush_if`] to send to Sentry.
pub(crate) struct SentryPolicy {
    registry: Registry<Key, PolicyStorage>,

    /// How many label sets the registry keeps for each metric name. Only registering a label set
    /// the registry doesn't have locks this.
    label_sets: Mutex<LabelSets>,

    /// Counts the values recorded for the label sets the registry doesn't keep.
    untracked: Arc<Untracked>,

    /// The unit of each described metric, by name.
    units: Mutex<HashMap<String, Unit>>,

    /// Held for a whole [`flush_if`](Self::flush_if), so that the flush at shutdown waits for a
    /// periodic flush that has taken counter increases but not sent them yet.
    flushing: Mutex<FlushState>,
}

thread_local! {
    /// Whether this thread is in a [`SentryPolicy::flush_if`], so that the panic hook's flush
    /// returns instead of waiting for a flush that panicked on the same thread.
    static IN_FLUSH: Cell<bool> = const { Cell::new(false) };
}

impl Default for SentryPolicy {
    fn default() -> Self {
        Self {
            registry: Registry::new(PolicyStorage),
            label_sets: Mutex::default(),
            untracked: Arc::default(),
            units: Mutex::default(),
            flushing: Mutex::default(),
        }
    }
}

impl SentryPolicy {
    /// Gives `send` the metrics that are due, as one batch of at most [`FLUSH_BUDGET`] metrics.
    #[cfg(test)]
    pub(crate) fn flush(&self, send: impl FnOnce(Vec<SentryMetric>)) {
        self.flush_if(|| true, send);
    }

    /// Gives `send` the metrics that are due, as one batch of at most [`FLUSH_BUDGET`] metrics, if
    /// `transport_ready` returns true. Otherwise gives `send` an empty batch: counters and gauges
    /// stay due, and histogram values are dropped.
    ///
    /// Calls `transport_ready` once any flush in progress has finished, so that it can wait for the
    /// transport to send that flush. Builds the batch before calling `send`, so `send` can record
    /// metrics, and this can run from the panic hook.
    pub(crate) fn flush_if(
        &self,
        transport_ready: impl FnOnce() -> bool,
        send: impl FnOnce(Vec<SentryMetric>),
    ) {
        if IN_FLUSH.get() {
            return;
        }

        let mut state = self.flushing.lock().unwrap_or_else(PoisonError::into_inner);
        IN_FLUSH.set(true);

        let mut dropped = Dropped {
            label_set_cap: self.untracked.take(),
            ..Dropped::default()
        };
        let batch = if transport_ready() {
            let (batch, left_out) = self.take_batch(&mut state, FLUSH_BUDGET);
            dropped.flush_budget = left_out;
            batch
        } else {
            let (_, left_out) = self.take_batch(&mut state, 0);
            dropped.transport_backlog = left_out;
            Vec::new()
        };
        let capped = mem::take(&mut self.lock_label_sets().newly_capped);

        dropped.record();
        if let Some((dropped, capped)) = state.warning.add(dropped, capped, Instant::now()) {
            tracing::warn!(
                flush_budget = dropped.flush_budget,
                transport_backlog = dropped.transport_backlog,
                label_set_cap = dropped.label_set_cap,
                capped_metrics = ?capped,
                "some metric values weren't sent to Sentry; \
                 zebrad.sentry.metrics.dropped.total counts them by reason",
            );
        }

        send(batch);
        IN_FLUSH.set(false);
    }

    /// Takes the metrics that are due, and returns at most `room` of them, with how many didn't
    /// fit. Only [`flush_if`](Self::flush_if) calls this.
    fn take_batch(&self, state: &mut FlushState, mut room: usize) -> (Vec<SentryMetric>, u64) {
        state.flushes += 1;
        let flush = state.flushes;

        let mut counters = Vec::new();
        self.registry.visit_counters(|key, counter| {
            if counter.has_increase() {
                counters.push((key.clone(), counter.clone()));
            }
        });

        let mut changed_gauges = Vec::new();
        let mut unchanged_gauges = Vec::new();
        self.registry
            .visit_gauges(|key, gauge| match gauge.due(flush) {
                Some(GaugeDue::Changed) => changed_gauges.push((key.clone(), gauge.clone())),
                Some(GaugeDue::Refresh) => unchanged_gauges.push((key.clone(), gauge.clone())),
                None => {}
            });

        let mut histograms = Vec::new();
        self.registry.visit_histograms(|key, distribution| {
            let values = distribution.take_sample().values;
            if !values.is_empty() {
                histograms.push((key.clone(), values));
            }
        });

        let units = self.units.lock().unwrap_or_else(PoisonError::into_inner);
        let unit = |key: &Key| units.get(key.name()).copied();
        let mut over_budget = 0;
        let mut batch = Vec::new();
        let [next_counter, next_changed_gauge, next_unchanged_gauge] = &mut state.next;

        let (counters, left_out) = take_turns(counters, &mut room, next_counter);
        over_budget += left_out;
        for (key, counter) in counters {
            // One interval's increase is far below 2^53, so it converts to f64 exactly.
            let value = counter.take_increase() as f64;
            batch.push(metric(MetricType::Counter, &key, value, unit(&key)));
        }

        for (gauges, next) in [
            (changed_gauges, next_changed_gauge),
            (unchanged_gauges, next_unchanged_gauge),
        ] {
            let (gauges, left_out) = take_turns(gauges, &mut room, next);
            over_budget += left_out;
            for (key, gauge) in gauges {
                if let Some(value) = gauge.take_unsent(flush) {
                    batch.push(metric(MetricType::Gauge, &key, value, unit(&key)));
                }
            }
        }

        let recorded: usize = histograms.iter().map(|(_, values)| values.len()).sum();
        let kept = recorded.min(room);
        over_budget += recorded - kept;
        // Spread across every label set's values, so each label set keeps its share.
        let mut kept_indexes = (0..kept).map(|i| i * recorded / kept).peekable();
        let values = histograms
            .iter()
            .flat_map(|(key, values)| values.iter().map(move |value| (key, value)));
        for (index, (key, value)) in values.enumerate() {
            if kept_indexes.next_if_eq(&index).is_some() {
                batch.push(metric(MetricType::Distribution, key, *value, unit(key)));
            }
        }

        (batch, u64::try_from(over_budget).unwrap_or(u64::MAX))
    }

    /// Returns the value the registry keeps for `key` from `get`, or creates it with `create`
    /// unless `key`'s name already has [`LABEL_SET_CAP`] label sets.
    fn tracked<T>(
        &self,
        key: &Key,
        get: impl Fn(&Key) -> Option<T>,
        create: impl FnOnce(&Key) -> T,
    ) -> Option<T> {
        if let Some(value) = get(key) {
            return Some(value);
        }

        let mut label_sets = self.lock_label_sets();
        // Another thread may have created it while this one waited for the lock.
        if let Some(value) = get(key) {
            return Some(value);
        }

        label_sets.try_add(key.name()).then(|| create(key))
    }

    fn lock_label_sets(&self) -> MutexGuard<'_, LabelSets> {
        self.label_sets
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
    }

    fn describe(&self, key: KeyName, unit: Option<Unit>) {
        if let Some(unit) = unit {
            self.units
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .insert(key.as_str().to_owned(), unit);
        }
    }
}

/// Takes the `due` metrics that fit in `room`, starting at `*next` and wrapping around, and moves
/// `*next` past them, so that metrics left out go first in a later flush. Returns the metrics
/// taken, and how many were left out.
fn take_turns<T>(mut due: Vec<T>, room: &mut usize, next: &mut usize) -> (Vec<T>, usize) {
    let len = due.len();
    let taken = len.min(*room);
    *room -= taken;

    if taken < len {
        let start = *next % len;
        due.rotate_left(start);
        due.truncate(taken);
        *next = start + taken;
    }

    (due, len - taken)
}

/// Returns the metric to send for `value` under `key`, with the key's labels as attributes.
fn metric(metric_type: MetricType, key: &Key, value: f64, unit: Option<Unit>) -> SentryMetric {
    let attributes = key
        .labels()
        .map(|label| {
            (
                label.key().to_owned(),
                AttributeValue::String(label.value().to_owned()),
            )
        })
        .collect();

    SentryMetric {
        metric_type,
        name: key.name().to_owned(),
        value,
        unit,
        attributes,
    }
}

impl Recorder for SentryPolicy {
    fn describe_counter(&self, key: KeyName, unit: Option<Unit>, _: SharedString) {
        self.describe(key, unit);
    }

    fn describe_gauge(&self, key: KeyName, unit: Option<Unit>, _: SharedString) {
        self.describe(key, unit);
    }

    fn describe_histogram(&self, key: KeyName, unit: Option<Unit>, _: SharedString) {
        self.describe(key, unit);
    }

    fn register_counter(&self, key: &Key, _: &Metadata<'_>) -> Counter {
        let counter = self.tracked(
            key,
            |key| self.registry.get_counter(key),
            |key| self.registry.get_or_create_counter(key, Arc::clone),
        );
        match counter {
            Some(counter) => Counter::from_arc(counter),
            None => Counter::from_arc(self.untracked.clone()),
        }
    }

    fn register_gauge(&self, key: &Key, _: &Metadata<'_>) -> Gauge {
        let gauge = self.tracked(
            key,
            |key| self.registry.get_gauge(key),
            |key| self.registry.get_or_create_gauge(key, Arc::clone),
        );
        match gauge {
            Some(gauge) => Gauge::from_arc(gauge),
            None => Gauge::from_arc(self.untracked.clone()),
        }
    }

    fn register_histogram(&self, key: &Key, _: &Metadata<'_>) -> Histogram {
        if SENT_DIRECTLY.contains(&key.name()) {
            return Histogram::noop();
        }

        let distribution = self.tracked(
            key,
            |key| self.registry.get_histogram(key),
            |key| self.registry.get_or_create_histogram(key, Arc::clone),
        );
        match distribution {
            Some(distribution) => Histogram::from_arc(distribution),
            None => Histogram::from_arc(self.untracked.clone()),
        }
    }
}

/// Creates the values the [`SentryPolicy`] registry keeps for each label set.
struct PolicyStorage;

impl Storage<Key> for PolicyStorage {
    type Counter = Arc<CounterValue>;
    type Gauge = Arc<GaugeValue>;
    type Histogram = Arc<Distribution>;

    fn counter(&self, _: &Key) -> Self::Counter {
        Arc::default()
    }

    fn gauge(&self, _: &Key) -> Self::Gauge {
        Arc::default()
    }

    fn histogram(&self, _: &Key) -> Self::Histogram {
        Arc::default()
    }
}

/// How many label sets the [`SentryPolicy`] registry keeps for each metric name.
#[derive(Default)]
struct LabelSets {
    by_name: HashMap<String, NameLabelSets>,
    /// The names that reached [`LABEL_SET_CAP`] since the last flush, once each.
    newly_capped: Vec<String>,
}

#[derive(Default)]
struct NameLabelSets {
    kept: usize,
    capped: bool,
}

impl LabelSets {
    /// Adds a label set for the metric `name`, and returns whether the name had room for it.
    fn try_add(&mut self, name: &str) -> bool {
        let Some(label_sets) = self.by_name.get_mut(name) else {
            self.by_name.insert(
                name.to_owned(),
                NameLabelSets {
                    kept: 1,
                    capped: false,
                },
            );
            return true;
        };

        if label_sets.kept < LABEL_SET_CAP {
            label_sets.kept += 1;
            return true;
        }

        if !label_sets.capped {
            label_sets.capped = true;
            self.newly_capped.push(name.to_owned());
        }
        false
    }
}

/// The state that each flush updates.
#[derive(Default)]
struct FlushState {
    /// How many flushes have taken a batch.
    flushes: u64,
    /// Where counters, changed gauges, and unchanged gauges start taking turns when they don't
    /// all fit in a flush.
    next: [usize; 3],
    warning: DropWarning,
}

/// The values the policy didn't send, by why.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
struct Dropped {
    /// Values that were due, but didn't fit in a flush's [`FLUSH_BUDGET`].
    flush_budget: u64,
    /// Values that were due in a flush that sent nothing, because the transport was still sending
    /// what earlier flushes queued.
    transport_backlog: u64,
    /// Values recorded for label sets over [`LABEL_SET_CAP`].
    label_set_cap: u64,
}

impl Dropped {
    /// Counts these values in the installed recorder.
    fn record(&self) {
        for (reason, values) in [
            ("flush_budget", self.flush_budget),
            ("transport_backlog", self.transport_backlog),
            ("label_set_cap", self.label_set_cap),
        ] {
            if values > 0 {
                metrics::counter!("zebrad.sentry.metrics.dropped.total", "reason" => reason)
                    .increment(values);
            }
        }
    }
}

impl AddAssign for Dropped {
    fn add_assign(&mut self, other: Self) {
        self.flush_budget = self.flush_budget.saturating_add(other.flush_budget);
        self.transport_backlog = self
            .transport_backlog
            .saturating_add(other.transport_backlog);
        self.label_set_cap = self.label_set_cap.saturating_add(other.label_set_cap);
    }
}

/// Decides when the policy warns that it dropped values.
#[derive(Default)]
struct DropWarning {
    /// When the policy last warned.
    last: Option<Instant>,
    /// The values dropped since then.
    dropped: Dropped,
    /// The metrics that reached [`LABEL_SET_CAP`] since then.
    capped: Vec<String>,
}

impl DropWarning {
    /// Adds what a flush at `now` dropped, and returns everything dropped since the last warning
    /// if a warning is due: when drops start, then at most once per [`DROP_WARNING_INTERVAL`].
    fn add(
        &mut self,
        dropped: Dropped,
        capped: Vec<String>,
        now: Instant,
    ) -> Option<(Dropped, Vec<String>)> {
        self.dropped += dropped;
        self.capped.extend(capped);

        let nothing_dropped = self.dropped == Dropped::default() && self.capped.is_empty();
        let warned_recently = self
            .last
            .is_some_and(|last| now.saturating_duration_since(last) < DROP_WARNING_INTERVAL);
        if nothing_dropped || warned_recently {
            return None;
        }

        self.last = Some(now);
        Some((mem::take(&mut self.dropped), mem::take(&mut self.capped)))
    }
}

/// Stands in for every label set over [`LABEL_SET_CAP`], and counts the values recorded for them.
#[derive(Default)]
struct Untracked(AtomicU64);

impl Untracked {
    fn drop_value(&self) {
        self.0.fetch_add(1, Ordering::Relaxed);
    }

    /// Returns how many values were recorded since the previous call.
    fn take(&self) -> u64 {
        self.0.swap(0, Ordering::Relaxed)
    }
}

impl CounterFn for Untracked {
    fn increment(&self, _: u64) {
        self.drop_value();
    }

    fn absolute(&self, _: u64) {
        self.drop_value();
    }
}

impl GaugeFn for Untracked {
    fn increment(&self, _: f64) {
        self.drop_value();
    }

    fn decrement(&self, _: f64) {
        self.drop_value();
    }

    fn set(&self, _: f64) {
        self.drop_value();
    }
}

impl HistogramFn for Untracked {
    fn record(&self, _: f64) {
        self.drop_value();
    }
}

/// A counter's total, and its total when a flush last took its increase.
///
/// Keeping the previous total, instead of resetting the counter, makes counters set with
/// [`absolute`](CounterFn::absolute), such as `process_cpu_seconds_total`, send their increase.
#[derive(Default)]
struct CounterValue {
    total: AtomicU64,
    flushed: AtomicU64,
}

impl CounterValue {
    fn has_increase(&self) -> bool {
        self.total.load(Ordering::Relaxed) > self.flushed.load(Ordering::Relaxed)
    }

    /// Returns the increase since the previous call.
    fn take_increase(&self) -> u64 {
        let total = self.total.load(Ordering::Relaxed);
        let flushed = self.flushed.swap(total, Ordering::Relaxed);
        total.saturating_sub(flushed)
    }
}

impl CounterFn for CounterValue {
    fn increment(&self, value: u64) {
        self.total.fetch_add(value, Ordering::Relaxed);
    }

    fn absolute(&self, value: u64) {
        self.total.fetch_max(value, Ordering::Relaxed);
    }
}

/// A gauge's latest value, and the value and flush it was last sent at. Values are `f64` bits.
///
/// Only flushes, which hold [`SentryPolicy::flushing`], use `sent` and `sent_at`.
struct GaugeValue {
    /// The latest value, or NaN until the gauge is first set.
    value: AtomicU64,
    /// The value last sent, or NaN until the gauge is first sent.
    sent: AtomicU64,
    /// The flush that last sent the gauge.
    sent_at: AtomicU64,
}

/// Why a flush sends a gauge.
enum GaugeDue {
    Changed,
    Refresh,
}

impl Default for GaugeValue {
    fn default() -> Self {
        Self {
            value: AtomicU64::new(f64::NAN.to_bits()),
            sent: AtomicU64::new(f64::NAN.to_bits()),
            sent_at: AtomicU64::new(0),
        }
    }
}

impl GaugeValue {
    /// Returns the gauge's value, unless it was never set or isn't finite, which Sentry can't
    /// store.
    fn get(&self) -> Option<f64> {
        Some(f64::from_bits(self.value.load(Ordering::Relaxed))).filter(|value| value.is_finite())
    }

    /// Returns why the flush numbered `flush` sends this gauge, or `None` if it doesn't.
    fn due(&self, flush: u64) -> Option<GaugeDue> {
        let value = self.get()?;

        if value.to_bits() != self.sent.load(Ordering::Relaxed) {
            Some(GaugeDue::Changed)
        } else if flush.saturating_sub(self.sent_at.load(Ordering::Relaxed))
            >= GAUGE_REFRESH_FLUSHES
        {
            Some(GaugeDue::Refresh)
        } else {
            None
        }
    }

    /// Returns the value for the flush numbered `flush` to send, and remembers it as sent.
    fn take_unsent(&self, flush: u64) -> Option<f64> {
        let value = self.get()?;
        self.sent.store(value.to_bits(), Ordering::Relaxed);
        self.sent_at.store(flush, Ordering::Relaxed);
        Some(value)
    }

    fn update(&self, change: impl Fn(f64) -> f64) {
        let mut bits = self.value.load(Ordering::Relaxed);
        loop {
            // A gauge that was never set starts at zero, as it does in Prometheus.
            let current = Some(f64::from_bits(bits))
                .filter(|value| !value.is_nan())
                .unwrap_or_default();
            match self.value.compare_exchange_weak(
                bits,
                change(current).to_bits(),
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => return,
                Err(actual) => bits = actual,
            }
        }
    }
}

impl GaugeFn for GaugeValue {
    fn increment(&self, value: f64) {
        self.update(|current| current + value);
    }

    fn decrement(&self, value: f64) {
        self.update(|current| current - value);
    }

    fn set(&self, value: f64) {
        self.value.store(value.to_bits(), Ordering::Relaxed);
    }
}

/// A uniform sample of the values one histogram label set recorded since the previous flush.
///
/// Each sample keeps at most [`HISTOGRAM_CAP`] values, so a histogram's memory and flush work stay
/// the same however often it is recorded. Recording a value takes a short lock, so a flush takes
/// every value recorded before it, and none recorded after it.
#[derive(Default)]
struct Distribution {
    sample: Mutex<Sample>,
}

impl Distribution {
    /// Adds `value` to the sample, unless the value isn't finite, which Sentry can't store.
    fn add(&self, value: f64) {
        if !value.is_finite() {
            return;
        }

        self.sample
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .record(value);
    }

    /// Takes the sample of the interval, and starts a new interval.
    fn take_sample(&self) -> Sample {
        mem::take(&mut *self.sample.lock().unwrap_or_else(PoisonError::into_inner))
    }
}

impl HistogramFn for Distribution {
    fn record(&self, value: f64) {
        self.add(value);
    }
}

/// A uniform sample of at most [`HISTOGRAM_CAP`] values, kept with
/// [Algorithm R].
///
/// [Algorithm R]: https://en.wikipedia.org/wiki/Reservoir_sampling#Simple:_Algorithm_R
#[derive(Default)]
struct Sample {
    /// How many values were recorded in the interval.
    recorded: u64,
    values: Vec<f64>,
}

impl Sample {
    fn record(&mut self, value: f64) {
        let index = self.recorded;
        self.recorded = self.recorded.saturating_add(1);
        match sample_slot(index) {
            Some(slot) if slot < self.values.len() => self.values[slot] = value,
            Some(_) => self.values.push(value),
            None => {}
        }
    }
}

/// Returns the slot of a [`HISTOGRAM_CAP`]-value sample that the value numbered `index` in an
/// interval replaces, if it is sampled: each of the first values takes the next slot, then each
/// later value replaces a random slot with probability `HISTOGRAM_CAP / (index + 1)`.
fn sample_slot(index: u64) -> Option<usize> {
    if let Some(index) = usize::try_from(index)
        .ok()
        .filter(|index| *index < HISTOGRAM_CAP)
    {
        return Some(index);
    }

    usize::try_from(rand::random_range(0..=index))
        .ok()
        .filter(|slot| *slot < HISTOGRAM_CAP)
}

#[cfg(test)]
mod tests {
    use std::{sync::mpsc, thread};

    use metrics::with_local_recorder;

    use super::*;

    /// Returns the batch of one flush of `policy`.
    fn flush(policy: &SentryPolicy) -> Vec<SentryMetric> {
        let mut sent = Vec::new();
        policy.flush(|batch| sent = batch);
        sent
    }

    fn string(value: &str) -> AttributeValue {
        AttributeValue::String(value.to_owned())
    }

    #[test]
    fn counters_send_their_increase_once_per_flush() {
        let policy = SentryPolicy::default();

        with_local_recorder(&policy, || {
            metrics::describe_counter!("a.count", Unit::Count, "A count.");
            for _ in 0..3 {
                metrics::counter!("a.count", "kind" => "not_found").increment(1);
            }
        });

        assert_eq!(
            flush(&policy),
            [SentryMetric {
                metric_type: MetricType::Counter,
                name: "a.count".to_owned(),
                value: 3.0,
                unit: Some(Unit::Count),
                attributes: vec![("kind".to_owned(), string("not_found"))],
            }],
        );
        assert!(flush(&policy).is_empty(), "nothing new was counted");

        with_local_recorder(&policy, || {
            metrics::counter!("a.count", "kind" => "not_found").increment(2)
        });
        assert_eq!(
            flush(&policy)
                .iter()
                .map(|metric| metric.value)
                .collect::<Vec<_>>(),
            [2.0],
        );
    }

    #[test]
    fn absolute_counters_send_their_increase() {
        let policy = SentryPolicy::default();
        let cpu_seconds = |total| {
            with_local_recorder(&policy, || {
                metrics::counter!("process_cpu_seconds_total").absolute(total)
            })
        };
        let values =
            |batch: Vec<SentryMetric>| batch.iter().map(|metric| metric.value).collect::<Vec<_>>();

        cpu_seconds(100);
        assert_eq!(values(flush(&policy)), [100.0]);

        cpu_seconds(130);
        cpu_seconds(145);
        assert_eq!(values(flush(&policy)), [45.0]);

        cpu_seconds(145);
        assert!(flush(&policy).is_empty(), "the total didn't increase");
    }

    #[test]
    fn gauges_send_their_latest_value_once_set() {
        let policy = SentryPolicy::default();

        with_local_recorder(&policy, || {
            metrics::describe_gauge!("a.bytes", Unit::Bytes, "Some bytes.");
            metrics::gauge!("a.bytes").set(30.0);
            metrics::gauge!("a.bytes").set(90.0);
            // Registered, but never set.
            let _ = metrics::gauge!("a.unset");
            // Only incremented, so it counts from zero.
            metrics::gauge!("a.connections").increment(2.0);
            metrics::gauge!("a.connections").decrement(1.0);
        });

        let mut sent: Vec<_> = flush(&policy)
            .into_iter()
            .map(|metric| (metric.metric_type, metric.name, metric.value, metric.unit))
            .collect();
        sent.sort_by(|a, b| a.1.cmp(&b.1));

        let gauge = |name: &str, value, unit| (MetricType::Gauge, name.to_owned(), value, unit);
        assert_eq!(
            sent,
            [
                gauge("a.bytes", 90.0, Some(Unit::Bytes)),
                gauge("a.connections", 1.0, None),
            ],
        );
        assert!(
            flush(&policy).is_empty(),
            "unchanged gauges wait for their refresh",
        );
    }

    #[test]
    fn unchanged_gauges_are_sent_again_only_at_each_refresh() {
        let policy = SentryPolicy::default();
        let set = |value| with_local_recorder(&policy, || metrics::gauge!("a.peers").set(value));
        let values =
            |batch: Vec<SentryMetric>| batch.iter().map(|metric| metric.value).collect::<Vec<_>>();

        set(8.0);
        assert_eq!(values(flush(&policy)), [8.0], "a new gauge is sent");

        for _ in 1..GAUGE_REFRESH_FLUSHES {
            set(8.0);
            assert!(
                flush(&policy).is_empty(),
                "an unchanged gauge waits for its refresh",
            );
        }
        assert_eq!(
            values(flush(&policy)),
            [8.0],
            "an unchanged gauge is sent at its refresh",
        );

        set(9.0);
        assert_eq!(
            values(flush(&policy)),
            [9.0],
            "a changed gauge is sent at the next flush",
        );
        assert!(flush(&policy).is_empty());
    }

    fn count(values: usize) -> f64 {
        f64::from(u32::try_from(values).expect("test counts are small"))
    }

    /// Returns the values the batch reports as dropped for `reason`.
    fn dropped(batch: &[SentryMetric], reason: &str) -> f64 {
        batch
            .iter()
            .filter(|metric| {
                metric.name == "zebrad.sentry.metrics.dropped.total"
                    && metric.attributes == [("reason".to_owned(), string(reason))]
            })
            .map(|metric| metric.value)
            .sum()
    }

    /// Registers `label_sets` label sets with `register`, spread over the metrics `prefix0`,
    /// `prefix1`, and so on, at most [`LABEL_SET_CAP`] per name, and calls `record` with each.
    fn record_label_sets<T>(
        policy: &SentryPolicy,
        prefix: &str,
        label_sets: usize,
        register: impl Fn(String, String) -> T,
        record: impl Fn(T),
    ) {
        with_local_recorder(policy, || {
            for label_set in 0..label_sets {
                let name = format!("{prefix}{}", label_set / LABEL_SET_CAP);
                record(register(name, label_set.to_string()));
            }
        });
    }

    #[test]
    fn a_full_flush_sends_counters_then_changed_gauges_then_histogram_values() {
        let policy = SentryPolicy::default();
        let (counters, gauges, histograms) = (600, 600, 400);
        let records_per_histogram = 2 * HISTOGRAM_CAP;

        record_label_sets(
            &policy,
            "c",
            counters,
            |name, label| metrics::counter!(name, "i" => label),
            |counter| counter.increment(1),
        );
        record_label_sets(
            &policy,
            "g",
            gauges,
            |name, label| metrics::gauge!(name, "i" => label),
            |gauge| gauge.set(1.0),
        );
        record_label_sets(
            &policy,
            "h",
            histograms,
            |name, label| metrics::histogram!(name, "i" => label),
            |histogram| {
                for _ in 0..records_per_histogram {
                    histogram.record(1.0);
                }
            },
        );

        let sent = with_local_recorder(&policy, || flush(&policy));
        let types: Vec<MetricType> = sent.iter().map(|metric| metric.metric_type).collect();
        let histogram_room = FLUSH_BUDGET - counters - gauges;

        assert_eq!(sent.len(), FLUSH_BUDGET, "a full flush sends its budget");
        assert!(
            types[..counters].iter().all(|t| *t == MetricType::Counter)
                && types[counters..counters + gauges]
                    .iter()
                    .all(|t| *t == MetricType::Gauge)
                && types[counters + gauges..]
                    .iter()
                    .all(|t| *t == MetricType::Distribution),
            "counters, then gauges, then histogram values",
        );

        let mut sampled_label_sets: Vec<_> = sent[counters + gauges..]
            .iter()
            .map(|metric| (&metric.name, &metric.attributes))
            .collect();
        sampled_label_sets.dedup();
        assert_eq!(
            sampled_label_sets.len(),
            histogram_room.min(histograms),
            "histogram values are spread across label sets",
        );

        let next = flush(&policy);
        assert_eq!(
            dropped(&next, "flush_budget"),
            count(histograms * HISTOGRAM_CAP - histogram_room),
            "every histogram value left out is counted",
        );
        assert_eq!(dropped(&next, "label_set_cap"), 0.0);
    }

    #[test]
    fn counters_left_out_of_a_full_flush_take_turns_and_keep_their_increase() {
        let policy = SentryPolicy::default();
        let label_sets = FLUSH_BUDGET + FLUSH_BUDGET / 3;
        let increment = |policy: &SentryPolicy| {
            record_label_sets(
                policy,
                "c",
                label_sets,
                |name, label| metrics::counter!(name, "i" => label),
                |counter| counter.increment(1),
            )
        };

        let mut sent = Vec::new();
        increment(&policy);
        let first = flush(&policy);
        assert_eq!(first.len(), FLUSH_BUDGET);
        sent.extend(first);

        increment(&policy);
        for _ in 0..3 {
            sent.extend(flush(&policy));
        }

        let mut totals: HashMap<_, f64> = HashMap::new();
        for metric in &sent {
            *totals
                .entry((metric.name.clone(), format!("{:?}", metric.attributes)))
                .or_default() += metric.value;
        }
        assert_eq!(totals.len(), label_sets, "every label set gets its turn");
        assert!(
            totals.values().all(|total| *total == 2.0),
            "counters left out keep their increase for a later flush",
        );
    }

    #[test]
    fn a_flush_the_transport_isnt_ready_for_sends_nothing_and_counts_what_was_due() {
        let policy = SentryPolicy::default();
        let histogram_values = 3;

        with_local_recorder(&policy, || {
            metrics::counter!("a.count").increment(2);
            metrics::gauge!("a.peers").set(8.0);
            let histogram = metrics::histogram!("a.duration_seconds");
            for _ in 0..histogram_values {
                histogram.record(1.0);
            }
        });

        let mut sent = vec![];
        with_local_recorder(&policy, || {
            policy.flush_if(|| false, |batch| sent = batch);
        });
        assert!(
            sent.is_empty(),
            "nothing is sent while the transport is behind"
        );

        let next = flush(&policy);
        let value = |name: &str| {
            next.iter()
                .find(|metric| metric.name == name)
                .map(|metric| metric.value)
        };
        assert_eq!(value("a.count"), Some(2.0), "counters keep their increase");
        assert_eq!(value("a.peers"), Some(8.0), "changed gauges stay due");
        assert_eq!(
            value("a.duration_seconds"),
            None,
            "histogram values are dropped"
        );
        assert_eq!(
            dropped(&next, "transport_backlog"),
            count(1 + 1 + histogram_values),
            "every due value is counted",
        );
        assert_eq!(dropped(&next, "flush_budget"), 0.0);
    }

    #[test]
    fn label_sets_over_the_cap_are_neither_kept_nor_sent() {
        let policy = SentryPolicy::default();
        let over = 10;

        with_local_recorder(&policy, || {
            for label_set in 0..LABEL_SET_CAP + over {
                metrics::counter!("a.count", "i" => label_set.to_string()).increment(1);
            }
            let kept_handle = metrics::counter!("a.count", "i" => "kept_handle");
            for _ in 0..5 {
                kept_handle.increment(1);
            }
            for label_set in 0..=LABEL_SET_CAP {
                metrics::gauge!("a.gauge", "i" => label_set.to_string()).set(1.0);
                metrics::histogram!("a.delay_seconds", "i" => label_set.to_string()).record(1.0);
            }
            // A label set the policy keeps still records after the name reaches the cap.
            metrics::counter!("a.count", "i" => "0").increment(1);
            metrics::counter!("b.count", "i" => "0").increment(1);
        });

        let mut a_label_sets = 0;
        policy.registry.visit_counters(|key, _| {
            if key.name() == "a.count" {
                a_label_sets += 1;
            }
        });
        assert_eq!(a_label_sets, LABEL_SET_CAP);

        let sent = with_local_recorder(&policy, || flush(&policy));
        let total = |name: &str| -> (usize, f64) {
            let values: Vec<f64> = sent
                .iter()
                .filter(|metric| metric.name == name)
                .map(|metric| metric.value)
                .collect();
            (values.len(), values.iter().sum())
        };
        assert_eq!(total("a.count"), (LABEL_SET_CAP, count(LABEL_SET_CAP + 1)));
        assert_eq!(total("a.gauge").0, LABEL_SET_CAP);
        assert_eq!(total("a.delay_seconds").0, LABEL_SET_CAP);
        assert_eq!(total("b.count"), (1, 1.0), "the cap is for each name");

        let next = flush(&policy);
        assert_eq!(
            dropped(&next, "label_set_cap"),
            count(over + 5 + 1 + 1),
            "every value recorded for a label set over the cap is counted",
        );
    }

    #[test]
    fn drop_warnings_start_with_the_first_drop_and_repeat_at_most_hourly() {
        let mut warning = DropWarning::default();
        let start = Instant::now();
        let over_budget = |values| Dropped {
            flush_budget: values,
            ..Dropped::default()
        };
        let capped = || vec!["a.count".to_owned()];

        assert_eq!(warning.add(Dropped::default(), Vec::new(), start), None);
        assert_eq!(
            warning.add(over_budget(3), Vec::new(), start),
            Some((over_budget(3), Vec::new())),
            "drops that start are logged at once",
        );
        assert_eq!(
            warning.add(over_budget(4), capped(), start + FLUSH_INTERVAL),
            None,
        );

        let an_hour_later = start + DROP_WARNING_INTERVAL;
        assert_eq!(
            warning.add(over_budget(5), Vec::new(), an_hour_later),
            Some((over_budget(9), capped())),
            "the next warning has everything dropped since the last one",
        );

        let later = an_hour_later + 2 * DROP_WARNING_INTERVAL;
        assert_eq!(warning.add(Dropped::default(), Vec::new(), later), None);
        assert_eq!(
            warning.add(over_budget(1), Vec::new(), later),
            Some((over_budget(1), Vec::new())),
            "drops that start again are logged at once",
        );
    }

    #[test]
    fn histograms_send_at_most_the_cap_sampled_from_the_interval() {
        let policy = SentryPolicy::default();
        let cap = u32::try_from(HISTOGRAM_CAP).expect("the cap is small");
        let recorded = 10 * cap;

        with_local_recorder(&policy, || {
            metrics::describe_histogram!("a.duration_seconds", Unit::Seconds, "A duration.");
            let histogram = metrics::histogram!("a.duration_seconds", "result" => "success");
            for value in 0..recorded {
                histogram.record(f64::from(value));
            }
            metrics::histogram!("a.duration_seconds", "result" => "failure").record(f64::NAN);
            metrics::histogram!("b.duration_seconds").record(1.5);
        });

        let sent = flush(&policy);
        let values = |name: &str| -> Vec<f64> {
            sent.iter()
                .filter(|metric| metric.name == name)
                .map(|metric| metric.value)
                .collect()
        };

        let mut capped = values("a.duration_seconds");
        capped.sort_by(f64::total_cmp);
        capped.dedup();
        assert_eq!(capped.len(), HISTOGRAM_CAP, "distinct values up to the cap");
        assert!(
            capped.iter().all(|value| *value < f64::from(recorded)),
            "only recorded values are sent: {capped:?}",
        );
        assert!(sent
            .iter()
            .filter(|metric| metric.name == "a.duration_seconds")
            .all(|metric| metric.metric_type == MetricType::Distribution
                && metric.unit == Some(Unit::Seconds)
                && metric.attributes == [("result".to_owned(), string("success"))]));
        assert_eq!(values("b.duration_seconds"), [1.5]);
        assert!(flush(&policy).is_empty(), "histograms only send new values");
    }

    #[test]
    fn histogram_samples_stay_bounded_without_a_flush() {
        let policy = SentryPolicy::default();
        let recorded = 100_000_u32;

        with_local_recorder(&policy, || {
            let histogram = metrics::histogram!("a.duration_seconds");
            for value in 0..recorded {
                histogram.record(f64::from(value));
            }
        });

        let distribution = policy
            .registry
            .get_histogram(&Key::from_name("a.duration_seconds"))
            .expect("the label set was recorded");
        let sample = distribution
            .sample
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        assert_eq!(sample.recorded, u64::from(recorded));
        assert_eq!(
            sample.values.len(),
            HISTOGRAM_CAP,
            "the sample keeps only the cap"
        );
        drop(sample);

        assert_eq!(flush(&policy).len(), HISTOGRAM_CAP);
        assert!(flush(&policy).is_empty(), "a flush starts a new interval");
    }

    #[test]
    fn values_recorded_during_a_flush_stay_in_one_interval() {
        let distribution = Arc::new(Distribution::default());
        let recorded = 200_000_u32;

        let recorder = {
            let distribution = distribution.clone();
            thread::spawn(move || {
                for value in 0..recorded {
                    distribution.add(f64::from(value));
                }
            })
        };

        let mut counted = 0;
        let check = |sample: Sample| {
            let expected = usize::try_from(sample.recorded)
                .expect("an interval's count fits in usize")
                .min(HISTOGRAM_CAP);
            assert_eq!(
                sample.values.len(),
                expected,
                "a flush sends every value it counted, up to the cap",
            );
            sample.recorded
        };
        while !recorder.is_finished() {
            counted += check(distribution.take_sample());
        }
        recorder.join().expect("the recording thread doesn't panic");
        counted += check(distribution.take_sample());

        assert_eq!(counted, u64::from(recorded), "each value is counted once");
    }

    #[test]
    fn histogram_samples_are_uniform_over_the_interval() {
        let policy = SentryPolicy::default();
        let (intervals, recorded) = (200, 1_000_u32);

        let mut sampled = Vec::new();
        for _ in 0..intervals {
            with_local_recorder(&policy, || {
                let histogram = metrics::histogram!("a.duration_seconds");
                for value in 0..recorded {
                    histogram.record(f64::from(value));
                }
            });
            sampled.extend(flush(&policy).into_iter().map(|metric| metric.value));
        }

        // 4,000 values: each tenth of the interval expects 400, with a standard deviation under
        // 19, and the mean expects 499.5, with a standard deviation under 5. The bounds are more
        // than 6 standard deviations wide, so the test doesn't fail by chance.
        assert_eq!(sampled.len(), intervals * HISTOGRAM_CAP);
        let mut tenths = [0_usize; 10];
        for value in &sampled {
            // The values are whole numbers below `recorded`, so the tenth is below 10.
            tenths[(*value / f64::from(recorded / 10)) as usize] += 1;
        }
        assert!(
            tenths.iter().all(|values| (280..=520).contains(values)),
            "each tenth of the interval is sampled about as often: {tenths:?}",
        );
        let mean = sampled.iter().sum::<f64>() / count(sampled.len());
        assert!((470.0..=530.0).contains(&mean), "mean: {mean}");
    }

    #[test]
    fn flush_waits_for_a_flush_in_progress() {
        let policy = Arc::new(SentryPolicy::default());
        let (locked_tx, locked_rx) = mpsc::channel();
        let (release_tx, release_rx) = mpsc::channel();

        // Stands in for a periodic flush that has taken the increases but not sent them yet.
        let in_progress = thread::spawn({
            let policy = policy.clone();
            move || {
                let _flushing = policy
                    .flushing
                    .lock()
                    .unwrap_or_else(PoisonError::into_inner);
                locked_tx.send(()).expect("the test thread is waiting");
                release_rx
                    .recv()
                    .expect("the test thread releases the flush");
            }
        });
        locked_rx
            .recv()
            .expect("the other thread holds the flush lock");

        let (flushed_tx, flushed_rx) = mpsc::channel();
        let flushing = thread::spawn({
            let policy = policy.clone();
            move || {
                policy.flush(drop);
                flushed_tx.send(()).expect("the test thread is waiting");
            }
        });

        assert!(
            flushed_rx.recv_timeout(Duration::from_millis(100)).is_err(),
            "a flush finished while another one was in progress"
        );
        release_tx.send(()).expect("the other thread is waiting");
        flushed_rx
            .recv()
            .expect("the flush finishes once the other one does");

        in_progress.join().expect("the other thread doesn't panic");
        flushing.join().expect("the flushing thread doesn't panic");
    }

    #[test]
    fn flush_returns_when_reentered_from_a_flush_on_the_same_thread() {
        let policy = SentryPolicy::default();

        // A panic during a flush runs the panic hook's flush on the same thread.
        let _flushing = policy
            .flushing
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        IN_FLUSH.set(true);

        policy.flush(|_| panic!("a reentered flush doesn't send"));

        IN_FLUSH.set(false);
    }
}