sozu-lib 2.3.0

sozu library to build hot reconfigurable HTTP reverse proxies
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
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
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
//! Metrics façade.
//!
//! Defines the per-thread `Aggregator`, the `incr!`/`count!`/`gauge!`/
//! `gauge_add!`/`time!` macros consumed across the lib + bin crates, the
//! local-vs-network drain dispatch, and the label allow/deny filtering
//! used to keep cardinality bounded. Gauge underflow is treated as a
//! correctness bug (saturating clamp + warn log), not a rounding artefact.

mod local_drain;
pub mod names;
mod network_drain;
mod writer;

use std::{
    cell::RefCell,
    collections::{BTreeMap, HashMap},
    io::{self, Write},
    net::SocketAddr,
    num::NonZeroU64,
    str,
    time::{Duration, Instant},
};

use mio::net::UdpSocket;
use sozu_command::config::MetricDetailLevel;
use sozu_command::proto::command::{
    FilteredMetrics, MetricsConfiguration, QueryMetricsOptions, ResponseContent,
};

use crate::metrics::{local_drain::LocalDrain, network_drain::NetworkDrain};

/// Filter `(cluster_id, backend_id)` labels at emission time according to the
/// configured [`MetricDetailLevel`]. Each level is a SUPERSET of the previous
/// in cardinality terms:
///
/// - `Process`: drop both labels — proxy-only counters (smallest keyspace).
/// - `Frontend`: same as `Process` today; reserved for the per-listener
///   counters tracked as a follow-up. Listed in the proto + config so
///   operators can opt in once per-listener labels land.
/// - `Cluster`: keep `cluster_id`, drop `backend_id` — current default.
/// - `Backend`: keep both — current historical behaviour.
///
/// Pure function so the filter can be unit-tested exhaustively without the
/// drain machinery in scope.
fn filter_labels_for_detail<'a>(
    detail: MetricDetailLevel,
    cluster_id: Option<&'a str>,
    backend_id: Option<&'a str>,
) -> (Option<&'a str>, Option<&'a str>) {
    let (out_cluster, out_backend) = match detail {
        MetricDetailLevel::Process | MetricDetailLevel::Frontend => (None, None),
        MetricDetailLevel::Cluster => (cluster_id, None),
        MetricDetailLevel::Backend => (cluster_id, backend_id),
    };
    // The filter only ever DROPS labels: an absent input can never become a
    // present output, and a kept output must be the exact same borrow we
    // were handed (no synthesised label). This is the cardinality-safety
    // post-condition that keeps the local CLI view and the wire consistent.
    debug_assert!(
        cluster_id.is_some() || out_cluster.is_none(),
        "filter must never invent a cluster_id"
    );
    debug_assert!(
        backend_id.is_some() || out_backend.is_none(),
        "filter must never invent a backend_id"
    );
    debug_assert!(
        out_backend.is_none() || out_cluster.is_some(),
        "a kept backend label implies a kept cluster label (no orphan backend)"
    );
    (out_cluster, out_backend)
}

/// Map a numeric HTTP status code to its dedicated counter name, if any.
///
/// Returns `Some("http.status.<code>")` for the eighteen status codes Sōzu
/// either generates as a default answer or that operators routinely chart
/// (`200/201/204`, `301/302/304`, `400/401/403/404/408/413/429`, plus the
/// `5xx` family Sōzu can synthesise — `500/502/503/504/507`). All other
/// codes return `None` so the bucket counter (`http.status.{1xx,…,other}`)
/// remains the sole emission and metric cardinality stays bounded.
///
/// Hoisted out of the protocol modules so every emission site
/// (`mux::stream::generate_access_log`, shared by the H1 and H2 mux paths)
/// cannot drift on which codes get a per-code counter. It was shared with
/// `kawa_h1::save_http_status_metric` until that unreachable H1 session was
/// removed on 2026-09-20 (sozu#1346).
pub(crate) fn http_status_code_metric_name(status: u16) -> Option<&'static str> {
    match status {
        200 => Some("http.status.200"),
        201 => Some("http.status.201"),
        204 => Some("http.status.204"),
        301 => Some("http.status.301"),
        302 => Some("http.status.302"),
        304 => Some("http.status.304"),
        400 => Some("http.status.400"),
        401 => Some("http.status.401"),
        403 => Some("http.status.403"),
        404 => Some("http.status.404"),
        408 => Some("http.status.408"),
        413 => Some("http.status.413"),
        429 => Some("http.status.429"),
        500 => Some("http.status.500"),
        502 => Some("http.status.502"),
        503 => Some("http.status.503"),
        504 => Some("http.status.504"),
        507 => Some("http.status.507"),
        _ => None,
    }
}

thread_local! {
  pub static METRICS: RefCell<Aggregator> = RefCell::new(Aggregator::new(String::from("sozu")));
}

/// Opaque identity of one active lifetime of a cluster metrics row.
///
/// A lifetime starts either when a legal route first references a cluster or
/// when `AddCluster` arrives first. `AddCluster` without an intervening
/// `RemoveCluster` keeps the identity so gauges opened by the implicit route
/// remain balanced. A remove followed by another route or add allocates a new
/// identity, fencing late metrics from the previous lifetime.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct ClusterMetricsIncarnation(NonZeroU64);

/// Identity state captured by one delayed metric owner.
///
/// `Missing` is the default before routing. `Invalid` records the terminal
/// allocation-exhaustion path. Neither may be mistaken for a legal implicit
/// cluster lifetime when labels are retained.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum ClusterMetricsCapture {
    #[default]
    Missing,
    Active(ClusterMetricsIncarnation),
    Invalid,
}

/// Capture (and, for a legal first implicit reference, allocate) the active
/// metrics identity for `cluster_id`.
pub(crate) fn capture_cluster_metrics(cluster_id: &str) -> ClusterMetricsCapture {
    METRICS.with(|metrics| metrics.borrow_mut().capture_cluster_metrics(cluster_id))
}

#[derive(thiserror::Error, Debug)]
pub enum MetricError {
    #[error("Could not parse udp address {address}: {error}")]
    WrongUdpAddress { address: String, error: String },
    #[error("Could not bind to udp address {address}: {error}")]
    UdpBind { address: String, error: String },
    #[error("No metrics found for object with id {0}")]
    NoMetrics(String),
    #[error("Could not create histogram for time metric {time_metric:?}: {error}")]
    HistogramCreation {
        time_metric: MetricValue,
        error: String,
    },
    #[error("could not record time metric {time_metric:?}: {error}")]
    TimeMetricRecordingError {
        time_metric: MetricValue,
        error: String,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MetricValue {
    Gauge(usize),
    GaugeAdd(i64),
    Count(i64),
    Time(usize),
}

impl MetricValue {
    fn is_time(&self) -> bool {
        matches!(self, &MetricValue::Time(_))
    }

    fn update(&mut self, key: &'static str, m: MetricValue) -> bool {
        match (self, m) {
            (&mut MetricValue::Gauge(ref mut v1), MetricValue::Gauge(v2)) => {
                // `changed` reflects the absolute set: true iff the stored
                // value actually moves. Pair-assert the post-state matches
                // the requested value and that `changed` is the equality
                // relationship between before and after.
                let before = *v1;
                let changed = *v1 != v2;
                *v1 = v2;
                debug_assert_eq!(*v1, v2, "gauge set must store exactly the requested value");
                debug_assert_eq!(
                    changed,
                    before != v2,
                    "`changed` must report whether the gauge actually moved"
                );
                changed
            }
            (&mut MetricValue::Gauge(ref mut v1), MetricValue::GaugeAdd(v2)) => {
                // Saturating clamp + `error!` log on underflow, symmetric
                // with `AggregatedMetric::update` in `local_drain.rs`. The
                // `debug_assert!` that lived here previously made debug
                // builds panic while release silently clamped; H2
                // `ConnectionH2::Drop` rebalance + the saturating clamps
                // make underflow structurally impossible from live code,
                // and a panic on a metric mismatch is too aggressive — the
                // log line names the offending key for observability.
                //
                // INVARIANT (the canonical gauge-underflow guard): the post
                // value is non-negative, equals `before + v2` on the
                // non-underflow path, and is exactly the `0` floor whenever
                // `before + v2` would have gone negative. We assert the
                // *post-condition* (never panic the recovery path) rather
                // than a `before >= -v2` pre-condition, because a real
                // underflow is recovered (clamp + log) by design.
                let before = *v1;
                let changed = v2 != 0;
                let res = before as i64 + v2;
                *v1 = if res >= 0 {
                    res as usize
                } else {
                    error!(
                        "metric {} underflow: previous value: {}, adding: {}",
                        key, before, v2
                    );
                    0
                };
                debug_assert!(
                    res >= 0 || *v1 == 0,
                    "gauge underflow must clamp to exactly 0 (never wrap)"
                );
                debug_assert!(
                    res < 0 || *v1 as i64 == before as i64 + v2,
                    "non-underflow gauge add must equal before + v2 exactly"
                );

                changed
            }
            (&mut MetricValue::Count(ref mut v1), MetricValue::Count(v2)) => {
                // Counter delta: `v2` may be negative (e.g. `decr!`), so the
                // monotonic-between-resets invariant is encoded as the exact
                // delta — the post value equals the pre value plus `v2`.
                let before = *v1;
                let changed = v2 != 0;
                *v1 += v2;
                debug_assert_eq!(*v1, before + v2, "count update must advance by exactly v2");
                changed
            }
            (s, m) => {
                // Symmetric with `AggregatedMetric::update` in `local_drain.rs`:
                // a type-mismatch is a coding bug (a `metric_name` was
                // re-emitted with a different `MetricValue` variant), not a
                // network-driven event, but log-and-continue is still the
                // right policy — taking the worker process down on a metric
                // bookkeeping mismatch is too aggressive for a single-bug
                // recovery path. The log line names the offending key.
                error!(
                    "tried to update metric {} of value {:?} with an incompatible metric: {:?}",
                    key, s, m
                );
                false
            }
        }
    }
}

#[derive(Debug, Clone)]
pub struct StoredMetricValue {
    last_sent: Instant,
    updated: bool,
    data: MetricValue,
}

impl StoredMetricValue {
    pub fn new(last_sent: Instant, data: MetricValue) -> StoredMetricValue {
        // Symmetric with `AggregatedMetric::new`: a first emission with a
        // negative `GaugeAdd` is a coding bug (a `-1` ran with no paired
        // `+1` earlier in the worker's lifetime); clamp to 0 and log so the
        // offending pattern shows up in operator logs alongside the
        // local-drain side.
        let data = if let MetricValue::GaugeAdd(v) = data {
            if v >= 0 {
                MetricValue::Gauge(v as usize)
            } else {
                error!(
                    "stored metric created with negative GaugeAdd({}), clamping to 0",
                    v
                );
                MetricValue::Gauge(0)
            }
        } else {
            data
        };
        // Post-conditions: a `GaugeAdd` seed never survives as `GaugeAdd`
        // (it is normalised to a non-negative `Gauge`), and a fresh stored
        // value is always flagged `updated` so the next drain emits it.
        debug_assert!(
            !matches!(data, MetricValue::GaugeAdd(_)),
            "GaugeAdd must be normalised to a non-negative Gauge at construction"
        );
        StoredMetricValue {
            last_sent,
            updated: true,
            data,
        }
    }

    pub fn update(&mut self, key: &'static str, m: MetricValue) {
        // The `updated` flag is a monotone latch within one drain cycle: it
        // may only go from false→true here (the drain resets it to false on
        // emit). So once set, it stays set; and if this update reports a
        // change, the flag must be set afterwards.
        let was_updated = self.updated;
        let updated = self.data.update(key, m);
        if !self.updated {
            self.updated = updated;
        }
        debug_assert!(
            self.updated || (!was_updated && !updated),
            "`updated` must stay set once latched; a reported change must latch it"
        );
        debug_assert!(
            !was_updated || self.updated,
            "`updated` is a monotone latch within a drain cycle (never cleared here)"
        );
    }
}

pub fn setup<O: Into<String>>(
    metrics_host: &SocketAddr,
    origin: O,
    use_tagged_metrics: bool,
    prefix: Option<String>,
    detail: MetricDetailLevel,
) -> Result<(), MetricError> {
    let metrics_socket = udp_bind()?;

    debug!(
        "setting up metrics: local address = {:#?}",
        metrics_socket.local_addr()
    );

    METRICS.with(|metrics| {
        if let Some(p) = prefix {
            (*metrics.borrow_mut()).set_up_prefix(p);
        }
        (*metrics.borrow_mut()).set_up_remote(metrics_socket, *metrics_host);
        (*metrics.borrow_mut()).set_up_origin(origin.into());
        (*metrics.borrow_mut()).set_up_tagged_metrics(use_tagged_metrics);
        (*metrics.borrow_mut()).set_up_detail(detail);
    });
    Ok(())
}

pub trait Subscriber {
    fn receive_metric(
        &mut self,
        label: &'static str,
        cluster_id: Option<&str>,
        backend_id: Option<&str>,
        metric: MetricValue,
    );
}

/// How often `lease_tick` actually does work; cheaper than recomputing the
/// effective level on every metric emission. Polled once per worker event-loop
/// iteration (which wakes at least once per one-second poll timeout) and on
/// every worker command, so an expired lease is retired within this interval
/// plus one poll timeout, with or without traffic or commands.
const LEASE_TICK_INTERVAL: Duration = Duration::from_secs(5);

/// Hard cap on lease TTL, mirroring the proto comment on `SetMetricDetail`.
/// Bounds the worst-case effect of a stuck renewer.
pub const LEASE_TTL_MAX: Duration = Duration::from_secs(300);

/// Default lease TTL applied when the proto request omits `ttl_seconds` (or
/// passes `0`). Matches the proto comment.
pub const LEASE_TTL_DEFAULT: Duration = Duration::from_secs(60);

/// Hard cap on the number of simultaneous leases held by the aggregator.
/// `lease_apply` rejects new entries (with [`LeaseApplyOutcome::TableFull`])
/// once the table reaches this size. Bounds the lease table's memory and
/// neutralises the CWE-770 vector where a same-UID attacker rolls
/// `client_id` faster than expiry to grow the map unbounded. 64 is well
/// above any plausible TUI fleet (one TUI per operator + a handful of
/// metric scrapers); legitimate callers renewing the same `client_id`
/// REPLACE rather than insert and therefore don't bump the count.
pub const LEASE_TABLE_CAP: usize = 64;

/// Hard cap on `client_id` length accepted by `lease_apply`. The TUI
/// uses `top:<pid>:<8 hex chars>` ≤ 24 bytes; 64 leaves headroom for
/// other operator-side scrapers while keeping the lease table's per-
/// entry memory bounded and the audit-log lease_id column small.
pub const LEASE_CLIENT_ID_MAX_BYTES: usize = 64;

/// Outcome of [`Aggregator::lease_apply`]. The success arm returns the
/// `(previous_effective, new_effective)` pair the caller can use to
/// decide whether to emit a `MetricDetailChanged` audit event; the
/// failure arms surface bounded-input rejections.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LeaseApplyOutcome {
    /// Lease inserted / renewed.
    Applied {
        previous_effective: MetricDetailLevel,
        new_effective: MetricDetailLevel,
    },
    /// `client_id` length exceeds [`LEASE_CLIENT_ID_MAX_BYTES`].
    ClientIdTooLong,
    /// Lease table is at [`LEASE_TABLE_CAP`] and the request is a new
    /// insert (not a renewal). Callers MUST surface this as a `FAILURE`
    /// to the wire so the lessor can back off.
    TableFull,
    /// The requested TTL exceeds [`LEASE_TTL_MAX`]. This arm is
    /// theoretically unreachable in the normal flow because the
    /// dispatch site rejects out-of-range values before reaching the
    /// aggregator; surfacing it explicitly catches callers that bypass
    /// dispatch (proto fuzzing, future internal use) instead of
    /// silently clamping their intent.
    TtlOutOfRange,
    /// A renewal request was presented with a [`PeerBinding`] that
    /// disagrees with the existing lease's apply-time binding. Returned
    /// only when the existing binding is fully known (per
    /// [`PeerBinding::is_known`]); unknown-binding leases (no
    /// `SO_PEERCRED` available, pre-binding callers) continue to
    /// accept any renewer. Closes the same-UID `client_id`-collision
    /// takeover where an attacker re-applies a lease against a
    /// victim's id and replaces the binding to lock the victim out of
    /// their own clear.
    Unauthorized,
}

/// Master-populated peer binding stored alongside each lease so subsequent
/// `clear` requests can be authorised against the apply-time owner. The
/// binding pairs an OS pid (from `SO_PEERCRED` on Linux, captured by the
/// master at command-socket accept time) with the master-side connection
/// session ULID. A clear request must present BOTH values matching the
/// apply-time binding. A binding of `(None, None)` ("unknown") at apply
/// time means the connection had no peer credentials available — the
/// worker then accepts any clear for that `client_id` to preserve compat
/// with non-Linux callers and intermediate proxies. See the proto comment
/// on `SetMetricDetail.peer_pid` for the trust model.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct PeerBinding {
    pub pid: Option<i32>,
    /// Session ULID rendered as a `u128` (the master's session anchor).
    /// Stored as the raw u128 rather than the original `Ulid` to avoid
    /// dragging that crate into the metrics-aggregator dependencies.
    pub session_ulid: Option<u128>,
}

impl PeerBinding {
    /// `true` when both halves are known. A fully-known binding is the
    /// only one against which `clear` may be authorised; partial bindings
    /// (one half `None`) degrade to "accept any clear" per the proto
    /// contract on `SetMetricDetail.peer_pid` / `peer_session_ulid`.
    pub fn is_known(&self) -> bool {
        self.pid.is_some() && self.session_ulid.is_some()
    }

    /// True when `self` and `other` are compatible (same `pid` + same
    /// `session_ulid`) AND `self.is_known()`. Used by `lease_clear` to
    /// reject mismatched clears.
    pub fn matches(&self, other: &PeerBinding) -> bool {
        self.is_known() && self.pid == other.pid && self.session_ulid == other.session_ulid
    }
}

/// One lease entry kept inside [`Aggregator::leases`]. Carries the
/// requested cardinality level, the absolute expiry instant, and the
/// master-supplied [`PeerBinding`] captured at apply time.
#[derive(Clone, Copy, Debug)]
struct LeaseEntry {
    level: MetricDetailLevel,
    expires_at: Instant,
    binding: PeerBinding,
}

/// Outcome of [`Aggregator::lease_clear`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LeaseClearOutcome {
    /// The lease was found, the binding matched (or was unknown at apply
    /// time), and the entry has been removed. Carries the worker's
    /// previous effective level so the caller can decide whether to
    /// emit a `MetricDetailChanged` audit event.
    Cleared {
        previous_effective: MetricDetailLevel,
    },
    /// No lease was held by the requested `client_id`. Silent no-op.
    NotFound,
    /// A lease existed but the peer binding presented with the clear did
    /// not match the apply-time binding. The lease is left intact. The
    /// worker MUST surface this as a `FAILURE` response to discourage
    /// guessing attacks against another operator's lease.
    Unauthorized,
}

pub struct Aggregator {
    /// appended to metric keys, usually "sozu-"
    prefix: String,
    /// gathers metrics and sends them on a UDP socket
    network: Option<NetworkDrain>,
    /// gather metrics locally, queried by the CLI
    local: LocalDrain,
    /// Static cardinality knob — set once at boot from `MetricsConfig.detail`.
    /// Filters `(cluster_id, backend_id)` labels at emission time. Each level
    /// is a SUPERSET of the previous one (`Process ⊆ Frontend ⊆ Cluster ⊆ Backend`).
    configured: MetricDetailLevel,
    /// Effective cardinality knob actually applied at emission. Equal to
    /// `max(configured, max(active leases))`. Recomputed only when leases
    /// change, so the hot path (`receive_metric`) reads a single field.
    effective: MetricDetailLevel,
    /// Active TTL leases keyed by `client_id` from `SetMetricDetail`. A live
    /// lease holds the worker's effective level at-or-above its requested
    /// detail until it expires or is explicitly cleared. Multiple clients
    /// (e.g. several `sozu top` sessions) lease independently.
    leases: HashMap<String, LeaseEntry>,
    /// Wall-clock anchor for the polled lease janitor. Updated on every
    /// `lease_tick` call regardless of whether expiry happened, so the
    /// caller's "is it time to tick?" check stays cheap.
    last_lease_tick: Instant,
    /// Active lifetime for each cluster id. A legal implicit route allocates
    /// an entry before `AddCluster`; the add reuses it because only
    /// `RemoveCluster` is a lifetime boundary. This identity deliberately
    /// survives metrics collection being disabled or local storage being
    /// cleared.
    cluster_incarnations: HashMap<String, ClusterMetricsIncarnation>,
    /// Last allocated incarnation. Zero is reserved for `Option`'s niche and
    /// checked arithmetic makes exhaustion fail closed instead of reusing an
    /// old identity.
    last_cluster_incarnation: u64,
}

impl Aggregator {
    pub fn new(prefix: String) -> Aggregator {
        let default_detail = MetricDetailLevel::default();
        Aggregator {
            prefix: prefix.clone(),
            network: None,
            local: LocalDrain::new(prefix),
            configured: default_detail,
            effective: default_detail,
            leases: HashMap::new(),
            last_lease_tick: Instant::now(),
            cluster_incarnations: HashMap::new(),
            last_cluster_incarnation: 0,
        }
    }

    pub fn set_up_prefix(&mut self, prefix: String) {
        self.prefix = prefix;
    }

    pub fn set_up_remote(&mut self, socket: UdpSocket, addr: SocketAddr) {
        self.network = Some(NetworkDrain::new(self.prefix.clone(), socket, addr));
    }

    pub fn set_up_origin(&mut self, origin: String) {
        if let Some(n) = self.network.as_mut() {
            n.origin = origin;
        }
    }

    pub fn set_up_tagged_metrics(&mut self, tagged: bool) {
        if let Some(n) = self.network.as_mut() {
            n.use_tagged_metrics = tagged;
        }
    }

    /// Set the static cardinality floor (`MetricsConfig.detail` from the TOML
    /// configuration). Live leases applied via [`Self::lease_apply`] can
    /// elevate the effective level at runtime; the configured floor is the
    /// lower bound the worker falls back to when no lease is active.
    ///
    /// See [`MetricDetailLevel`] and `filter_labels_for_detail` for the
    /// per-level filtering rules.
    pub fn set_up_detail(&mut self, detail: MetricDetailLevel) {
        self.configured = detail;
        self.recompute_effective();
        // Raising the configured floor can only raise/hold the effective
        // level; the full invariant set must hold afterwards.
        debug_assert!(
            self.effective >= self.configured,
            "effective must dominate the freshly-set configured floor"
        );
        #[cfg(debug_assertions)]
        self.check_lease_invariants();
    }

    /// Returns the static (configured) cardinality floor. Independent of
    /// runtime leases.
    pub fn detail_configured(&self) -> MetricDetailLevel {
        self.configured
    }

    /// Returns the cardinality level actually applied to emissions. Equal to
    /// `max(configured, max(active leases))`.
    pub fn detail_effective(&self) -> MetricDetailLevel {
        self.effective
    }

    /// Apply or renew a runtime cardinality lease for `client_id`. If a lease
    /// for the same client already exists it is REPLACED (used for renewals).
    /// `ttl` values above [`LEASE_TTL_MAX`] are **rejected** with
    /// [`LeaseApplyOutcome::TtlOutOfRange`] (NOT clamped); callers must
    /// handle that arm or pre-validate the TTL. On success the call returns
    /// `(previous_effective, new_effective)` so callers can decide whether
    /// to emit a `MetricDetailChanged` audit event.
    ///
    /// The proto contract on `SetMetricDetail.ttl_seconds` is that the worker
    /// **rejects** out-of-range values with a `FAILURE` response — that
    /// enforcement lives at the dispatch site in `lib/src/server.rs::notify`.
    /// `lease_apply` itself returns [`LeaseApplyOutcome::TtlOutOfRange`]
    /// when called with `ttl > LEASE_TTL_MAX` so callers that bypass the
    /// dispatch site (proto fuzzing, future internal use) see a loud
    /// rejection instead of silently capped semantics. Same shape for
    /// over-long `client_id` and a full lease table — see
    /// [`LeaseApplyOutcome`] for the failure arms.
    ///
    /// `now` is parameterised exactly like [`Self::lease_tick`] so the whole
    /// lease lifecycle (apply, tick) is driven off one caller-supplied clock
    /// reading instead of `Instant::now()` on the datapath — the same
    /// sans-io shape as `UdpManager::handle_input` — letting a deterministic
    /// simulation drive `lease_apply` under a virtual clock. `SessionMetrics`
    /// and the timer wheel elsewhere in this module intentionally keep the
    /// real clock and are out of scope for this parameterisation.
    pub fn lease_apply(
        &mut self,
        client_id: String,
        level: MetricDetailLevel,
        ttl: Duration,
        binding: PeerBinding,
        now: Instant,
    ) -> LeaseApplyOutcome {
        if client_id.len() > LEASE_CLIENT_ID_MAX_BYTES {
            return LeaseApplyOutcome::ClientIdTooLong;
        }
        if ttl > LEASE_TTL_MAX {
            return LeaseApplyOutcome::TtlOutOfRange;
        }
        // Cap the table size BEFORE the insert, but only when the caller
        // is inserting a fresh entry. Renewals (same `client_id` already
        // present) REPLACE the existing entry and therefore keep the
        // count stable — they must always succeed so an active operator
        // never loses their lease just because the table is full.
        let is_renewal = self.leases.contains_key(&client_id);
        if !is_renewal && self.leases.len() >= LEASE_TABLE_CAP {
            return LeaseApplyOutcome::TableFull;
        }
        // Renewal-binding gate: when a lease already exists for this
        // `client_id` and its apply-time binding is fully known, the
        // renewer's presented binding MUST match. Without this check
        // any same-UID caller that learns the `client_id` (PID from
        // /proc, suffix from audit log) could re-apply against it,
        // overwriting the binding to point at the attacker's session
        // and then driving the victim's Drop-time `clear` into
        // `Unauthorized`. Unknown apply-time bindings continue to
        // accept any renewer per the proto contract on
        // `SetMetricDetail.peer_pid` / `peer_session_ulid`.
        if is_renewal
            && let Some(entry) = self.leases.get(&client_id)
            && entry.binding.is_known()
            && !entry.binding.matches(&binding)
        {
            return LeaseApplyOutcome::Unauthorized;
        }
        let expires_at = now + ttl;
        let before_len = self.leases.len();
        self.leases.insert(
            client_id,
            LeaseEntry {
                level,
                expires_at,
                binding,
            },
        );
        // Map-accounting invariant: a renewal REPLACES in place (count
        // stable) while a fresh insert grows the table by exactly one. The
        // table must never exceed the cap on the insert path because the
        // fresh-insert branch was gated by the `>= LEASE_TABLE_CAP` check
        // above.
        debug_assert_eq!(
            self.leases.len(),
            before_len + (!is_renewal) as usize,
            "lease_apply grows the table by exactly one iff this is a fresh insert"
        );
        debug_assert!(
            self.leases.len() <= LEASE_TABLE_CAP,
            "lease table must never exceed LEASE_TABLE_CAP after an accepted apply"
        );
        let previous_effective = self.effective;
        self.recompute_effective();
        // A lease is a mechanism for a client to temporarily ELEVATE
        // cardinality on its own behalf, not a one-way ratchet on the
        // shared `effective` level: `effective` is, and must stay,
        // `max(configured, max over live leases)`. A renewal at a LOWER
        // level than the client's own previous entry is that client
        // withdrawing part of its own request, so `effective` dropping in
        // response is the correct outcome -- not an invariant violation.
        // The only invariant that must hold here is the configured floor,
        // checked below.
        debug_assert!(
            self.effective >= self.configured,
            "effective must never drop below the configured floor"
        );
        #[cfg(debug_assertions)]
        self.check_lease_invariants();
        LeaseApplyOutcome::Applied {
            previous_effective,
            new_effective: self.effective,
        }
    }

    /// Explicitly release a lease keyed by `client_id`. The clear is
    /// authorised against the apply-time [`PeerBinding`] when one was
    /// recorded — see [`LeaseClearOutcome`] for the three result states.
    /// A clear request with `presented = PeerBinding::default()` matches
    /// only leases whose apply-time binding was also unknown, preserving
    /// compat with pre-binding callers and platforms without
    /// `SO_PEERCRED`.
    pub fn lease_clear(&mut self, client_id: &str, presented: PeerBinding) -> LeaseClearOutcome {
        let Some(entry) = self.leases.get(client_id) else {
            return LeaseClearOutcome::NotFound;
        };
        // If the apply-time binding is fully known we MUST authorise the
        // clear against it; presenting `default()` (an empty binding) is
        // a mismatch. If the apply-time binding is unknown (a pre-binding
        // caller, or a non-Linux peer with no credentials), we permit
        // any clear — there is nothing to authorise against.
        if entry.binding.is_known() && !entry.binding.matches(&presented) {
            return LeaseClearOutcome::Unauthorized;
        }
        // We only reach here after the `get` above proved the key is
        // present, so the removal evicts exactly one entry.
        let before_len = self.leases.len();
        self.leases.remove(client_id);
        debug_assert!(
            !self.leases.contains_key(client_id),
            "lease_clear must evict the cleared client_id"
        );
        debug_assert_eq!(
            self.leases.len(),
            before_len - 1,
            "lease_clear removes exactly one entry on the authorised path"
        );
        let previous = self.effective;
        self.recompute_effective();
        // Clearing a lease can only LOWER or hold the effective level (one
        // contributor to the max is gone), never raise it.
        debug_assert!(
            self.effective <= previous,
            "lease_clear must not raise the effective detail level"
        );
        debug_assert!(
            self.effective >= self.configured,
            "effective must never drop below the configured floor"
        );
        #[cfg(debug_assertions)]
        self.check_lease_invariants();
        LeaseClearOutcome::Cleared {
            previous_effective: previous,
        }
    }

    /// Returns the number of active (non-expired-as-of-last-tick) leases.
    /// Surfaced in `WorkerMetricDetailStatus` so the TUI can show
    /// "another client is still leasing this level".
    pub fn lease_count(&self) -> u32 {
        self.leases.len() as u32
    }

    /// Polled lease-expiry janitor. Called from the worker's event loop on
    /// every iteration and from `notify`, both gated by
    /// [`Self::lease_tick_due`]; cheap when nothing has expired. Returns
    /// `Some(previous_effective)` when at least one lease expired AND that
    /// expiry actually moved the effective level (so the caller can emit a
    /// `MetricDetailChanged` audit event), or `None` for the no-change path.
    ///
    /// `now` is parameterised so unit tests can advance the clock
    /// deterministically without sleeping.
    pub fn lease_tick(&mut self, now: Instant) -> Option<MetricDetailLevel> {
        self.last_lease_tick = now;
        let before = self.leases.len();
        self.leases.retain(|_, entry| entry.expires_at > now);
        // Expiry is a pure shrink: `retain` can only drop entries, never
        // add them, and every surviving lease must be strictly in the
        // future relative to `now`.
        debug_assert!(
            self.leases.len() <= before,
            "lease_tick (expiry) can only shrink the table, never grow it"
        );
        debug_assert!(
            self.leases.values().all(|entry| entry.expires_at > now),
            "every surviving lease must expire strictly after `now`"
        );
        if self.leases.len() == before {
            return None;
        }
        let previous = self.effective;
        self.recompute_effective();
        // Losing leases can only lower or hold the effective level.
        debug_assert!(
            self.effective <= previous,
            "lease expiry must not raise the effective detail level"
        );
        debug_assert!(
            self.effective >= self.configured,
            "effective must never drop below the configured floor"
        );
        #[cfg(debug_assertions)]
        self.check_lease_invariants();
        if previous != self.effective {
            Some(previous)
        } else {
            None
        }
    }

    /// True when at least `LEASE_TICK_INTERVAL` has passed since the last
    /// `lease_tick`. Gates the polled janitor so the worker's event loop
    /// does not pay a HashMap walk on every iteration.
    pub fn lease_tick_due(&self, now: Instant) -> bool {
        now.duration_since(self.last_lease_tick) >= LEASE_TICK_INTERVAL
    }

    /// Recompute `effective = max(configured, max(active leases))`. Cheap (one
    /// linear pass over the lease table) and only called on apply/clear/tick,
    /// never on the metric-emission hot path.
    fn recompute_effective(&mut self) {
        let mut max_lease = self.configured;
        for entry in self.leases.values() {
            if entry.level > max_lease {
                max_lease = entry.level;
            }
        }
        self.effective = max_lease;
        // Post-condition: effective is the floor AND every active lease's
        // ceiling — i.e. it is at least `configured` and at least each
        // lease level, and it equals one of those contributors (max).
        debug_assert!(
            self.effective >= self.configured,
            "effective is bounded below by the configured floor"
        );
        debug_assert!(
            self.leases.values().all(|e| self.effective >= e.level),
            "effective dominates every active lease level"
        );
        debug_assert!(
            self.effective == self.configured
                || self.leases.values().any(|e| e.level == self.effective),
            "effective must equal the configured floor or one active lease level"
        );
    }

    /// Debug-only full invariant sweep for the lease table, called as a
    /// run-to-completion post-condition from every lease-mutating method.
    /// Gathers the cross-field relationships that the individual delta
    /// assertions cannot express on their own.
    #[cfg(debug_assertions)]
    fn check_lease_invariants(&self) {
        debug_assert!(
            self.leases.len() <= LEASE_TABLE_CAP,
            "lease table size must stay within LEASE_TABLE_CAP"
        );
        debug_assert!(
            self.leases
                .keys()
                .all(|id| id.len() <= LEASE_CLIENT_ID_MAX_BYTES),
            "every stored client_id must respect LEASE_CLIENT_ID_MAX_BYTES"
        );
        debug_assert!(
            self.effective >= self.configured,
            "effective is at least the configured floor"
        );
        debug_assert!(
            self.leases.values().all(|e| self.effective >= e.level),
            "effective dominates every active lease level"
        );
        debug_assert!(
            self.effective == self.configured
                || self.leases.values().any(|e| e.level == self.effective),
            "effective equals the configured floor or some active lease level"
        );
    }

    pub fn socket(&self) -> Option<&UdpSocket> {
        self.network.as_ref().map(|n| &n.remote.get_ref().socket)
    }

    pub fn socket_mut(&mut self) -> Option<&mut UdpSocket> {
        self.network
            .as_mut()
            .map(|n| &mut n.remote.get_mut().socket)
    }

    pub fn count_add(&mut self, key: &'static str, count_value: i64) {
        self.receive_metric(key, None, None, MetricValue::Count(count_value));
    }

    pub fn set_gauge(&mut self, key: &'static str, gauge_value: usize) {
        self.receive_metric(key, None, None, MetricValue::Gauge(gauge_value));
    }

    pub fn gauge_add(&mut self, key: &'static str, gauge_value: i64) {
        self.receive_metric(key, None, None, MetricValue::GaugeAdd(gauge_value));
    }

    pub fn writable(&mut self) {
        if let Some(ref mut net) = self.network.as_mut() {
            net.writable();
        }
    }

    pub fn send_data(&mut self) {
        if let Some(ref mut net) = self.network.as_mut() {
            net.send_metrics();
        }
    }

    pub fn dump_local_proxy_metrics(&mut self) -> BTreeMap<String, FilteredMetrics> {
        self.local.dump_proxy_metrics(&Vec::new())
    }

    pub fn query(&mut self, q: &QueryMetricsOptions) -> Result<ResponseContent, MetricError> {
        self.local.query(q)
    }

    pub fn clear_local(&mut self) {
        if let Some(ref mut net) = self.network.as_mut() {
            net.clear();
        }
        self.local.clear();
    }

    pub fn configure(&mut self, config: &MetricsConfiguration) {
        self.local.configure(config);
    }

    /// Drop all metric storage for a cluster across BOTH drains and arm
    /// the per-drain tombstone so subsequent emissions for the cluster are
    /// dropped on the floor instead of resurrecting the row via
    /// `entry().or_default()`. Called from the worker IPC dispatch on
    /// [`RequestType::RemoveCluster`](sozu_command::proto::command::request::RequestType::RemoveCluster). Network-side draining the queued
    /// `MetricLine`s produces immediate silence on the wire (any unsent
    /// statsd interval for the cluster is discarded).
    ///
    /// Worker-event-loop only — `METRICS` is a `thread_local!`, and the
    /// mutable borrow taken here must not be re-entered from a session-
    /// bound code path. Calling this from a metrics emission site would
    /// deadlock the thread-local on `borrow_mut`.
    pub fn remove_cluster(&mut self, cluster_id: &str) {
        self.cluster_incarnations.remove(cluster_id);
        if let Some(ref mut net) = self.network.as_mut() {
            net.remove_cluster(cluster_id);
        }
        self.local.remove_cluster(cluster_id);
    }

    /// Re-arm a previously-removed cluster id across BOTH drains. Called
    /// from the worker IPC dispatch on [`RequestType::AddCluster`](sozu_command::proto::command::request::RequestType::AddCluster).
    /// Idempotent on ids that were never removed. Without this hook a
    /// cluster removed then re-added would stay tombstoned forever and
    /// every fresh metric emission would be dropped.
    pub fn add_cluster(&mut self, cluster_id: &str) {
        if !self.cluster_incarnations.contains_key(cluster_id) {
            let ClusterMetricsCapture::Active(incarnation) =
                self.allocate_cluster_metrics_incarnation(cluster_id)
            else {
                return;
            };
            self.cluster_incarnations
                .insert(cluster_id.to_owned(), incarnation);
        }
        if let Some(ref mut net) = self.network.as_mut() {
            net.add_cluster(cluster_id);
        }
        self.local.add_cluster(cluster_id);
    }

    fn allocate_cluster_metrics_incarnation(&mut self, cluster_id: &str) -> ClusterMetricsCapture {
        let Some(next) = self.last_cluster_incarnation.checked_add(1) else {
            error!(
                "cluster metrics incarnation space exhausted; refusing metrics identity for cluster {:?}",
                cluster_id
            );
            return ClusterMetricsCapture::Invalid;
        };
        let Some(incarnation) = NonZeroU64::new(next).map(ClusterMetricsIncarnation) else {
            unreachable!("checked increment from a live u64 incarnation cannot yield zero");
        };
        self.last_cluster_incarnation = next;
        ClusterMetricsCapture::Active(incarnation)
    }

    /// Capture the active lifetime of `cluster_id`, allocating it when a
    /// legal frontend/backend route references the id before `AddCluster`.
    fn capture_cluster_metrics(&mut self, cluster_id: &str) -> ClusterMetricsCapture {
        if let Some(incarnation) = self.cluster_incarnations.get(cluster_id).copied() {
            return ClusterMetricsCapture::Active(incarnation);
        }
        let capture = self.allocate_cluster_metrics_incarnation(cluster_id);
        if let ClusterMetricsCapture::Active(incarnation) = capture {
            self.cluster_incarnations
                .insert(cluster_id.to_owned(), incarnation);
            // This implicit route starts the replacement lifetime just as an
            // AddCluster would. Re-arm both drains so the new identity can
            // create its row while the identity gate still rejects owners
            // captured before RemoveCluster.
            if let Some(ref mut net) = self.network.as_mut() {
                net.add_cluster(cluster_id);
            }
            self.local.add_cluster(cluster_id);
        }
        capture
    }

    /// Return the active lifetime of `cluster_id`.
    pub(crate) fn cluster_incarnation(
        &self,
        cluster_id: &str,
    ) -> Option<ClusterMetricsIncarnation> {
        self.cluster_incarnations.get(cluster_id).copied()
    }

    /// Whether an emission for `cluster_id` captured with `capture`
    /// may reach the drains under the current effective detail.
    ///
    /// Process and Frontend detail discard cluster/backend labels, so every
    /// capture state contributes to that aggregate: a late decrement must
    /// balance the earlier increment. Cluster and Backend detail retain the
    /// label and therefore require a real captured identity equal to the
    /// current active identity. Missing and exhausted captures fail closed.
    pub(crate) fn accepts_cluster_capture(
        &self,
        cluster_id: &str,
        capture: ClusterMetricsCapture,
    ) -> bool {
        if !matches!(
            self.effective,
            MetricDetailLevel::Cluster | MetricDetailLevel::Backend
        ) {
            return true;
        }
        matches!(
            capture,
            ClusterMetricsCapture::Active(incarnation)
                if self.cluster_incarnation(cluster_id) == Some(incarnation)
        )
    }

    /// Receive a metric from an owner that captured a cluster incarnation.
    ///
    /// The identity check intentionally precedes cardinality filtering. A
    /// stale event is rejected when the effective detail retains its cluster
    /// label, protecting a same-id replacement row. At `Process` and
    /// `Frontend` detail the label is deliberately removed to form a worker
    /// aggregate, so the event remains part of that aggregate: while detail
    /// stays at that level, an old connection's late `-1` still balances its
    /// earlier `+1`. Existing detail transitions do not migrate stored gauge
    /// contributions between shapes. See
    /// [`Self::accepts_cluster_capture`] for the identity comparison.
    pub(crate) fn receive_metric_for_incarnation(
        &mut self,
        label: &'static str,
        cluster_id: Option<&str>,
        backend_id: Option<&str>,
        capture: ClusterMetricsCapture,
        metric: MetricValue,
    ) {
        if cluster_id.is_none_or(|cluster_id| self.accepts_cluster_capture(cluster_id, capture)) {
            self.receive_metric(label, cluster_id, backend_id, metric);
        }
    }

    /// Drop all metric storage for one backend across BOTH drains. Called
    /// from the worker IPC dispatch on [`RequestType::RemoveBackend`](sozu_command::proto::command::request::RequestType::RemoveBackend).
    /// Does NOT tombstone the cluster (only `remove_cluster` does).
    pub fn remove_backend(&mut self, cluster_id: &str, backend_id: &str) {
        if let Some(ref mut net) = self.network.as_mut() {
            net.remove_backend(cluster_id, backend_id);
        }
        self.local.remove_backend(cluster_id, backend_id);
    }
}

impl Subscriber for Aggregator {
    fn receive_metric(
        &mut self,
        label: &'static str,
        cluster_id: Option<&str>,
        backend_id: Option<&str>,
        metric: MetricValue,
    ) {
        // Apply the cardinality knob BEFORE handing the metric to either
        // drain. Both drains see the same filtered labels, keeping the local
        // CLI view consistent with what statsd receives on the wire. Reads
        // `effective` (max of the configured floor and any active leases),
        // which is recomputed off the hot path.
        let (cluster_id, backend_id) =
            filter_labels_for_detail(self.effective, cluster_id, backend_id);
        if let Some(ref mut net) = self.network.as_mut() {
            net.receive_metric(label, cluster_id, backend_id, metric.to_owned());
        }
        self.local
            .receive_metric(label, cluster_id, backend_id, metric);
    }
}

pub struct MetricSocket {
    pub addr: SocketAddr,
    pub socket: UdpSocket,
}

impl Write for MetricSocket {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.socket.send_to(buf, self.addr)
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

pub fn udp_bind() -> Result<UdpSocket, MetricError> {
    let address = "0.0.0.0:0";

    let udp_address =
        address
            .parse::<SocketAddr>()
            .map_err(|parse_error| MetricError::WrongUdpAddress {
                address: address.to_owned(),
                error: parse_error.to_string(),
            })?;

    UdpSocket::bind(udp_address).map_err(|parse_error| MetricError::UdpBind {
        address: udp_address.to_string(),
        error: parse_error.to_string(),
    })
}

/// adds a value to a counter
#[macro_export]
macro_rules! count (
  ($key:expr, $value: expr) => ({
    let v = $value;
    $crate::metrics::METRICS.with(|metrics| {
      (*metrics.borrow_mut()).count_add($key, v);
    });
  })
);

/// adds 1 to a counter
#[macro_export]
macro_rules! incr (
  ($key:expr) => (count!($key, 1));
  ($key:expr, $cluster_id:expr, $backend_id:expr) => {
    {
        use $crate::metrics::Subscriber;

        $crate::metrics::METRICS.with(|metrics| {
          (*metrics.borrow_mut()).receive_metric($key, $cluster_id, $backend_id, $crate::metrics::MetricValue::Count(1));
        });
    }
  };
  ($key:expr, $cluster_id:expr, $backend_id:expr, $incarnation:expr) => {
    {
        $crate::metrics::METRICS.with(|metrics| {
          (*metrics.borrow_mut()).receive_metric_for_incarnation(
            $key,
            $cluster_id,
            $backend_id,
            $incarnation,
            $crate::metrics::MetricValue::Count(1),
          );
        });
    }
  }
);

#[macro_export]
macro_rules! decr (
  ($key:expr) => (count!($key, -1))
);

#[macro_export]
macro_rules! gauge (
  ($key:expr, $value: expr) => ({
    let v = $value;
    $crate::metrics::METRICS.with(|metrics| {
      (*metrics.borrow_mut()).set_gauge($key, v);
    });
  });
  ($key:expr, $value:expr, $cluster_id:expr, $backend_id:expr) => {
    {
        use $crate::metrics::Subscriber;
        let v = $value;

        $crate::metrics::METRICS.with(|metrics| {
          (*metrics.borrow_mut()).receive_metric($key, $cluster_id, $backend_id, $crate::metrics::MetricValue::Gauge(v as usize));
        });
    }
  };
  ($key:expr, $value:expr, $cluster_id:expr, $backend_id:expr, $incarnation:expr) => {
    {
        let v = $value;
        $crate::metrics::METRICS.with(|metrics| {
          (*metrics.borrow_mut()).receive_metric_for_incarnation(
            $key,
            $cluster_id,
            $backend_id,
            $incarnation,
            $crate::metrics::MetricValue::Gauge(v as usize),
          );
        });
    }
  }
);

#[macro_export]
macro_rules! gauge_add (
  ($key:expr, $value: expr) => ({
    let v = $value;
    $crate::metrics::METRICS.with(|metrics| {
      (*metrics.borrow_mut()).gauge_add($key, v);
    });
  });
  ($key:expr, $value:expr, $cluster_id:expr, $backend_id:expr) => {
    {
        use $crate::metrics::Subscriber;
        let v = $value;

        $crate::metrics::METRICS.with(|metrics| {
          (*metrics.borrow_mut()).receive_metric($key, $cluster_id, $backend_id, $crate::metrics::MetricValue::GaugeAdd(v));
        });
    }
  };
  ($key:expr, $value:expr, $cluster_id:expr, $backend_id:expr, $incarnation:expr) => {
    {
        let v = $value;
        $crate::metrics::METRICS.with(|metrics| {
          (*metrics.borrow_mut()).receive_metric_for_incarnation(
            $key,
            $cluster_id,
            $backend_id,
            $incarnation,
            $crate::metrics::MetricValue::GaugeAdd(v),
          );
        });
    }
  }
);

#[macro_export]
macro_rules! time (
  ($key:expr, $value: expr) => ({
    use $crate::metrics::{MetricValue,Subscriber};
    let v = $value;
    $crate::metrics::METRICS.with(|metrics| {
      let m = &mut *metrics.borrow_mut();

      m.receive_metric($key, None, None, MetricValue::Time(v as usize));
    });
  });
  ($key:expr, $cluster_id:expr, $value: expr) => ({
    use $crate::metrics::{MetricValue,Subscriber};
    let v = $value;
    $crate::metrics::METRICS.with(|metrics| {
      let m = &mut *metrics.borrow_mut();
      let cluster: &str = $cluster_id;

      m.receive_metric($key, Some(cluster), None, MetricValue::Time(v as usize));
    });
  });
  ($key:expr, $cluster_id:expr, $value:expr, $incarnation:expr) => ({
    use $crate::metrics::MetricValue;
    let v = $value;
    $crate::metrics::METRICS.with(|metrics| {
      let m = &mut *metrics.borrow_mut();
      let cluster: &str = $cluster_id;

      m.receive_metric_for_incarnation(
        $key,
        Some(cluster),
        None,
        $incarnation,
        MetricValue::Time(v as usize),
      );
    });
  })
);

#[macro_export]
macro_rules! record_backend_metrics (
  ($cluster_id:expr, $backend_id:expr, $response_time: expr, $backend_connection_time: expr, $backend_header_time: expr, $bin: expr, $bout: expr) => {
    use $crate::metrics::{MetricValue,Subscriber};
    $crate::metrics::METRICS.with(|metrics| {
      let m = &mut *metrics.borrow_mut();
      let cluster_id: &str = $cluster_id;
      let backend_id: &str = $backend_id;

      m.receive_metric($crate::metrics::names::backend::BYTES_IN, Some(cluster_id), Some(backend_id), MetricValue::Count($bin as i64));
      m.receive_metric($crate::metrics::names::backend::BYTES_OUT, Some(cluster_id), Some(backend_id), MetricValue::Count($bout as i64));
      m.receive_metric($crate::metrics::names::backend::RESPONSE_TIME, Some(cluster_id), Some(backend_id), MetricValue::Time($response_time as usize));
      if let Some(t) = $backend_connection_time {
        m.receive_metric($crate::metrics::names::backend::CONNECTION_TIME, Some(cluster_id), Some(backend_id), MetricValue::Time(t.as_millis() as usize));
      }
      // `Option`, like the connection time and unlike the response time: a
      // protocol with no response headers at all (raw TCP) must record
      // nothing here rather than a zero.
      if let Some(t) = $backend_header_time {
        m.receive_metric($crate::metrics::names::backend::HEADER_TIME, Some(cluster_id), Some(backend_id), MetricValue::Time(t.as_millis() as usize));
      }

      m.receive_metric($crate::metrics::names::backend::REQUESTS, Some(cluster_id), Some(backend_id), MetricValue::Count(1));
    });
  };
  ($cluster_id:expr, $backend_id:expr, $response_time:expr, $backend_connection_time:expr, $backend_header_time:expr, $bin:expr, $bout:expr, $incarnation:expr) => {
    use $crate::metrics::{MetricValue,Subscriber};
    $crate::metrics::METRICS.with(|metrics| {
      let m = &mut *metrics.borrow_mut();
      let cluster_id: &str = $cluster_id;
      let backend_id: &str = $backend_id;

      // One identity lookup gates the whole burst: every emission below
      // shares the same cluster and captured incarnation.
      if m.accepts_cluster_capture(cluster_id, $incarnation) {
        m.receive_metric($crate::metrics::names::backend::BYTES_IN, Some(cluster_id), Some(backend_id), MetricValue::Count($bin as i64));
        m.receive_metric($crate::metrics::names::backend::BYTES_OUT, Some(cluster_id), Some(backend_id), MetricValue::Count($bout as i64));
        m.receive_metric($crate::metrics::names::backend::RESPONSE_TIME, Some(cluster_id), Some(backend_id), MetricValue::Time($response_time as usize));
        if let Some(t) = $backend_connection_time {
          m.receive_metric($crate::metrics::names::backend::CONNECTION_TIME, Some(cluster_id), Some(backend_id), MetricValue::Time(t.as_millis() as usize));
        }
        if let Some(t) = $backend_header_time {
          m.receive_metric($crate::metrics::names::backend::HEADER_TIME, Some(cluster_id), Some(backend_id), MetricValue::Time(t.as_millis() as usize));
        }

        m.receive_metric($crate::metrics::names::backend::REQUESTS, Some(cluster_id), Some(backend_id), MetricValue::Count(1));
      }
    });
  }
);

#[cfg(test)]
mod tests {
    use super::*;
    use sozu_command::proto::command::filtered_metrics;

    #[test]
    fn filter_labels_process_drops_both() {
        assert_eq!(
            filter_labels_for_detail(MetricDetailLevel::Process, Some("c"), Some("b")),
            (None, None),
        );
    }

    #[test]
    fn filter_labels_frontend_drops_both_today() {
        // Reserved for per-listener counters; same as Process for now.
        assert_eq!(
            filter_labels_for_detail(MetricDetailLevel::Frontend, Some("c"), Some("b")),
            (None, None),
        );
    }

    #[test]
    fn filter_labels_cluster_keeps_cluster_drops_backend() {
        assert_eq!(
            filter_labels_for_detail(MetricDetailLevel::Cluster, Some("c"), Some("b")),
            (Some("c"), None),
        );
    }

    #[test]
    fn cluster_incarnation_fences_labelled_rows_and_preserves_process_aggregates() {
        let mut aggregator = Aggregator::new(String::new());
        aggregator.set_up_detail(MetricDetailLevel::Backend);
        aggregator.add_cluster("reused");
        let old_token = aggregator
            .cluster_incarnation("reused")
            .expect("configured cluster has an identity");
        let old = ClusterMetricsCapture::Active(old_token);

        // Neither a live AddCluster update nor local collection controls end
        // the configured cluster lifetime.
        aggregator.add_cluster("reused");
        assert_eq!(aggregator.cluster_incarnation("reused"), Some(old_token));
        aggregator.configure(&MetricsConfiguration::Disabled);
        aggregator.configure(&MetricsConfiguration::Enabled);
        aggregator.configure(&MetricsConfiguration::Clear);
        assert_eq!(aggregator.cluster_incarnation("reused"), Some(old_token));

        aggregator.remove_cluster("reused");
        aggregator.add_cluster("reused");
        let replacement_token = aggregator
            .cluster_incarnation("reused")
            .expect("replacement cluster has an identity");
        let replacement = ClusterMetricsCapture::Active(replacement_token);
        assert_ne!(replacement_token, old_token);

        // At Backend detail an obsolete event is rejected instead of being
        // applied to the replacement row.
        aggregator.receive_metric_for_incarnation(
            "incarnation_test",
            Some("reused"),
            Some("same-backend"),
            old,
            MetricValue::Count(7),
        );
        assert!(
            !aggregator
                .local
                .dump_cluster_metrics(&[])
                .expect("dump cluster metrics after stale emission")
                .contains_key("reused"),
            "a labelled stale event must not recreate the replacement row",
        );

        // The same gate must still accept an event from the active lifetime.
        aggregator.receive_metric_for_incarnation(
            "incarnation_test",
            Some("reused"),
            Some("same-backend"),
            replacement,
            MetricValue::Count(3),
        );
        let labelled = aggregator
            .local
            .dump_cluster_metrics(&[])
            .expect("dump active cluster metrics");
        let active_backend = labelled["reused"]
            .backends
            .iter()
            .find(|backend| backend.backend_id == "same-backend")
            .expect("active backend metric row");
        assert_eq!(
            active_backend
                .metrics
                .get("incarnation_test")
                .and_then(|metric| metric.inner.as_ref()),
            Some(&filtered_metrics::Inner::Count(3)),
        );

        // Process detail deliberately aggregates cluster/backend events by
        // removing their labels. Both old and current work contribute while
        // the effective detail remains at that level.
        aggregator.set_up_detail(MetricDetailLevel::Process);
        aggregator.receive_metric_for_incarnation(
            "incarnation_test",
            Some("reused"),
            Some("same-backend"),
            old,
            MetricValue::Count(7),
        );
        aggregator.receive_metric_for_incarnation(
            "incarnation_test",
            Some("reused"),
            Some("same-backend"),
            replacement,
            MetricValue::Count(3),
        );
        assert_eq!(
            aggregator
                .dump_local_proxy_metrics()
                .get("incarnation_test")
                .and_then(|metric| metric.inner.as_ref()),
            Some(&filtered_metrics::Inner::Count(10)),
        );

        // A true proxy metric has no cluster identity and is independent of
        // the incarnation gate at every detail level.
        aggregator.receive_metric("direct_proxy_test", None, None, MetricValue::Count(5));
        assert_eq!(
            aggregator
                .dump_local_proxy_metrics()
                .get("direct_proxy_test")
                .and_then(|metric| metric.inner.as_ref()),
            Some(&filtered_metrics::Inner::Count(5)),
        );

        // Exhaustion never wraps to a previously issued identity and never
        // re-arms the drain tombstone.
        let mut exhausted = Aggregator::new(String::new());
        exhausted.last_cluster_incarnation = u64::MAX;
        exhausted.add_cluster("exhausted");
        assert_eq!(exhausted.cluster_incarnation("exhausted"), None);
    }

    /// Value of `key` in the local drain for `cluster_id`: the backend row
    /// when `backend_id` is given, the cluster row otherwise.
    fn local_labelled_value(
        aggregator: &mut Aggregator,
        cluster_id: &str,
        backend_id: Option<&str>,
        key: &str,
    ) -> Option<filtered_metrics::Inner> {
        let dump = aggregator
            .local
            .dump_cluster_metrics(&[])
            .expect("dump cluster metrics");
        let row = dump.get(cluster_id)?;
        let metrics = match backend_id {
            Some(backend_id) => {
                &row.backends
                    .iter()
                    .find(|backend| backend.backend_id == backend_id)?
                    .metrics
            }
            None => &row.cluster,
        };
        metrics.get(key)?.inner.clone()
    }

    #[test]
    fn add_without_remove_keeps_implicit_identity_and_balances_labelled_gauge() {
        let mut aggregator = Aggregator::new(String::new());
        aggregator.set_up_detail(MetricDetailLevel::Backend);
        let capture = aggregator.capture_cluster_metrics("implicit-then-added");
        let ClusterMetricsCapture::Active(token) = capture else {
            panic!("implicit route must allocate an active identity");
        };
        aggregator.receive_metric_for_incarnation(
            "implicit_balance",
            Some("implicit-then-added"),
            Some("backend"),
            capture,
            MetricValue::GaugeAdd(1),
        );

        aggregator.add_cluster("implicit-then-added");
        assert_eq!(
            aggregator.cluster_incarnation("implicit-then-added"),
            Some(token),
        );
        aggregator.receive_metric_for_incarnation(
            "implicit_balance",
            Some("implicit-then-added"),
            Some("backend"),
            capture,
            MetricValue::GaugeAdd(-1),
        );

        assert_eq!(
            local_labelled_value(
                &mut aggregator,
                "implicit-then-added",
                Some("backend"),
                "implicit_balance",
            ),
            Some(filtered_metrics::Inner::Gauge(0)),
        );
    }

    #[test]
    fn implicit_cluster_capture_is_recorded_at_process_detail() {
        // A frontend may route to a cluster that never received AddCluster;
        // its owners capture a valid implicit identity. The label-stripped
        // worker aggregate must still count those events.
        let mut aggregator = Aggregator::new(String::new());
        aggregator.set_up_detail(MetricDetailLevel::Process);
        let capture = aggregator.capture_cluster_metrics("undeclared");
        aggregator.receive_metric_for_incarnation(
            "undeclared_test",
            Some("undeclared"),
            Some("backend"),
            capture,
            MetricValue::Count(4),
        );
        assert_eq!(
            aggregator
                .dump_local_proxy_metrics()
                .get("undeclared_test")
                .and_then(|metric| metric.inner.as_ref()),
            Some(&filtered_metrics::Inner::Count(4)),
        );
    }

    #[test]
    fn implicit_cluster_capture_is_recorded_at_labelled_detail() {
        for detail in [MetricDetailLevel::Cluster, MetricDetailLevel::Backend] {
            let mut aggregator = Aggregator::new(String::new());
            aggregator.set_up_detail(detail);
            let capture = aggregator.capture_cluster_metrics("undeclared");
            assert!(matches!(capture, ClusterMetricsCapture::Active(_)));
            aggregator.receive_metric_for_incarnation(
                "undeclared_test",
                Some("undeclared"),
                Some("backend"),
                capture,
                MetricValue::Count(4),
            );
            let backend_id = match detail {
                MetricDetailLevel::Backend => Some("backend"),
                _ => None,
            };
            let recorded =
                local_labelled_value(&mut aggregator, "undeclared", backend_id, "undeclared_test");
            assert_eq!(
                recorded,
                Some(filtered_metrics::Inner::Count(4)),
                "the active implicit identity must pass the gate at {detail:?}",
            );
        }
    }

    #[test]
    fn mismatched_incarnation_is_dropped_at_labelled_detail() {
        for detail in [MetricDetailLevel::Cluster, MetricDetailLevel::Backend] {
            let mut aggregator = Aggregator::new(String::new());
            aggregator.set_up_detail(detail);

            // Captured Some(I1), current Some(I2).
            aggregator.add_cluster("c");
            let first = aggregator
                .cluster_incarnation("c")
                .expect("first configured identity");
            aggregator.remove_cluster("c");
            aggregator.add_cluster("c");
            let second = aggregator
                .cluster_incarnation("c")
                .expect("replacement configured identity");
            assert_ne!(first, second);
            aggregator.receive_metric_for_incarnation(
                "mismatch_test",
                Some("c"),
                Some("b"),
                ClusterMetricsCapture::Active(first),
                MetricValue::Count(1),
            );

            // A missing capture never adopts the current lifetime.
            aggregator.receive_metric_for_incarnation(
                "mismatch_test",
                Some("c"),
                Some("b"),
                ClusterMetricsCapture::Missing,
                MetricValue::Count(1),
            );
            assert!(
                !aggregator
                    .local
                    .dump_cluster_metrics(&[])
                    .expect("dump cluster metrics")
                    .contains_key("c"),
                "stale or missing capture must not reach the Some(I2) row at {detail:?}",
            );

            // Captured I1, current identity absent under this id.
            aggregator.receive_metric_for_incarnation(
                "mismatch_test",
                Some("other"),
                Some("b"),
                ClusterMetricsCapture::Active(first),
                MetricValue::Count(1),
            );
            assert!(
                !aggregator
                    .local
                    .dump_cluster_metrics(&[])
                    .expect("dump cluster metrics")
                    .contains_key("other"),
                "Some(I1) must not reach an undeclared (None) cluster at {detail:?}",
            );
        }
    }

    #[test]
    fn removed_implicit_capture_stays_rejected_after_clear() {
        let mut aggregator = Aggregator::new(String::new());
        aggregator.set_up_detail(MetricDetailLevel::Backend);

        // A legal route allocates an implicit identity before this cluster
        // has ever received AddCluster.
        let captured_before_add = aggregator.capture_cluster_metrics("reviewer-reused");
        let ClusterMetricsCapture::Active(implicit_token) = captured_before_add else {
            panic!("a legal implicit route must capture an active identity");
        };

        aggregator.add_cluster("reviewer-reused");
        assert_eq!(
            aggregator.cluster_incarnation("reviewer-reused"),
            Some(implicit_token),
            "AddCluster without RemoveCluster keeps the implicit lifetime",
        );
        aggregator.remove_cluster("reviewer-reused");

        // Clear is a supported metrics operation. It clears drain storage,
        // including tombstones, but it must not make a removed lifetime's
        // earlier identity become current again.
        aggregator.configure(&MetricsConfiguration::Clear);
        aggregator.receive_metric_for_incarnation(
            "reviewer_late_close",
            Some("reviewer-reused"),
            Some("backend"),
            captured_before_add,
            MetricValue::Count(1),
        );

        assert!(
            !aggregator
                .local
                .dump_cluster_metrics(&[])
                .expect("dump metrics after late removed emission")
                .contains_key("reviewer-reused"),
            "a removed lifetime must stay fenced after Clear removes the drain tombstone",
        );

        let replacement = aggregator.capture_cluster_metrics("reviewer-reused");
        let ClusterMetricsCapture::Active(replacement_token) = replacement else {
            panic!("a route after removal must allocate a replacement identity");
        };
        assert_ne!(replacement_token, implicit_token);
        aggregator.add_cluster("reviewer-reused");
        assert_eq!(
            aggregator.cluster_incarnation("reviewer-reused"),
            Some(replacement_token),
        );
        aggregator.receive_metric_for_incarnation(
            "reviewer_replacement",
            Some("reviewer-reused"),
            Some("backend"),
            captured_before_add,
            MetricValue::Count(1),
        );
        aggregator.receive_metric_for_incarnation(
            "reviewer_replacement",
            Some("reviewer-reused"),
            Some("backend"),
            replacement,
            MetricValue::Count(2),
        );
        assert_eq!(
            local_labelled_value(
                &mut aggregator,
                "reviewer-reused",
                Some("backend"),
                "reviewer_replacement",
            ),
            Some(filtered_metrics::Inner::Count(2)),
            "only the post-remove identity may reach the replacement row",
        );
    }

    #[test]
    fn implicit_route_after_remove_rearms_drains_without_add_cluster() {
        let mut aggregator = Aggregator::new(String::new());
        aggregator.set_up_detail(MetricDetailLevel::Backend);

        let first = aggregator.capture_cluster_metrics("implicit-replacement");
        aggregator.receive_metric_for_incarnation(
            "implicit_replacement",
            Some("implicit-replacement"),
            Some("backend"),
            first,
            MetricValue::Count(1),
        );
        aggregator.remove_cluster("implicit-replacement");

        // Frontends and backends may legally recreate this id without an
        // AddCluster. The first new route is therefore both the identity and
        // drain-lifetime boundary for the replacement.
        let replacement = aggregator.capture_cluster_metrics("implicit-replacement");
        assert_ne!(replacement, first);
        aggregator.receive_metric_for_incarnation(
            "implicit_replacement",
            Some("implicit-replacement"),
            Some("backend"),
            replacement,
            MetricValue::Count(2),
        );
        aggregator.receive_metric_for_incarnation(
            "implicit_replacement",
            Some("implicit-replacement"),
            Some("backend"),
            first,
            MetricValue::Count(4),
        );

        assert_eq!(
            local_labelled_value(
                &mut aggregator,
                "implicit-replacement",
                Some("backend"),
                "implicit_replacement",
            ),
            Some(filtered_metrics::Inner::Count(2)),
            "the replacement implicit lifetime must be visible while the removed lifetime stays fenced",
        );
    }

    #[test]
    fn exhausted_add_is_not_an_implicit_identity() {
        let mut aggregator = Aggregator::new(String::new());
        aggregator.set_up_detail(MetricDetailLevel::Backend);
        aggregator.last_cluster_incarnation = u64::MAX;
        aggregator.add_cluster("reviewer-exhausted");
        assert_eq!(aggregator.cluster_incarnation("reviewer-exhausted"), None);

        aggregator.receive_metric_for_incarnation(
            "reviewer_exhausted",
            Some("reviewer-exhausted"),
            Some("backend"),
            ClusterMetricsCapture::Invalid,
            MetricValue::Count(1),
        );

        assert!(
            !aggregator
                .local
                .dump_cluster_metrics(&[])
                .expect("dump metrics after exhausted emission")
                .contains_key("reviewer-exhausted"),
            "allocation exhaustion must fail closed instead of sharing the undeclared identity",
        );
    }

    #[test]
    fn exhausted_implicit_capture_does_not_rearm_removed_drains() {
        let mut aggregator = Aggregator::new(String::new());
        aggregator.set_up_detail(MetricDetailLevel::Backend);
        assert!(matches!(
            aggregator.capture_cluster_metrics("exhausted-after-remove"),
            ClusterMetricsCapture::Active(_),
        ));
        aggregator.remove_cluster("exhausted-after-remove");
        aggregator.last_cluster_incarnation = u64::MAX;

        let exhausted = aggregator.capture_cluster_metrics("exhausted-after-remove");
        assert_eq!(exhausted, ClusterMetricsCapture::Invalid);
        aggregator.receive_metric_for_incarnation(
            "exhausted_after_remove",
            Some("exhausted-after-remove"),
            Some("backend"),
            exhausted,
            MetricValue::Count(1),
        );
        // Probe the drain directly as well: the identity-aware call above is
        // rejected before dispatch and therefore cannot prove that allocation
        // exhaustion left the RemoveCluster tombstone armed.
        aggregator.receive_metric(
            "exhausted_after_remove_probe",
            Some("exhausted-after-remove"),
            Some("backend"),
            MetricValue::Count(1),
        );

        assert!(
            !aggregator
                .local
                .dump_cluster_metrics(&[])
                .expect("dump metrics after exhausted implicit capture")
                .contains_key("exhausted-after-remove"),
            "allocation exhaustion must leave the removed drain tombstone armed",
        );
    }

    #[test]
    fn every_capture_state_keeps_process_and_frontend_aggregates() {
        for detail in [MetricDetailLevel::Process, MetricDetailLevel::Frontend] {
            let mut aggregator = Aggregator::new(String::new());
            aggregator.set_up_detail(detail);
            let active = aggregator.capture_cluster_metrics("reviewer-undeclared");
            for capture in [
                active,
                ClusterMetricsCapture::Missing,
                ClusterMetricsCapture::Invalid,
            ] {
                aggregator.receive_metric_for_incarnation(
                    "reviewer_unconfigured_aggregate",
                    Some("reviewer-undeclared"),
                    Some("backend"),
                    capture,
                    MetricValue::Count(1),
                );
            }
            assert_eq!(
                aggregator
                    .dump_local_proxy_metrics()
                    .get("reviewer_unconfigured_aggregate")
                    .and_then(|metric| metric.inner.as_ref()),
                Some(&filtered_metrics::Inner::Count(3)),
                "identity state cannot suppress a label-free {detail:?} aggregate",
            );
        }
    }

    #[test]
    fn filter_labels_backend_keeps_both() {
        assert_eq!(
            filter_labels_for_detail(MetricDetailLevel::Backend, Some("c"), Some("b")),
            (Some("c"), Some("b")),
        );
    }

    #[test]
    fn filter_labels_none_in_none_out() {
        // Absent labels stay absent regardless of detail level — the filter
        // never invents a label, only drops.
        for detail in [
            MetricDetailLevel::Process,
            MetricDetailLevel::Frontend,
            MetricDetailLevel::Cluster,
            MetricDetailLevel::Backend,
        ] {
            assert_eq!(filter_labels_for_detail(detail, None, None), (None, None));
        }
    }

    #[test]
    fn aggregator_default_detail_is_cluster() {
        // Pre-knob behaviour preserved: if a worker / process never calls
        // `set_up_detail`, cluster-scoped metrics still reach the drains.
        let agg = Aggregator::new("sozu".to_owned());
        assert_eq!(agg.detail_configured(), MetricDetailLevel::Cluster);
        assert_eq!(agg.detail_effective(), MetricDetailLevel::Cluster);
        assert_eq!(agg.lease_count(), 0);
    }

    /// Fully-known binding used by tests that don't otherwise care about the
    /// peer-binding mechanic. Two distinct values (`OWNER_*` / `OTHER_*`)
    /// let `lease_clear` tests assert authorised vs unauthorised paths.
    fn owner_binding() -> PeerBinding {
        PeerBinding {
            pid: Some(1234),
            session_ulid: Some(0x0123_4567_89ab_cdef_0123_4567_89ab_cdefu128),
        }
    }

    fn other_binding() -> PeerBinding {
        PeerBinding {
            pid: Some(5678),
            session_ulid: Some(0xfedc_ba98_7654_3210_fedc_ba98_7654_3210u128),
        }
    }

    /// Extract `(previous_effective, new_effective)` from a successful
    /// `lease_apply`; panics on any failure arm so tests that don't care
    /// about the failure paths stay compact.
    fn unwrap_applied(outcome: LeaseApplyOutcome) -> (MetricDetailLevel, MetricDetailLevel) {
        match outcome {
            LeaseApplyOutcome::Applied {
                previous_effective,
                new_effective,
            } => (previous_effective, new_effective),
            other => panic!("expected LeaseApplyOutcome::Applied, got {other:?}"),
        }
    }

    #[test]
    fn lease_apply_elevates_effective_above_configured() {
        // Configured floor stays at Cluster; a lease for Backend lifts the
        // effective level until the lease expires or is cleared.
        let mut agg = Aggregator::new("sozu".to_owned());
        agg.set_up_detail(MetricDetailLevel::Cluster);
        let (prev, new) = unwrap_applied(agg.lease_apply(
            "test:1".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(60),
            PeerBinding::default(),
            Instant::now(),
        ));
        assert_eq!(prev, MetricDetailLevel::Cluster);
        assert_eq!(new, MetricDetailLevel::Backend);
        assert_eq!(agg.detail_configured(), MetricDetailLevel::Cluster);
        assert_eq!(agg.detail_effective(), MetricDetailLevel::Backend);
        assert_eq!(agg.lease_count(), 1);
    }

    #[test]
    fn lease_apply_below_configured_does_not_lower_effective() {
        // A lease can only ELEVATE the floor, never push below `configured`.
        let mut agg = Aggregator::new("sozu".to_owned());
        agg.set_up_detail(MetricDetailLevel::Backend);
        let (prev, new) = unwrap_applied(agg.lease_apply(
            "test:1".to_owned(),
            MetricDetailLevel::Cluster,
            Duration::from_secs(60),
            PeerBinding::default(),
            Instant::now(),
        ));
        assert_eq!(prev, MetricDetailLevel::Backend);
        assert_eq!(new, MetricDetailLevel::Backend);
    }

    #[test]
    fn lease_apply_rejects_client_id_over_cap() {
        // Defence-in-depth: even if dispatch lets a too-long id through,
        // the aggregator refuses to store it.
        let mut agg = Aggregator::new("sozu".to_owned());
        let too_long = "x".repeat(LEASE_CLIENT_ID_MAX_BYTES + 1);
        assert_eq!(
            agg.lease_apply(
                too_long,
                MetricDetailLevel::Backend,
                Duration::from_secs(60),
                PeerBinding::default(),
                Instant::now(),
            ),
            LeaseApplyOutcome::ClientIdTooLong
        );
        assert_eq!(agg.lease_count(), 0);
    }

    #[test]
    fn lease_apply_rejects_when_table_is_full() {
        // Fill the table to capacity with distinct client_ids; one more
        // insert is refused. A RENEWAL of an existing entry must still
        // succeed (replaces in place, count unchanged).
        let mut agg = Aggregator::new("sozu".to_owned());
        let now = Instant::now();
        for i in 0..LEASE_TABLE_CAP {
            assert!(matches!(
                agg.lease_apply(
                    format!("client:{i:02}"),
                    MetricDetailLevel::Backend,
                    Duration::from_secs(60),
                    PeerBinding::default(),
                    now,
                ),
                LeaseApplyOutcome::Applied { .. }
            ));
        }
        assert_eq!(agg.lease_count() as usize, LEASE_TABLE_CAP);
        // New distinct client → rejected.
        assert_eq!(
            agg.lease_apply(
                "newcomer".to_owned(),
                MetricDetailLevel::Backend,
                Duration::from_secs(60),
                PeerBinding::default(),
                now,
            ),
            LeaseApplyOutcome::TableFull,
        );
        assert_eq!(agg.lease_count() as usize, LEASE_TABLE_CAP);
        // Renewal of an existing entry → still accepted.
        assert!(matches!(
            agg.lease_apply(
                "client:00".to_owned(),
                MetricDetailLevel::Backend,
                Duration::from_secs(120),
                PeerBinding::default(),
                now,
            ),
            LeaseApplyOutcome::Applied { .. }
        ));
        assert_eq!(agg.lease_count() as usize, LEASE_TABLE_CAP);
    }

    #[test]
    fn lease_apply_rejects_ttl_over_max() {
        // The aggregator no longer silently clamps oversize TTLs.
        let mut agg = Aggregator::new("sozu".to_owned());
        assert_eq!(
            agg.lease_apply(
                "client:0".to_owned(),
                MetricDetailLevel::Backend,
                LEASE_TTL_MAX + Duration::from_secs(1),
                PeerBinding::default(),
                Instant::now(),
            ),
            LeaseApplyOutcome::TtlOutOfRange,
        );
        assert_eq!(agg.lease_count(), 0);
    }

    #[test]
    fn lease_apply_renewal_replaces_previous_for_same_client() {
        // The renewer client re-sends with the same `client_id`; the entry
        // is REPLACED (not duplicated). Lease count stays at 1.
        // Unknown bindings on both sides skip the renewal-binding gate.
        let mut agg = Aggregator::new("sozu".to_owned());
        let now = Instant::now();
        let _ = agg.lease_apply(
            "renewer".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(30),
            PeerBinding::default(),
            now,
        );
        let _ = agg.lease_apply(
            "renewer".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(60),
            PeerBinding::default(),
            now,
        );
        assert_eq!(agg.lease_count(), 1);
    }

    #[test]
    fn lease_apply_renewal_at_lower_level_lowers_effective() {
        // Issue #1408: a client holding the current maximum renews its own
        // lease at a LOWER level. A lease only ever elevates cardinality on
        // the client's own behalf, so a client giving part of that back is
        // withdrawing its own request, not violating a shared invariant.
        // `effective` must recompute to max(configured, max over live
        // leases) exactly like any other apply -- dropping here is the
        // correct outcome, not a bug.
        let mut agg = Aggregator::new("sozu".to_owned());
        agg.set_up_detail(MetricDetailLevel::Process);
        let now = Instant::now();
        let (_, new) = unwrap_applied(agg.lease_apply(
            "renewer".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(60),
            PeerBinding::default(),
            now,
        ));
        assert_eq!(new, MetricDetailLevel::Backend);
        assert_eq!(agg.detail_effective(), MetricDetailLevel::Backend);
        // Same client_id renews at a strictly lower level.
        let (prev, new) = unwrap_applied(agg.lease_apply(
            "renewer".to_owned(),
            MetricDetailLevel::Frontend,
            Duration::from_secs(60),
            PeerBinding::default(),
            now,
        ));
        assert_eq!(prev, MetricDetailLevel::Backend);
        assert_eq!(new, MetricDetailLevel::Frontend);
        assert_eq!(agg.detail_effective(), MetricDetailLevel::Frontend);
        assert_eq!(agg.lease_count(), 1);
    }

    #[test]
    fn lease_apply_renewal_rejects_foreign_binding() {
        // Same-UID `client_id` collision attack: the victim applies with
        // a known binding; an attacker that learns the `client_id`
        // attempts to renew under a different (pid, session_ulid). The
        // renewal must be refused so the victim's apply-time binding
        // remains the sole authoritative owner — both for subsequent
        // renewals AND for the victim's own Drop-time `clear`.
        let mut agg = Aggregator::new("sozu".to_owned());
        let now = Instant::now();
        let victim = PeerBinding {
            pid: Some(4242),
            session_ulid: Some(0x0123_4567_89AB_CDEF_FEDC_BA98_7654_3210),
        };
        let outcome = agg.lease_apply(
            "topcli".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(60),
            victim,
            now,
        );
        assert!(
            matches!(outcome, LeaseApplyOutcome::Applied { .. }),
            "victim's initial apply must succeed"
        );
        let attacker = PeerBinding {
            pid: Some(9999),
            session_ulid: Some(0xDEAD_BEEF_DEAD_BEEF_DEAD_BEEF_DEAD_BEEF),
        };
        let outcome = agg.lease_apply(
            "topcli".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(60),
            attacker,
            now,
        );
        assert_eq!(
            outcome,
            LeaseApplyOutcome::Unauthorized,
            "renewal with a mismatched known binding must be refused"
        );
        // The victim can still clear their own lease — proof the
        // refused attempt did not corrupt the stored binding.
        let clear = agg.lease_clear("topcli", victim);
        assert!(
            matches!(clear, LeaseClearOutcome::Cleared { .. }),
            "victim's original binding must still clear cleanly after \
             the foreign-binding renewal was refused"
        );
    }

    #[test]
    fn lease_apply_renewal_with_matching_binding_succeeds() {
        // Symmetry case: the legitimate owner re-applies with the same
        // (pid, session_ulid). The renewal must succeed so the TUI's
        // own renewer thread keeps the lease alive across its TTL.
        let mut agg = Aggregator::new("sozu".to_owned());
        let now = Instant::now();
        let owner = PeerBinding {
            pid: Some(1234),
            session_ulid: Some(0xAAAA_BBBB_CCCC_DDDD_EEEE_FFFF_0000_1111),
        };
        let _ = agg.lease_apply(
            "topcli".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(30),
            owner,
            now,
        );
        let outcome = agg.lease_apply(
            "topcli".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(60),
            owner,
            now,
        );
        assert!(
            matches!(outcome, LeaseApplyOutcome::Applied { .. }),
            "renewal with matching binding must succeed (otherwise the \
             TUI's own renewer thread would be locked out)"
        );
    }

    #[test]
    fn lease_apply_max_merge_two_clients() {
        // Two clients, two levels: effective = max(both leases, configured).
        // Use `Process` as the floor so the Frontend lease is observable
        // after the Backend lease is cleared (otherwise the configured
        // Cluster floor would mask the Frontend lease).
        let mut agg = Aggregator::new("sozu".to_owned());
        agg.set_up_detail(MetricDetailLevel::Process);
        let now = Instant::now();
        let _ = agg.lease_apply(
            "scraper".to_owned(),
            MetricDetailLevel::Frontend,
            Duration::from_secs(60),
            PeerBinding::default(),
            now,
        );
        let _ = agg.lease_apply(
            "topcli".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(60),
            PeerBinding::default(),
            now,
        );
        assert_eq!(agg.detail_effective(), MetricDetailLevel::Backend);
        assert_eq!(agg.lease_count(), 2);
        // Clearing the higher lease drops effective back to the lower one.
        let outcome = agg.lease_clear("topcli", PeerBinding::default());
        assert_eq!(
            outcome,
            LeaseClearOutcome::Cleared {
                previous_effective: MetricDetailLevel::Backend,
            }
        );
        assert_eq!(agg.detail_effective(), MetricDetailLevel::Frontend);
        assert_eq!(agg.lease_count(), 1);
    }

    #[test]
    fn lease_clear_unknown_id_is_silent_noop() {
        // Mismatched IDs are silently ignored (other clients' leases unaffected).
        let mut agg = Aggregator::new("sozu".to_owned());
        let _ = agg.lease_apply(
            "real".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(60),
            PeerBinding::default(),
            Instant::now(),
        );
        assert_eq!(
            agg.lease_clear("ghost", PeerBinding::default()),
            LeaseClearOutcome::NotFound
        );
        assert_eq!(agg.detail_effective(), MetricDetailLevel::Backend);
        assert_eq!(agg.lease_count(), 1);
    }

    #[test]
    fn lease_clear_with_matching_binding_authorised() {
        // Apply with a known binding, clear with the same binding -> Cleared.
        let mut agg = Aggregator::new("sozu".to_owned());
        let _ = agg.lease_apply(
            "owner-lease".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(60),
            owner_binding(),
            Instant::now(),
        );
        let outcome = agg.lease_clear("owner-lease", owner_binding());
        assert!(matches!(outcome, LeaseClearOutcome::Cleared { .. }));
        assert_eq!(agg.lease_count(), 0);
    }

    #[test]
    fn lease_clear_with_mismatched_binding_is_unauthorized() {
        // Apply with one binding, attempt clear with a different binding ->
        // Unauthorized; lease left intact.
        let mut agg = Aggregator::new("sozu".to_owned());
        let _ = agg.lease_apply(
            "owner-lease".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(60),
            owner_binding(),
            Instant::now(),
        );
        let outcome = agg.lease_clear("owner-lease", other_binding());
        assert_eq!(outcome, LeaseClearOutcome::Unauthorized);
        assert_eq!(agg.lease_count(), 1);
    }

    #[test]
    fn lease_clear_unknown_apply_binding_accepts_any_clear() {
        // Pre-binding / non-Linux apply -> any clear authorised.
        let mut agg = Aggregator::new("sozu".to_owned());
        let _ = agg.lease_apply(
            "legacy".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(60),
            PeerBinding::default(),
            Instant::now(),
        );
        let outcome = agg.lease_clear("legacy", owner_binding());
        assert!(matches!(outcome, LeaseClearOutcome::Cleared { .. }));
        assert_eq!(agg.lease_count(), 0);
    }

    #[test]
    fn lease_clear_known_apply_rejects_default_clear() {
        // Known apply binding -> a default ("unknown") clear is rejected.
        let mut agg = Aggregator::new("sozu".to_owned());
        let _ = agg.lease_apply(
            "owner-lease".to_owned(),
            MetricDetailLevel::Backend,
            Duration::from_secs(60),
            owner_binding(),
            Instant::now(),
        );
        let outcome = agg.lease_clear("owner-lease", PeerBinding::default());
        assert_eq!(outcome, LeaseClearOutcome::Unauthorized);
    }

    #[test]
    fn lease_tick_expires_only_past_due_leases() {
        // `lease_tick(now)` parameterises the clock so we can test expiry
        // without sleeping. Setup: one lease past due, one still active.
        // Use `Process` as the floor so the surviving Frontend lease drives
        // the effective level after the Backend lease expires (otherwise
        // the Cluster floor would mask it).
        let mut agg = Aggregator::new("sozu".to_owned());
        agg.set_up_detail(MetricDetailLevel::Process);
        let now = Instant::now();
        // Inject directly into the table to control expires_at deterministically.
        agg.leases.insert(
            "expired".to_owned(),
            LeaseEntry {
                level: MetricDetailLevel::Backend,
                expires_at: now - Duration::from_secs(1),
                binding: PeerBinding::default(),
            },
        );
        agg.leases.insert(
            "live".to_owned(),
            LeaseEntry {
                level: MetricDetailLevel::Frontend,
                expires_at: now + Duration::from_secs(60),
                binding: PeerBinding::default(),
            },
        );
        agg.recompute_effective();
        assert_eq!(agg.detail_effective(), MetricDetailLevel::Backend);
        let prev = agg.lease_tick(now);
        assert_eq!(prev, Some(MetricDetailLevel::Backend));
        assert_eq!(agg.detail_effective(), MetricDetailLevel::Frontend);
        assert_eq!(agg.lease_count(), 1);
    }

    #[test]
    fn lease_tick_no_change_returns_none() {
        // No leases -> no-op tick, no audit signal.
        let mut agg = Aggregator::new("sozu".to_owned());
        assert!(agg.lease_tick(Instant::now()).is_none());
    }

    #[test]
    fn lease_apply_at_max_ttl_succeeds() {
        // Boundary: exactly LEASE_TTL_MAX is allowed; LEASE_TTL_MAX + 1ns is
        // rejected (covered by lease_apply_rejects_ttl_over_max above). With
        // `now` injected (rather than read from the host clock) the expiry
        // is checked for EXACT equality to `now + LEASE_TTL_MAX`, not a
        // fuzzy upper bound — the whole point of parameterising the clock.
        let mut agg = Aggregator::new("sozu".to_owned());
        let now = Instant::now();
        let outcome = agg.lease_apply(
            "max".to_owned(),
            MetricDetailLevel::Backend,
            LEASE_TTL_MAX,
            PeerBinding::default(),
            now,
        );
        assert!(matches!(outcome, LeaseApplyOutcome::Applied { .. }));
        let entry = agg.leases.get("max").unwrap();
        assert_eq!(entry.expires_at, now + LEASE_TTL_MAX);
    }
}