conproxy 0.2.1

Cache proxy server for heterogeneous RAG/vector search backends with cascade, scope, and MCP
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
//! Metrics collection for the cache proxy.
//!
//! Tracks cache hit/miss rates, latencies, and other performance metrics
//! using lock-free atomic counters.

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

use super::types::{DriftLevel, MissReason};

/// Size of the latency ring buffer for percentile calculation.
const LATENCY_RING_SIZE: usize = 1000;

/// Metrics collector for the cache proxy.
#[derive(Debug)]
pub struct ProxyMetrics {
    /// Total number of requests processed.
    pub requests_total: AtomicU64,
    /// Number of cache hits (fresh).
    pub cache_hits: AtomicU64,
    /// Number of cache misses.
    pub cache_misses: AtomicU64,
    /// Cache misses: entry not in cache (cold miss).
    pub miss_not_in_cache: AtomicU64,
    /// Cache misses: entry expired, fetched from upstream.
    pub miss_expired: AtomicU64,
    /// Cache misses: upstream request failed, no frozen fallback.
    pub miss_upstream_error: AtomicU64,
    /// Cache misses: circuit breaker open, no frozen fallback.
    pub miss_circuit_open: AtomicU64,
    /// Number of stale-while-revalidate responses.
    pub cache_stale: AtomicU64,
    /// Number of frozen responses (upstream down).
    pub cache_frozen: AtomicU64,
    /// Number of coalesced requests (waited on leader).
    pub coalesced_requests: AtomicU64,
    /// Number of upstream requests made.
    pub upstream_requests: AtomicU64,
    /// Number of upstream request failures.
    pub upstream_failures: AtomicU64,
    /// Number of validation errors (bad requests).
    pub validation_errors: AtomicU64,
    /// Total latency in microseconds (for average calculation).
    latency_sum_us: AtomicU64,
    /// Maximum latency observed in microseconds.
    latency_max_us: AtomicU64,
    /// Minimum latency observed in microseconds (initialized to max).
    latency_min_us: AtomicU64,
    /// Number of refresh operations.
    pub refresh_operations: AtomicU64,
    /// Number of evictions.
    pub evictions: AtomicU64,
    /// Number of drift alerts triggered.
    pub drift_alerts: AtomicU64,
    /// Drift observations by level.
    pub drift_none: AtomicU64,
    pub drift_minor: AtomicU64,
    pub drift_moderate: AtomicU64,
    pub drift_significant: AtomicU64,
    pub drift_major: AtomicU64,
    /// Number of integrity check failures.
    pub integrity_failures: AtomicU64,
    /// Number of schema changes detected.
    pub schema_changes: AtomicU64,
    // Cascade metrics
    /// Number of successful cascade queries (threshold met).
    pub cascade_success: AtomicU64,
    /// Number of cascade queries that exhausted all upstreams.
    pub cascade_exhausted: AtomicU64,
    /// Number of cascade queries that timed out.
    pub cascade_timeout: AtomicU64,
    /// Sum of cascade depths (for averaging).
    cascade_depth_sum: AtomicU64,
    /// Number of cascade depth observations.
    cascade_depth_count: AtomicU64,
    // Warmup metrics
    /// Number of warmup requests (POST /cache/warmup calls).
    pub warmup_requests: AtomicU64,
    /// Number of individual entries warmed (successful cache inserts from warmup).
    pub warmup_entries: AtomicU64,
    /// Number of semantic cache hits (similarity above threshold).
    #[cfg(feature = "embed-api")]
    pub semantic_hits: AtomicU64,
    /// Number of semantic cache misses (no similar query above threshold).
    #[cfg(feature = "embed-api")]
    pub semantic_misses: AtomicU64,
    /// Pre-allocated lock-free ring buffer of recent latencies (microseconds).
    latency_ring: Box<[AtomicU64]>,
    /// Write position in the latency ring buffer (monotonically increasing).
    latency_ring_pos: AtomicU64,
}

impl Default for ProxyMetrics {
    fn default() -> Self {
        Self {
            requests_total: AtomicU64::new(0),
            cache_hits: AtomicU64::new(0),
            cache_misses: AtomicU64::new(0),
            miss_not_in_cache: AtomicU64::new(0),
            miss_expired: AtomicU64::new(0),
            miss_upstream_error: AtomicU64::new(0),
            miss_circuit_open: AtomicU64::new(0),
            cache_stale: AtomicU64::new(0),
            cache_frozen: AtomicU64::new(0),
            coalesced_requests: AtomicU64::new(0),
            upstream_requests: AtomicU64::new(0),
            upstream_failures: AtomicU64::new(0),
            validation_errors: AtomicU64::new(0),
            latency_sum_us: AtomicU64::new(0),
            latency_max_us: AtomicU64::new(0),
            latency_min_us: AtomicU64::new(u64::MAX),
            refresh_operations: AtomicU64::new(0),
            evictions: AtomicU64::new(0),
            drift_alerts: AtomicU64::new(0),
            drift_none: AtomicU64::new(0),
            drift_minor: AtomicU64::new(0),
            drift_moderate: AtomicU64::new(0),
            drift_significant: AtomicU64::new(0),
            drift_major: AtomicU64::new(0),
            integrity_failures: AtomicU64::new(0),
            schema_changes: AtomicU64::new(0),
            cascade_success: AtomicU64::new(0),
            cascade_exhausted: AtomicU64::new(0),
            cascade_timeout: AtomicU64::new(0),
            cascade_depth_sum: AtomicU64::new(0),
            cascade_depth_count: AtomicU64::new(0),
            warmup_requests: AtomicU64::new(0),
            warmup_entries: AtomicU64::new(0),
            #[cfg(feature = "embed-api")]
            semantic_hits: AtomicU64::new(0),
            #[cfg(feature = "embed-api")]
            semantic_misses: AtomicU64::new(0),
            latency_ring: (0..LATENCY_RING_SIZE)
                .map(|_| AtomicU64::new(0))
                .collect::<Vec<_>>()
                .into_boxed_slice(),
            latency_ring_pos: AtomicU64::new(0),
        }
    }
}

impl ProxyMetrics {
    /// Create a new metrics collector.
    pub fn new() -> Self {
        Self::default()
    }

    /// Record a cache hit.
    pub fn record_hit(&self) {
        self.requests_total.fetch_add(1, Ordering::Relaxed);
        self.cache_hits.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a cache miss with reason.
    pub fn record_miss(&self, reason: MissReason) {
        self.requests_total.fetch_add(1, Ordering::Relaxed);
        self.cache_misses.fetch_add(1, Ordering::Relaxed);
        match reason {
            MissReason::NotInCache => self.miss_not_in_cache.fetch_add(1, Ordering::Relaxed),
            MissReason::Expired => self.miss_expired.fetch_add(1, Ordering::Relaxed),
            MissReason::UpstreamError => self.miss_upstream_error.fetch_add(1, Ordering::Relaxed),
            MissReason::CircuitOpen => self.miss_circuit_open.fetch_add(1, Ordering::Relaxed),
        };
    }

    /// Record a stale response.
    pub fn record_stale(&self) {
        self.requests_total.fetch_add(1, Ordering::Relaxed);
        self.cache_stale.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a frozen response.
    pub fn record_frozen(&self) {
        self.requests_total.fetch_add(1, Ordering::Relaxed);
        self.cache_frozen.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a coalesced request (not leader).
    pub fn record_coalesced(&self) {
        self.coalesced_requests.fetch_add(1, Ordering::Relaxed);
    }

    /// Record an upstream request.
    pub fn record_upstream_request(&self) {
        self.upstream_requests.fetch_add(1, Ordering::Relaxed);
    }

    /// Record an upstream failure.
    pub fn record_upstream_failure(&self) {
        self.upstream_failures.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a validation error.
    pub fn record_validation_error(&self) {
        self.validation_errors.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a request latency.
    pub fn record_latency(&self, duration: Duration) {
        let us = duration.as_micros() as u64;
        self.latency_sum_us.fetch_add(us, Ordering::Relaxed);

        // Update max (compare-and-swap loop)
        let mut current_max = self.latency_max_us.load(Ordering::Relaxed);
        while us > current_max {
            match self.latency_max_us.compare_exchange_weak(
                current_max,
                us,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(actual) => current_max = actual,
            }
        }

        // Update min
        let mut current_min = self.latency_min_us.load(Ordering::Relaxed);
        while us < current_min {
            match self.latency_min_us.compare_exchange_weak(
                current_min,
                us,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(actual) => current_min = actual,
            }
        }

        // Record to lock-free ring buffer for percentile calculation
        let pos = self.latency_ring_pos.fetch_add(1, Ordering::Relaxed) as usize;
        let ring_idx = pos % LATENCY_RING_SIZE;
        if let Some(slot) = self.latency_ring.get(ring_idx) {
            slot.store(us, Ordering::Relaxed);
        }
    }

    /// Record a refresh operation.
    pub fn record_refresh(&self) {
        self.refresh_operations.fetch_add(1, Ordering::Relaxed);
    }

    /// Record an eviction.
    pub fn record_eviction(&self) {
        self.evictions.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a drift alert.
    pub fn record_drift_alert(&self) {
        self.drift_alerts.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a drift observation by level.
    pub fn record_drift(&self, level: DriftLevel) {
        match level {
            DriftLevel::None => self.drift_none.fetch_add(1, Ordering::Relaxed),
            DriftLevel::Minor => self.drift_minor.fetch_add(1, Ordering::Relaxed),
            DriftLevel::Moderate => self.drift_moderate.fetch_add(1, Ordering::Relaxed),
            DriftLevel::Significant => {
                self.drift_significant.fetch_add(1, Ordering::Relaxed);
                // Significant drift triggers an alert
                self.drift_alerts.fetch_add(1, Ordering::Relaxed);
                0
            }
            DriftLevel::Major => {
                self.drift_major.fetch_add(1, Ordering::Relaxed);
                // Major drift triggers an alert
                self.drift_alerts.fetch_add(1, Ordering::Relaxed);
                0
            }
        };
    }

    /// Record an integrity check failure.
    pub fn record_integrity_failure(&self) {
        self.integrity_failures.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a schema change detection.
    pub fn record_schema_change(&self) {
        self.schema_changes.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a successful cascade query.
    ///
    /// # Arguments
    /// * `depth` - The cascade depth (number of upstreams tried).
    pub fn record_cascade_success(&self, depth: usize) {
        self.cascade_success.fetch_add(1, Ordering::Relaxed);
        self.cascade_depth_sum
            .fetch_add(depth as u64, Ordering::Relaxed);
        self.cascade_depth_count.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a cascade query that exhausted all upstreams.
    pub fn record_cascade_exhausted(&self) {
        self.cascade_exhausted.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a cascade query that timed out.
    pub fn record_cascade_timeout(&self) {
        self.cascade_timeout.fetch_add(1, Ordering::Relaxed);
    }

    /// Record cascade depth (for non-success cases).
    pub fn record_cascade_depth(&self, depth: usize) {
        self.cascade_depth_sum
            .fetch_add(depth as u64, Ordering::Relaxed);
        self.cascade_depth_count.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a warmup request (one POST /cache/warmup call).
    pub fn record_warmup_request(&self) {
        self.warmup_requests.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a single warmup entry (one cache insert from warmup).
    pub fn record_warmup_entry(&self) {
        self.warmup_entries.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a semantic cache hit (cosine similarity above threshold).
    #[cfg(feature = "embed-api")]
    pub fn record_semantic_hit(&self) {
        self.semantic_hits.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a semantic cache miss (no similar query above threshold).
    #[cfg(feature = "embed-api")]
    pub fn record_semantic_miss(&self) {
        self.semantic_misses.fetch_add(1, Ordering::Relaxed);
    }

    /// Get the current metrics snapshot.
    pub fn snapshot(&self) -> MetricsSnapshot {
        let total = self.requests_total.load(Ordering::Relaxed);
        let hits = self.cache_hits.load(Ordering::Relaxed);
        let misses = self.cache_misses.load(Ordering::Relaxed);
        let latency_sum = self.latency_sum_us.load(Ordering::Relaxed);
        let latency_max = self.latency_max_us.load(Ordering::Relaxed);
        let latency_min = self.latency_min_us.load(Ordering::Relaxed);

        MetricsSnapshot {
            requests_total: total,
            cache_hits: hits,
            cache_misses: misses,
            miss_not_in_cache: self.miss_not_in_cache.load(Ordering::Relaxed),
            miss_expired: self.miss_expired.load(Ordering::Relaxed),
            miss_upstream_error: self.miss_upstream_error.load(Ordering::Relaxed),
            miss_circuit_open: self.miss_circuit_open.load(Ordering::Relaxed),
            cache_stale: self.cache_stale.load(Ordering::Relaxed),
            cache_frozen: self.cache_frozen.load(Ordering::Relaxed),
            cache_hit_rate: if total > 0 {
                hits as f64 / total as f64
            } else {
                0.0
            },
            coalesced_requests: self.coalesced_requests.load(Ordering::Relaxed),
            upstream_requests: self.upstream_requests.load(Ordering::Relaxed),
            upstream_failures: self.upstream_failures.load(Ordering::Relaxed),
            upstream_error_rate: {
                let upstream = self.upstream_requests.load(Ordering::Relaxed);
                let failures = self.upstream_failures.load(Ordering::Relaxed);
                if upstream > 0 {
                    failures as f64 / upstream as f64
                } else {
                    0.0
                }
            },
            validation_errors: self.validation_errors.load(Ordering::Relaxed),
            latency_avg_ms: (latency_sum.checked_div(total).unwrap_or(0)) as f64 / 1000.0,
            latency_max_ms: latency_max as f64 / 1000.0,
            latency_min_ms: if latency_min == u64::MAX {
                0.0
            } else {
                latency_min as f64 / 1000.0
            },
            refresh_operations: self.refresh_operations.load(Ordering::Relaxed),
            evictions: self.evictions.load(Ordering::Relaxed),
            drift_alerts: self.drift_alerts.load(Ordering::Relaxed),
            drift_none: self.drift_none.load(Ordering::Relaxed),
            drift_minor: self.drift_minor.load(Ordering::Relaxed),
            drift_moderate: self.drift_moderate.load(Ordering::Relaxed),
            drift_significant: self.drift_significant.load(Ordering::Relaxed),
            drift_major: self.drift_major.load(Ordering::Relaxed),
            integrity_failures: self.integrity_failures.load(Ordering::Relaxed),
            schema_changes: self.schema_changes.load(Ordering::Relaxed),
            cascade_success: self.cascade_success.load(Ordering::Relaxed),
            cascade_exhausted: self.cascade_exhausted.load(Ordering::Relaxed),
            cascade_timeout: self.cascade_timeout.load(Ordering::Relaxed),
            cascade_avg_depth: {
                let count = self.cascade_depth_count.load(Ordering::Relaxed);
                if count > 0 {
                    self.cascade_depth_sum.load(Ordering::Relaxed) as f64 / count as f64
                } else {
                    0.0
                }
            },
            warmup_requests: self.warmup_requests.load(Ordering::Relaxed),
            warmup_entries: self.warmup_entries.load(Ordering::Relaxed),
            #[cfg(feature = "embed-api")]
            semantic_hits: self.semantic_hits.load(Ordering::Relaxed),
            #[cfg(feature = "embed-api")]
            semantic_misses: self.semantic_misses.load(Ordering::Relaxed),
        }
    }

    /// Reset all metrics.
    pub fn reset(&self) {
        self.requests_total.store(0, Ordering::Relaxed);
        self.cache_hits.store(0, Ordering::Relaxed);
        self.cache_misses.store(0, Ordering::Relaxed);
        self.miss_not_in_cache.store(0, Ordering::Relaxed);
        self.miss_expired.store(0, Ordering::Relaxed);
        self.miss_upstream_error.store(0, Ordering::Relaxed);
        self.miss_circuit_open.store(0, Ordering::Relaxed);
        self.cache_stale.store(0, Ordering::Relaxed);
        self.cache_frozen.store(0, Ordering::Relaxed);
        self.coalesced_requests.store(0, Ordering::Relaxed);
        self.upstream_requests.store(0, Ordering::Relaxed);
        self.upstream_failures.store(0, Ordering::Relaxed);
        self.validation_errors.store(0, Ordering::Relaxed);
        self.latency_sum_us.store(0, Ordering::Relaxed);
        self.latency_max_us.store(0, Ordering::Relaxed);
        self.latency_min_us.store(u64::MAX, Ordering::Relaxed);
        self.refresh_operations.store(0, Ordering::Relaxed);
        self.evictions.store(0, Ordering::Relaxed);
        self.drift_alerts.store(0, Ordering::Relaxed);
        self.drift_none.store(0, Ordering::Relaxed);
        self.drift_minor.store(0, Ordering::Relaxed);
        self.drift_moderate.store(0, Ordering::Relaxed);
        self.drift_significant.store(0, Ordering::Relaxed);
        self.drift_major.store(0, Ordering::Relaxed);
        self.integrity_failures.store(0, Ordering::Relaxed);
        self.schema_changes.store(0, Ordering::Relaxed);
        self.cascade_success.store(0, Ordering::Relaxed);
        self.cascade_exhausted.store(0, Ordering::Relaxed);
        self.cascade_timeout.store(0, Ordering::Relaxed);
        self.cascade_depth_sum.store(0, Ordering::Relaxed);
        self.cascade_depth_count.store(0, Ordering::Relaxed);
        self.warmup_requests.store(0, Ordering::Relaxed);
        self.warmup_entries.store(0, Ordering::Relaxed);
        #[cfg(feature = "embed-api")]
        {
            self.semantic_hits.store(0, Ordering::Relaxed);
            self.semantic_misses.store(0, Ordering::Relaxed);
        }
        for slot in self.latency_ring.iter() {
            slot.store(0, Ordering::Relaxed);
        }
        self.latency_ring_pos.store(0, Ordering::Relaxed);
    }

    /// Get latency percentiles from the ring buffer.
    ///
    /// Returns P50, P95, P99 computed from recent latency samples across all requests
    /// (cache hits and misses). Lock-free: reads atomic slots without any locks.
    pub fn latency_percentiles(&self) -> LatencyPercentiles {
        let total_written = self.latency_ring_pos.load(Ordering::Relaxed);
        if total_written == 0 {
            return LatencyPercentiles {
                sample_count: 0,
                p50_ms: 0.0,
                p95_ms: 0.0,
                p99_ms: 0.0,
            };
        }

        let len = (total_written as usize).min(LATENCY_RING_SIZE);
        let mut sorted: Vec<u64> = Vec::with_capacity(len);
        sorted.extend(
            (0..len).filter_map(|i| self.latency_ring.get(i).map(|a| a.load(Ordering::Relaxed))),
        );
        sorted.sort_unstable();

        let len_f64 = len as f64;
        let p50_idx = (len_f64 * 0.50).ceil() as usize;
        let p50_idx = p50_idx.saturating_sub(1);
        let p95_idx = (len_f64 * 0.95).ceil() as usize;
        let p95_idx = p95_idx.saturating_sub(1);
        let p99_idx = (len_f64 * 0.99).ceil() as usize;
        let p99_idx = p99_idx.saturating_sub(1);
        let last_idx = len.saturating_sub(1);

        let p50_val = sorted.get(p50_idx.min(last_idx)).copied().unwrap_or(0);
        let p95_val = sorted.get(p95_idx.min(last_idx)).copied().unwrap_or(0);
        let p99_val = sorted.get(p99_idx.min(last_idx)).copied().unwrap_or(0);

        LatencyPercentiles {
            sample_count: total_written,
            p50_ms: p50_val as f64 / 1000.0,
            p95_ms: p95_val as f64 / 1000.0,
            p99_ms: p99_val as f64 / 1000.0,
        }
    }
}

/// Latency percentiles computed from the ring buffer.
#[derive(Debug, Clone, serde::Serialize)]
pub struct LatencyPercentiles {
    /// Total number of latency samples recorded (may exceed ring buffer size).
    pub sample_count: u64,
    /// 50th percentile latency in milliseconds.
    pub p50_ms: f64,
    /// 95th percentile latency in milliseconds.
    pub p95_ms: f64,
    /// 99th percentile latency in milliseconds.
    pub p99_ms: f64,
}

/// Point-in-time snapshot of metrics.
#[derive(Debug, Clone, serde::Serialize)]
pub struct MetricsSnapshot {
    /// Total requests processed.
    pub requests_total: u64,
    /// Cache hits.
    pub cache_hits: u64,
    /// Cache misses.
    pub cache_misses: u64,
    /// Cache misses: entry not in cache (cold miss).
    pub miss_not_in_cache: u64,
    /// Cache misses: entry expired.
    pub miss_expired: u64,
    /// Cache misses: upstream error.
    pub miss_upstream_error: u64,
    /// Cache misses: circuit breaker open.
    pub miss_circuit_open: u64,
    /// Stale-while-revalidate responses.
    pub cache_stale: u64,
    /// Frozen responses.
    pub cache_frozen: u64,
    /// Cache hit rate (0.0 to 1.0).
    pub cache_hit_rate: f64,
    /// Coalesced requests.
    pub coalesced_requests: u64,
    /// Upstream requests made.
    pub upstream_requests: u64,
    /// Upstream failures.
    pub upstream_failures: u64,
    /// Upstream error rate (0.0 to 1.0).
    pub upstream_error_rate: f64,
    /// Validation errors.
    pub validation_errors: u64,
    /// Average latency in milliseconds.
    pub latency_avg_ms: f64,
    /// Maximum latency in milliseconds.
    pub latency_max_ms: f64,
    /// Minimum latency in milliseconds.
    pub latency_min_ms: f64,
    /// Total refresh operations.
    pub refresh_operations: u64,
    /// Total evictions.
    pub evictions: u64,
    /// Drift alerts triggered.
    pub drift_alerts: u64,
    /// Drift observations with no change.
    pub drift_none: u64,
    /// Minor drift observations.
    pub drift_minor: u64,
    /// Moderate drift observations.
    pub drift_moderate: u64,
    /// Significant drift observations.
    pub drift_significant: u64,
    /// Major drift observations.
    pub drift_major: u64,
    /// Integrity check failures.
    pub integrity_failures: u64,
    /// Schema changes detected.
    pub schema_changes: u64,
    /// Successful cascade queries.
    pub cascade_success: u64,
    /// Cascade queries that exhausted all upstreams.
    pub cascade_exhausted: u64,
    /// Cascade queries that timed out.
    pub cascade_timeout: u64,
    /// Average cascade depth.
    pub cascade_avg_depth: f64,
    /// Number of warmup requests.
    pub warmup_requests: u64,
    /// Number of warmup entries cached.
    pub warmup_entries: u64,
    /// Number of semantic cache hits.
    #[cfg(feature = "embed-api")]
    pub semantic_hits: u64,
    /// Number of semantic cache misses.
    #[cfg(feature = "embed-api")]
    pub semantic_misses: u64,
}

impl MetricsSnapshot {
    /// Format metrics in Prometheus/OpenMetrics text format.
    ///
    /// This produces metrics compatible with Prometheus scraping.
    /// Example output:
    /// ```text
    /// # HELP conproxy_requests_total Total number of requests processed.
    /// # TYPE conproxy_requests_total counter
    /// conproxy_requests_total 12345
    /// ```
    pub fn to_prometheus(&self) -> String {
        let mut out = String::with_capacity(4096);

        // Helper macro for counter metrics (uses writeln! to avoid format!() allocation)
        macro_rules! counter {
            ($name:expr, $help:expr, $value:expr) => {
                let _ = writeln!(
                    out,
                    "# HELP conproxy_{} {}\n# TYPE conproxy_{} counter\nconproxy_{} {}",
                    $name, $help, $name, $name, $value
                );
            };
        }

        // Helper macro for gauge metrics (uses writeln! to avoid format!() allocation)
        macro_rules! gauge {
            ($name:expr, $help:expr, $value:expr) => {
                let _ = writeln!(
                    out,
                    "# HELP conproxy_{} {}\n# TYPE conproxy_{} gauge\nconproxy_{} {}",
                    $name, $help, $name, $name, $value
                );
            };
        }

        // Request counters
        counter!(
            "requests_total",
            "Total number of requests processed",
            self.requests_total
        );
        counter!(
            "cache_hits_total",
            "Number of cache hits (fresh responses)",
            self.cache_hits
        );
        counter!(
            "cache_misses_total",
            "Number of cache misses",
            self.cache_misses
        );

        // Miss reasons by label (follows drift_observations_total pattern)
        out.push_str("# HELP conproxy_cache_miss_reasons_total Number of cache misses by reason\n");
        out.push_str("# TYPE conproxy_cache_miss_reasons_total counter\n");
        let _ = writeln!(
            out,
            "conproxy_cache_miss_reasons_total{{reason=\"not_in_cache\"}} {}",
            self.miss_not_in_cache
        );
        let _ = writeln!(
            out,
            "conproxy_cache_miss_reasons_total{{reason=\"expired\"}} {}",
            self.miss_expired
        );
        let _ = writeln!(
            out,
            "conproxy_cache_miss_reasons_total{{reason=\"upstream_error\"}} {}",
            self.miss_upstream_error
        );
        let _ = writeln!(
            out,
            "conproxy_cache_miss_reasons_total{{reason=\"circuit_open\"}} {}",
            self.miss_circuit_open
        );

        counter!(
            "cache_stale_total",
            "Number of stale-while-revalidate responses",
            self.cache_stale
        );
        counter!(
            "cache_frozen_total",
            "Number of frozen responses (upstream unavailable)",
            self.cache_frozen
        );
        counter!(
            "coalesced_requests_total",
            "Number of coalesced requests (waited on leader)",
            self.coalesced_requests
        );

        // Upstream counters
        counter!(
            "upstream_requests_total",
            "Number of upstream requests made",
            self.upstream_requests
        );
        counter!(
            "upstream_failures_total",
            "Number of upstream request failures",
            self.upstream_failures
        );

        // Validation and error counters
        counter!(
            "validation_errors_total",
            "Number of validation errors (bad requests)",
            self.validation_errors
        );
        counter!(
            "integrity_failures_total",
            "Number of integrity check failures",
            self.integrity_failures
        );
        counter!(
            "schema_changes_total",
            "Number of schema changes detected",
            self.schema_changes
        );

        // Operations counters
        counter!(
            "refresh_operations_total",
            "Number of refresh operations",
            self.refresh_operations
        );
        counter!(
            "evictions_total",
            "Number of cache evictions",
            self.evictions
        );

        // Drift counters
        counter!(
            "drift_alerts_total",
            "Number of drift alerts triggered",
            self.drift_alerts
        );

        // Drift by level using labels
        out.push_str(
            "# HELP conproxy_drift_observations_total Number of drift observations by level\n",
        );
        out.push_str("# TYPE conproxy_drift_observations_total counter\n");
        let _ = writeln!(
            out,
            "conproxy_drift_observations_total{{level=\"none\"}} {}",
            self.drift_none
        );
        let _ = writeln!(
            out,
            "conproxy_drift_observations_total{{level=\"minor\"}} {}",
            self.drift_minor
        );
        let _ = writeln!(
            out,
            "conproxy_drift_observations_total{{level=\"moderate\"}} {}",
            self.drift_moderate
        );
        let _ = writeln!(
            out,
            "conproxy_drift_observations_total{{level=\"significant\"}} {}",
            self.drift_significant
        );
        let _ = writeln!(
            out,
            "conproxy_drift_observations_total{{level=\"major\"}} {}",
            self.drift_major
        );

        // Cascade counters
        counter!(
            "cascade_success_total",
            "Number of successful cascade queries",
            self.cascade_success
        );
        counter!(
            "cascade_exhausted_total",
            "Number of cascade queries that exhausted all upstreams",
            self.cascade_exhausted
        );
        counter!(
            "cascade_timeout_total",
            "Number of cascade queries that timed out",
            self.cascade_timeout
        );
        gauge!(
            "cascade_avg_depth",
            "Average cascade depth (upstreams tried per query)",
            self.cascade_avg_depth
        );

        // Warmup counters
        counter!(
            "warmup_requests_total",
            "Number of warmup requests (POST /cache/warmup calls)",
            self.warmup_requests
        );
        counter!(
            "warmup_entries_total",
            "Number of entries warmed (cache inserts from warmup)",
            self.warmup_entries
        );

        // Semantic cache counters
        #[cfg(feature = "embed-api")]
        {
            counter!(
                "semantic_hits_total",
                "Number of semantic cache hits (cosine similarity above threshold)",
                self.semantic_hits
            );
            counter!(
                "semantic_misses_total",
                "Number of semantic cache misses (no similar query above threshold)",
                self.semantic_misses
            );
        }

        // Rate gauges
        gauge!(
            "cache_hit_rate",
            "Cache hit rate (0.0 to 1.0)",
            self.cache_hit_rate
        );
        gauge!(
            "upstream_error_rate",
            "Upstream error rate (0.0 to 1.0)",
            self.upstream_error_rate
        );

        // Latency gauges (in milliseconds, but Prometheus convention is seconds)
        gauge!(
            "latency_avg_seconds",
            "Average request latency in seconds",
            self.latency_avg_ms / 1000.0
        );
        gauge!(
            "latency_max_seconds",
            "Maximum request latency in seconds",
            self.latency_max_ms / 1000.0
        );
        gauge!(
            "latency_min_seconds",
            "Minimum request latency in seconds",
            self.latency_min_ms / 1000.0
        );

        out
    }

    /// Format metrics in Prometheus text format with optional cache info.
    ///
    /// # Arguments
    /// * `cache_entries` - Current number of entries in cache
    /// * `cache_memory_bytes` - Current memory usage of cache in bytes
    /// * `cache_max_entries` - Maximum allowed cache entries
    pub fn to_prometheus_with_cache(
        &self,
        cache_entries: u64,
        cache_memory_bytes: u64,
        cache_max_entries: u64,
    ) -> String {
        let mut out = self.to_prometheus();

        // Cache gauge metrics
        let _ = writeln!(
            out,
            "# HELP conproxy_cache_entries Current number of entries in cache\n\
             # TYPE conproxy_cache_entries gauge\n\
             conproxy_cache_entries {}",
            cache_entries
        );

        let _ = writeln!(
            out,
            "# HELP conproxy_cache_memory_bytes Current memory usage of cache in bytes\n\
             # TYPE conproxy_cache_memory_bytes gauge\n\
             conproxy_cache_memory_bytes {}",
            cache_memory_bytes
        );

        let _ = writeln!(
            out,
            "# HELP conproxy_cache_max_entries Maximum allowed cache entries\n\
             # TYPE conproxy_cache_max_entries gauge\n\
             conproxy_cache_max_entries {}",
            cache_max_entries
        );

        let utilization = if cache_max_entries > 0 {
            cache_entries as f64 / cache_max_entries as f64
        } else {
            0.0
        };
        let _ = writeln!(
            out,
            "# HELP conproxy_cache_utilization Cache utilization (0.0 to 1.0)\n\
             # TYPE conproxy_cache_utilization gauge\n\
             conproxy_cache_utilization {}",
            utilization
        );

        out
    }

    /// Format metrics in Prometheus text format with pool statistics.
    ///
    /// # Arguments
    /// * `cache_entries` - Current number of entries in cache
    /// * `cache_memory_bytes` - Current memory usage of cache in bytes
    /// * `cache_max_entries` - Maximum allowed cache entries
    /// * `pool_params` - Pool statistics including upstream type counts
    pub fn to_prometheus_with_pool(
        &self,
        cache_entries: u64,
        cache_memory_bytes: u64,
        cache_max_entries: u64,
        pool_params: &PoolMetricsParams,
    ) -> String {
        let mut out =
            self.to_prometheus_with_cache(cache_entries, cache_memory_bytes, cache_max_entries);

        // Pool gauge metrics
        let _ = writeln!(
            out,
            "# HELP conproxy_pool_upstreams_total Total number of configured upstreams\n\
             # TYPE conproxy_pool_upstreams_total gauge\n\
             conproxy_pool_upstreams_total {}",
            pool_params.total_upstreams
        );

        let _ = writeln!(
            out,
            "# HELP conproxy_pool_upstreams_healthy Number of healthy upstreams\n\
             # TYPE conproxy_pool_upstreams_healthy gauge\n\
             conproxy_pool_upstreams_healthy {}",
            pool_params.healthy_upstreams
        );

        // Upstream type counts with labels
        out.push_str(
            "# HELP conproxy_pool_upstreams_by_type Number of upstreams by backend type\n\
             # TYPE conproxy_pool_upstreams_by_type gauge\n",
        );
        let _ = writeln!(
            out,
            "conproxy_pool_upstreams_by_type{{type=\"fts\"}} {}",
            pool_params.fts_count
        );
        let _ = writeln!(
            out,
            "conproxy_pool_upstreams_by_type{{type=\"vector_db\"}} {}",
            pool_params.vector_db_count
        );
        let _ = writeln!(
            out,
            "conproxy_pool_upstreams_by_type{{type=\"hybrid\"}} {}",
            pool_params.hybrid_count
        );
        let _ = writeln!(
            out,
            "conproxy_pool_upstreams_by_type{{type=\"unknown\"}} {}",
            pool_params.unknown_count
        );

        // Connection pool metrics
        let _ = writeln!(
            out,
            "# HELP conproxy_pool_active_connections Number of active connections\n\
             # TYPE conproxy_pool_active_connections gauge\n\
             conproxy_pool_active_connections {}",
            pool_params.active_connections
        );

        let _ = writeln!(
            out,
            "# HELP conproxy_pool_utilization Connection pool utilization (0.0 to 1.0)\n\
             # TYPE conproxy_pool_utilization gauge\n\
             conproxy_pool_utilization {}",
            pool_params.pool_utilization
        );

        out
    }
}

/// Parameters for pool-related Prometheus metrics.
pub struct PoolMetricsParams {
    /// Total number of configured upstreams.
    pub total_upstreams: usize,
    /// Number of healthy upstreams.
    pub healthy_upstreams: usize,
    /// Number of full-text search upstreams.
    pub fts_count: usize,
    /// Number of vector database upstreams.
    pub vector_db_count: usize,
    /// Number of hybrid upstreams.
    pub hybrid_count: usize,
    /// Number of unknown-type upstreams.
    pub unknown_count: usize,
    /// Number of active connections.
    pub active_connections: usize,
    /// Connection pool utilization (0.0 to 1.0).
    pub pool_utilization: f64,
}

/// Per-context stats for Prometheus export.
pub struct ContextMetricsEntry {
    /// Context identifier.
    pub id: String,
    /// Cache hits for this context.
    pub hits: u64,
    /// Cache misses for this context.
    pub misses: u64,
    /// Hit rate (0.0 to 1.0).
    pub hit_rate: f64,
}

/// Per-agent stats for Prometheus export.
pub struct AgentMetricsEntry {
    /// Agent identifier.
    pub id: String,
    /// Total requests from this agent.
    pub requests_total: u64,
    /// Cache hits for this agent.
    pub cache_hits: u64,
    /// Cache misses for this agent.
    pub cache_misses: u64,
    /// Rate-limited requests for this agent.
    pub rate_limited: u64,
    /// Context-denied requests for this agent.
    pub context_denied: u64,
}

impl MetricsSnapshot {
    /// Append per-context cache metrics to a Prometheus output string.
    pub fn append_context_stats_prometheus(out: &mut String, contexts: &[ContextMetricsEntry]) {
        if contexts.is_empty() {
            return;
        }

        out.push_str(
            "# HELP conproxy_context_cache_hits_total Cache hits by context\n\
             # TYPE conproxy_context_cache_hits_total counter\n",
        );
        for ctx in contexts {
            let _ = writeln!(
                out,
                "conproxy_context_cache_hits_total{{context=\"{}\"}} {}",
                ctx.id, ctx.hits
            );
        }

        out.push_str(
            "# HELP conproxy_context_cache_misses_total Cache misses by context\n\
             # TYPE conproxy_context_cache_misses_total counter\n",
        );
        for ctx in contexts {
            let _ = writeln!(
                out,
                "conproxy_context_cache_misses_total{{context=\"{}\"}} {}",
                ctx.id, ctx.misses
            );
        }

        out.push_str(
            "# HELP conproxy_context_hit_rate Cache hit rate by context\n\
             # TYPE conproxy_context_hit_rate gauge\n",
        );
        for ctx in contexts {
            let _ = writeln!(
                out,
                "conproxy_context_hit_rate{{context=\"{}\"}} {}",
                ctx.id, ctx.hit_rate
            );
        }
    }

    /// Append per-agent metrics to an existing Prometheus output string.
    pub fn append_agent_metrics_prometheus(out: &mut String, agents: &[AgentMetricsEntry]) {
        if agents.is_empty() {
            return;
        }

        out.push_str(
            "# HELP conproxy_agent_requests_total Total requests per agent\n\
             # TYPE conproxy_agent_requests_total counter\n",
        );
        for agent in agents {
            let _ = writeln!(
                out,
                "conproxy_agent_requests_total{{agent=\"{}\"}} {}",
                agent.id, agent.requests_total
            );
        }

        out.push_str(
            "# HELP conproxy_agent_cache_hits_total Cache hits per agent\n\
             # TYPE conproxy_agent_cache_hits_total counter\n",
        );
        for agent in agents {
            let _ = writeln!(
                out,
                "conproxy_agent_cache_hits_total{{agent=\"{}\"}} {}",
                agent.id, agent.cache_hits
            );
        }

        out.push_str(
            "# HELP conproxy_agent_cache_misses_total Cache misses per agent\n\
             # TYPE conproxy_agent_cache_misses_total counter\n",
        );
        for agent in agents {
            let _ = writeln!(
                out,
                "conproxy_agent_cache_misses_total{{agent=\"{}\"}} {}",
                agent.id, agent.cache_misses
            );
        }

        out.push_str(
            "# HELP conproxy_agent_rate_limited_total Rate limited requests per agent\n\
             # TYPE conproxy_agent_rate_limited_total counter\n",
        );
        for agent in agents {
            let _ = writeln!(
                out,
                "conproxy_agent_rate_limited_total{{agent=\"{}\"}} {}",
                agent.id, agent.rate_limited
            );
        }

        out.push_str(
            "# HELP conproxy_agent_context_denied_total Context denied requests per agent\n\
             # TYPE conproxy_agent_context_denied_total counter\n",
        );
        for agent in agents {
            let _ = writeln!(
                out,
                "conproxy_agent_context_denied_total{{agent=\"{}\"}} {}",
                agent.id, agent.context_denied
            );
        }
    }

    /// Append adaptive timeout percentiles to an existing Prometheus output string.
    pub fn append_adaptive_timeout_prometheus(
        out: &mut String,
        stats: &super::adaptive::AdaptiveTimeoutStats,
    ) {
        out.push_str(
            "# HELP conproxy_adaptive_timeout_seconds Current adaptive timeout in seconds\n\
             # TYPE conproxy_adaptive_timeout_seconds gauge\n",
        );
        let _ = writeln!(
            out,
            "conproxy_adaptive_timeout_seconds {}",
            stats.current_timeout_ms / 1000.0
        );

        out.push_str(
            "# HELP conproxy_adaptive_timeout_samples Number of latency samples in the adaptive timeout ring buffer\n\
             # TYPE conproxy_adaptive_timeout_samples gauge\n",
        );
        let _ = writeln!(
            out,
            "conproxy_adaptive_timeout_samples {}",
            stats.sample_count
        );

        if stats.sample_count > 0 {
            out.push_str(
                "# HELP conproxy_upstream_latency_seconds Upstream latency percentiles from adaptive timeout\n\
                 # TYPE conproxy_upstream_latency_seconds gauge\n",
            );
            let _ = writeln!(
                out,
                "conproxy_upstream_latency_seconds{{quantile=\"0.5\"}} {}",
                stats.p50_latency_ms / 1000.0
            );
            let _ = writeln!(
                out,
                "conproxy_upstream_latency_seconds{{quantile=\"0.95\"}} {}",
                stats.p95_latency_ms / 1000.0
            );
            let _ = writeln!(
                out,
                "conproxy_upstream_latency_seconds{{quantile=\"0.99\"}} {}",
                stats.p99_latency_ms / 1000.0
            );
            let _ = writeln!(
                out,
                "conproxy_upstream_latency_seconds{{quantile=\"min\"}} {}",
                stats.min_latency_ms / 1000.0
            );
            let _ = writeln!(
                out,
                "conproxy_upstream_latency_seconds{{quantile=\"max\"}} {}",
                stats.max_latency_ms / 1000.0
            );
            let _ = writeln!(
                out,
                "conproxy_upstream_latency_seconds{{quantile=\"avg\"}} {}",
                stats.avg_latency_ms / 1000.0
            );
        }
    }

    /// Append circuit breaker metrics to an existing Prometheus output string.
    pub fn append_circuit_breaker_prometheus(
        out: &mut String,
        state_value: u64,
        failure_count: u32,
        times_opened: u64,
        times_tripped: u64,
    ) {
        out.push_str(
            "# HELP conproxy_circuit_state Circuit breaker state (0=closed, 1=half_open, 2=open)\n\
             # TYPE conproxy_circuit_state gauge\n",
        );
        let _ = writeln!(out, "conproxy_circuit_state {}", state_value);

        out.push_str(
            "# HELP conproxy_circuit_failure_count Current failure count in the circuit breaker window\n\
             # TYPE conproxy_circuit_failure_count gauge\n",
        );
        let _ = writeln!(out, "conproxy_circuit_failure_count {}", failure_count);

        out.push_str(
            "# HELP conproxy_circuit_times_opened_total Total times the circuit breaker has opened\n\
             # TYPE conproxy_circuit_times_opened_total counter\n",
        );
        let _ = writeln!(out, "conproxy_circuit_times_opened_total {}", times_opened);

        out.push_str(
            "# HELP conproxy_circuit_times_tripped_total Total requests rejected by open circuit\n\
             # TYPE conproxy_circuit_times_tripped_total counter\n",
        );
        let _ = writeln!(
            out,
            "conproxy_circuit_times_tripped_total {}",
            times_tripped
        );
    }

    /// Append per-upstream metrics to an existing Prometheus output string.
    pub fn append_per_upstream_prometheus(
        out: &mut String,
        upstreams: &[(String, u64, u64, f64, String)], // (id, requests, failures, failure_rate, status)
    ) {
        if upstreams.is_empty() {
            return;
        }

        out.push_str(
            "# HELP conproxy_upstream_requests_by_upstream Total requests per upstream\n\
             # TYPE conproxy_upstream_requests_by_upstream counter\n",
        );
        for (id, requests, _, _, _) in upstreams {
            let _ = writeln!(
                out,
                "conproxy_upstream_requests_by_upstream{{upstream=\"{}\"}} {}",
                id, requests
            );
        }

        out.push_str(
            "# HELP conproxy_upstream_failures_by_upstream Total failures per upstream\n\
             # TYPE conproxy_upstream_failures_by_upstream counter\n",
        );
        for (id, _, failures, _, _) in upstreams {
            let _ = writeln!(
                out,
                "conproxy_upstream_failures_by_upstream{{upstream=\"{}\"}} {}",
                id, failures
            );
        }

        out.push_str(
            "# HELP conproxy_upstream_failure_rate_by_upstream Failure rate per upstream (0.0 to 1.0)\n\
             # TYPE conproxy_upstream_failure_rate_by_upstream gauge\n",
        );
        for (id, _, _, rate, _) in upstreams {
            let _ = writeln!(
                out,
                "conproxy_upstream_failure_rate_by_upstream{{upstream=\"{}\"}} {}",
                id, rate
            );
        }

        out.push_str(
            "# HELP conproxy_upstream_status Per-upstream health status (0=offline, 1=degraded, 2=online)\n\
             # TYPE conproxy_upstream_status gauge\n",
        );
        for (id, _, _, _, status) in upstreams {
            let value = match status.as_str() {
                "online" => 2,
                "degraded" => 1,
                _ => 0,
            };
            let _ = writeln!(
                out,
                "conproxy_upstream_status{{upstream=\"{}\"}} {}",
                id, value
            );
        }
    }

    /// Append request throughput gauge to an existing Prometheus output string.
    pub fn append_throughput_prometheus(out: &mut String, uptime_secs: u64, requests_total: u64) {
        let rps = if uptime_secs > 0 {
            requests_total as f64 / uptime_secs as f64
        } else {
            0.0
        };

        out.push_str(
            "# HELP conproxy_requests_per_second Average request throughput (requests/second)\n\
             # TYPE conproxy_requests_per_second gauge\n",
        );
        let _ = writeln!(out, "conproxy_requests_per_second {:.2}", rps);
    }

    /// Append all-request latency percentiles to an existing Prometheus output string.
    pub fn append_latency_percentiles_prometheus(
        out: &mut String,
        percentiles: &LatencyPercentiles,
    ) {
        if percentiles.sample_count == 0 {
            return;
        }

        out.push_str(
            "# HELP conproxy_request_latency_seconds All-request latency percentiles (cache hits + misses)\n\
             # TYPE conproxy_request_latency_seconds gauge\n",
        );
        let _ = writeln!(
            out,
            "conproxy_request_latency_seconds{{quantile=\"0.5\"}} {}",
            percentiles.p50_ms / 1000.0
        );
        let _ = writeln!(
            out,
            "conproxy_request_latency_seconds{{quantile=\"0.95\"}} {}",
            percentiles.p95_ms / 1000.0
        );
        let _ = writeln!(
            out,
            "conproxy_request_latency_seconds{{quantile=\"0.99\"}} {}",
            percentiles.p99_ms / 1000.0
        );
    }

    /// Append request queue metrics to Prometheus output.
    pub fn append_queue_prometheus(out: &mut String, stats: &crate::proxy::priority::QueueStats) {
        let _ = writeln!(
            out,
            "# HELP conproxy_queue_depth Total items in request queue\n\
             # TYPE conproxy_queue_depth gauge\n\
             conproxy_queue_depth {}",
            stats.total
        );
        let _ = writeln!(
            out,
            "# HELP conproxy_queue_max_size Maximum request queue capacity\n\
             # TYPE conproxy_queue_max_size gauge\n\
             conproxy_queue_max_size {}",
            stats.max_size
        );
        out.push_str(
            "# HELP conproxy_queue_by_priority Queue depth by priority level\n\
             # TYPE conproxy_queue_by_priority gauge\n",
        );
        let _ = writeln!(
            out,
            "conproxy_queue_by_priority{{priority=\"low\"}} {}",
            stats.low_priority
        );
        let _ = writeln!(
            out,
            "conproxy_queue_by_priority{{priority=\"normal\"}} {}",
            stats.normal_priority
        );
        let _ = writeln!(
            out,
            "conproxy_queue_by_priority{{priority=\"high\"}} {}",
            stats.high_priority
        );
        let _ = writeln!(
            out,
            "conproxy_queue_by_priority{{priority=\"critical\"}} {}",
            stats.critical_priority
        );
    }

    /// Append client tracker metrics to Prometheus output.
    pub fn append_client_tracker_prometheus(
        out: &mut String,
        active: usize,
        completed: u64,
        rejected: u64,
    ) {
        let _ = writeln!(
            out,
            "# HELP conproxy_clients_active Number of active client requests\n\
             # TYPE conproxy_clients_active gauge\n\
             conproxy_clients_active {}",
            active
        );
        let _ = writeln!(
            out,
            "# HELP conproxy_clients_completed_total Total completed client requests\n\
             # TYPE conproxy_clients_completed_total counter\n\
             conproxy_clients_completed_total {}",
            completed
        );
        let _ = writeln!(
            out,
            "# HELP conproxy_clients_rejected_total Total rejected client requests\n\
             # TYPE conproxy_clients_rejected_total counter\n\
             conproxy_clients_rejected_total {}",
            rejected
        );
    }

    /// Append SmartEmbedder cache stats (feature `embed-api`).
    #[cfg(feature = "embed-api")]
    pub fn append_smart_embedder_prometheus(
        out: &mut String,
        stats: &crate::proxy::smart_embedder::SmartEmbedderStats,
    ) {
        let _ = writeln!(
            out,
            "# HELP conproxy_smart_embedder_cache_size Current embedding cache entries\n\
             # TYPE conproxy_smart_embedder_cache_size gauge\n\
             conproxy_smart_embedder_cache_size{{model=\"{}\"}} {}",
            stats.model_name, stats.cache_size
        );
        let _ = writeln!(
            out,
            "# HELP conproxy_smart_embedder_cache_hits_total Embedding cache hits\n\
             # TYPE conproxy_smart_embedder_cache_hits_total counter\n\
             conproxy_smart_embedder_cache_hits_total{{model=\"{}\"}} {}",
            stats.model_name, stats.cache_hits
        );
        let _ = writeln!(
            out,
            "# HELP conproxy_smart_embedder_cache_misses_total Embedding cache misses\n\
             # TYPE conproxy_smart_embedder_cache_misses_total counter\n\
             conproxy_smart_embedder_cache_misses_total{{model=\"{}\"}} {}",
            stats.model_name, stats.cache_misses
        );
        let _ = writeln!(
            out,
            "# HELP conproxy_smart_embedder_embeddings_computed_total Embeddings computed\n\
             # TYPE conproxy_smart_embedder_embeddings_computed_total counter\n\
             conproxy_smart_embedder_embeddings_computed_total{{model=\"{}\"}} {}",
            stats.model_name, stats.embeddings_computed
        );
        let _ = writeln!(
            out,
            "# HELP conproxy_smart_embedder_coalesced_total Coalesced embed requests\n\
             # TYPE conproxy_smart_embedder_coalesced_total counter\n\
             conproxy_smart_embedder_coalesced_total{{model=\"{}\"}} {}",
            stats.model_name, stats.coalesced_requests
        );
    }
}

/// Timer guard that records latency on drop.
pub struct LatencyTimer<'a> {
    metrics: &'a ProxyMetrics,
    start: Instant,
}

impl<'a> LatencyTimer<'a> {
    /// Create a new latency timer.
    pub fn new(metrics: &'a ProxyMetrics) -> Self {
        Self {
            metrics,
            start: Instant::now(),
        }
    }
}

impl Drop for LatencyTimer<'_> {
    fn drop(&mut self) {
        self.metrics.record_latency(self.start.elapsed());
    }
}

#[cfg(test)]
#[path = "tests/metrics_tests.rs"]
mod tests;