lnc-metrics 0.2.2

Non-blocking telemetry and metrics for LANCE
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
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]

pub mod golden_signals;
pub mod otlp;
mod tracing_export;

use std::net::SocketAddr;
use std::sync::atomic::{AtomicU64, Ordering};

pub use golden_signals::{
    ERRORS_BY_TYPE,
    ERRORS_TOTAL,
    // Errors
    ErrorType,
    // Snapshot
    GoldenSignalsSnapshot,
    LATENCY_FETCH,
    LATENCY_INGEST,
    LATENCY_IO,
    LATENCY_NETWORK,
    LATENCY_REPLICATION,
    LATENCY_SAMPLE_RATE,
    // Latency
    LatencyHistogram,
    LatencySnapshot,
    LatencyTimer,
    RATE_INGEST_BYTES,
    RATE_INGEST_OPS,
    RATE_READ_BYTES,
    RATE_READ_OPS,
    // Traffic
    RateTracker,
    SATURATION_BUFFER_POOL_TOTAL,
    SATURATION_BUFFER_POOL_USED,
    SATURATION_CONNECTIONS_MAX,
    SATURATION_CONNECTIONS_USED,
    SATURATION_MEMORY_TOTAL,
    SATURATION_MEMORY_USED,
    SATURATION_PENDING_IO,
    SATURATION_QUEUE_CAPACITY,
    SATURATION_QUEUE_DEPTH,
    // Latency - SAMPLED (use these on hot paths)
    SampledTimer,
    buffer_pool_saturation,
    get_error_count,
    get_total_errors,
    memory_saturation,
    queue_saturation,
    record_error,
    record_fetch_latency,
    record_fetch_latency_sampled,
    record_ingest_latency,
    record_ingest_latency_sampled,
    record_io_latency,
    record_io_latency_sampled,
    record_network_latency,
    record_replication_latency,
    saturation_ratio,
    set_buffer_pool_usage,
    set_connection_usage,
    set_latency_sample_rate,
    set_memory_usage,
    set_pending_io,
    // Saturation
    set_queue_depth,
    should_sample,
    time_fetch,
    time_fetch_sampled,
    time_ingest,
    time_ingest_sampled,
    time_io,
    time_io_sampled,
    update_rates,
};
pub use tracing_export::{LocalSpan, OperationType, SpanContext, TracingConfig, init_tracing};

pub static RECORDS_INGESTED: AtomicU64 = AtomicU64::new(0);
pub static BYTES_INGESTED: AtomicU64 = AtomicU64::new(0);
pub static BATCHES_WRITTEN: AtomicU64 = AtomicU64::new(0);
pub static BYTES_WRITTEN: AtomicU64 = AtomicU64::new(0);
pub static IO_OPS_SUBMITTED: AtomicU64 = AtomicU64::new(0);
pub static IO_OPS_COMPLETED: AtomicU64 = AtomicU64::new(0);
pub static CONNECTIONS_ACTIVE: AtomicU64 = AtomicU64::new(0);
pub static CONNECTIONS_TOTAL: AtomicU64 = AtomicU64::new(0);
pub static CRC_FAILURES: AtomicU64 = AtomicU64::new(0);
pub static BACKPRESSURE_EVENTS: AtomicU64 = AtomicU64::new(0);
pub static POOL_EXHAUSTED: AtomicU64 = AtomicU64::new(0);
pub static NUMA_MISALIGNED: AtomicU64 = AtomicU64::new(0);
pub static FOLLOWER_EVICTIONS: AtomicU64 = AtomicU64::new(0);
pub static FOLLOWER_RECOVERIES: AtomicU64 = AtomicU64::new(0);
pub static QUORUM_FAILURES: AtomicU64 = AtomicU64::new(0);
pub static QUORUM_TIMEOUTS: AtomicU64 = AtomicU64::new(0);

// HLC (Hybrid Logical Clock) metrics
pub static HLC_DRIFT_MS: AtomicU64 = AtomicU64::new(0);
pub static HLC_DRIFT_WARNINGS: AtomicU64 = AtomicU64::new(0);
pub static HLC_DRIFT_CRITICAL: AtomicU64 = AtomicU64::new(0);
pub static HLC_LOGICAL_EXHAUSTED: AtomicU64 = AtomicU64::new(0);

// Raft consensus metrics
pub static RAFT_ELECTIONS_STARTED: AtomicU64 = AtomicU64::new(0);
pub static RAFT_ELECTIONS_WON: AtomicU64 = AtomicU64::new(0);
pub static RAFT_PRE_VOTES_REJECTED: AtomicU64 = AtomicU64::new(0);
pub static RAFT_LEADER_STEPDOWNS: AtomicU64 = AtomicU64::new(0);
pub static RAFT_FENCING_REJECTIONS: AtomicU64 = AtomicU64::new(0);

// Consumer read path metrics (per Architecture §17.7)
pub static READS_TOTAL: AtomicU64 = AtomicU64::new(0);
pub static READ_BYTES_TOTAL: AtomicU64 = AtomicU64::new(0);
pub static CONSUMER_THROTTLED: AtomicU64 = AtomicU64::new(0);
pub static ZERO_COPY_SENDS: AtomicU64 = AtomicU64::new(0);

// Cluster health metrics (for production observability)
pub static CLUSTER_LEADER_ID: AtomicU64 = AtomicU64::new(0);
pub static CLUSTER_CURRENT_TERM: AtomicU64 = AtomicU64::new(0);
pub static CLUSTER_NODE_COUNT: AtomicU64 = AtomicU64::new(0);
pub static CLUSTER_HEALTHY_NODES: AtomicU64 = AtomicU64::new(0);
pub static CLUSTER_IS_LEADER: AtomicU64 = AtomicU64::new(0);
pub static CLUSTER_QUORUM_AVAILABLE: AtomicU64 = AtomicU64::new(0);

// Replication lag metrics (bytes behind leader, time since last sync)
pub static REPLICATION_LAG_BYTES: AtomicU64 = AtomicU64::new(0);
pub static REPLICATION_LAST_SYNC_MS: AtomicU64 = AtomicU64::new(0);
pub static REPLICATION_PENDING_OPS: AtomicU64 = AtomicU64::new(0);

#[inline]
pub fn increment_records_ingested(count: u64) {
    RECORDS_INGESTED.fetch_add(count, Ordering::Relaxed);
}

#[inline]
pub fn increment_bytes_ingested(bytes: u64) {
    BYTES_INGESTED.fetch_add(bytes, Ordering::Relaxed);
}

#[inline]
pub fn increment_batches_written() {
    BATCHES_WRITTEN.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub fn increment_bytes_written(bytes: u64) {
    BYTES_WRITTEN.fetch_add(bytes, Ordering::Relaxed);
}

#[inline]
pub fn increment_io_submitted() {
    IO_OPS_SUBMITTED.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub fn increment_io_completed() {
    IO_OPS_COMPLETED.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub fn increment_connections() {
    CONNECTIONS_ACTIVE.fetch_add(1, Ordering::Relaxed);
    CONNECTIONS_TOTAL.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub fn decrement_connections() {
    CONNECTIONS_ACTIVE.fetch_sub(1, Ordering::Relaxed);
}

#[inline]
pub fn increment_crc_failures() {
    CRC_FAILURES.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub fn increment_backpressure() {
    BACKPRESSURE_EVENTS.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub fn increment_pool_exhausted() {
    POOL_EXHAUSTED.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub fn increment_numa_misaligned() {
    NUMA_MISALIGNED.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub fn increment_follower_evictions() {
    FOLLOWER_EVICTIONS.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub fn increment_follower_recoveries() {
    FOLLOWER_RECOVERIES.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub fn increment_quorum_failures() {
    QUORUM_FAILURES.fetch_add(1, Ordering::Relaxed);
}

#[inline]
pub fn increment_quorum_timeouts() {
    QUORUM_TIMEOUTS.fetch_add(1, Ordering::Relaxed);
}

// HLC metrics functions

/// Update the current HLC drift in milliseconds.
#[inline]
pub fn set_hlc_drift_ms(drift_ms: u64) {
    HLC_DRIFT_MS.store(drift_ms, Ordering::Relaxed);
}

/// Increment HLC drift warning counter (drift > 100ms).
#[inline]
pub fn increment_hlc_drift_warnings() {
    HLC_DRIFT_WARNINGS.fetch_add(1, Ordering::Relaxed);
}

/// Increment HLC drift critical counter (drift > 1s).
#[inline]
pub fn increment_hlc_drift_critical() {
    HLC_DRIFT_CRITICAL.fetch_add(1, Ordering::Relaxed);
}

/// Increment HLC logical counter exhausted events.
#[inline]
pub fn increment_hlc_logical_exhausted() {
    HLC_LOGICAL_EXHAUSTED.fetch_add(1, Ordering::Relaxed);
}

// Raft metrics functions

/// Increment Raft elections started counter.
#[inline]
pub fn increment_raft_elections_started() {
    RAFT_ELECTIONS_STARTED.fetch_add(1, Ordering::Relaxed);
}

/// Increment Raft elections won counter.
#[inline]
pub fn increment_raft_elections_won() {
    RAFT_ELECTIONS_WON.fetch_add(1, Ordering::Relaxed);
}

/// Increment Raft pre-votes rejected counter.
#[inline]
pub fn increment_raft_pre_votes_rejected() {
    RAFT_PRE_VOTES_REJECTED.fetch_add(1, Ordering::Relaxed);
}

/// Increment Raft leader stepdown counter.
#[inline]
pub fn increment_raft_leader_stepdowns() {
    RAFT_LEADER_STEPDOWNS.fetch_add(1, Ordering::Relaxed);
}

/// Increment Raft fencing rejection counter.
#[inline]
pub fn increment_raft_fencing_rejections() {
    RAFT_FENCING_REJECTIONS.fetch_add(1, Ordering::Relaxed);
}

// Consumer read path functions

/// Increment total read operations counter.
#[inline]
pub fn increment_reads() {
    READS_TOTAL.fetch_add(1, Ordering::Relaxed);
}

/// Increment total bytes read counter.
#[inline]
pub fn increment_read_bytes(bytes: u64) {
    READ_BYTES_TOTAL.fetch_add(bytes, Ordering::Relaxed);
}

/// Increment consumer throttled events counter.
#[inline]
pub fn increment_consumer_throttled() {
    CONSUMER_THROTTLED.fetch_add(1, Ordering::Relaxed);
}

/// Increment zero-copy sends counter (per Architecture §17.7)
#[inline]
pub fn increment_zero_copy_sends() {
    ZERO_COPY_SENDS.fetch_add(1, Ordering::Relaxed);
}

// Cluster health metric functions

/// Set the current cluster leader ID.
#[inline]
pub fn set_cluster_leader_id(leader_id: u16) {
    CLUSTER_LEADER_ID.store(leader_id as u64, Ordering::Relaxed);
}

/// Set the current Raft term.
#[inline]
pub fn set_cluster_current_term(term: u64) {
    CLUSTER_CURRENT_TERM.store(term, Ordering::Relaxed);
}

/// Set the total number of nodes in the cluster.
#[inline]
pub fn set_cluster_node_count(count: usize) {
    CLUSTER_NODE_COUNT.store(count as u64, Ordering::Relaxed);
}

/// Set the number of healthy (connected) nodes.
#[inline]
pub fn set_cluster_healthy_nodes(count: usize) {
    CLUSTER_HEALTHY_NODES.store(count as u64, Ordering::Relaxed);
}

/// Set whether this node is the leader (1 = leader, 0 = follower).
#[inline]
pub fn set_cluster_is_leader(is_leader: bool) {
    CLUSTER_IS_LEADER.store(if is_leader { 1 } else { 0 }, Ordering::Relaxed);
}

/// Set whether quorum is available (1 = yes, 0 = no).
#[inline]
pub fn set_cluster_quorum_available(available: bool) {
    CLUSTER_QUORUM_AVAILABLE.store(if available { 1 } else { 0 }, Ordering::Relaxed);
}

// Replication lag metric functions

/// Set the replication lag in bytes (how far behind the leader).
#[inline]
pub fn set_replication_lag_bytes(bytes: u64) {
    REPLICATION_LAG_BYTES.store(bytes, Ordering::Relaxed);
}

/// Set the time since last successful sync in milliseconds.
#[inline]
pub fn set_replication_last_sync_ms(ms: u64) {
    REPLICATION_LAST_SYNC_MS.store(ms, Ordering::Relaxed);
}

/// Set the number of pending replication operations.
#[inline]
pub fn set_replication_pending_ops(count: u64) {
    REPLICATION_PENDING_OPS.store(count, Ordering::Relaxed);
}

/// Increment pending replication operations.
#[inline]
pub fn increment_replication_pending_ops() {
    REPLICATION_PENDING_OPS.fetch_add(1, Ordering::Relaxed);
}

/// Decrement pending replication operations.
#[inline]
pub fn decrement_replication_pending_ops() {
    REPLICATION_PENDING_OPS.fetch_sub(1, Ordering::Relaxed);
}

pub struct MetricsSnapshot {
    pub records_ingested: u64,
    pub bytes_ingested: u64,
    pub batches_written: u64,
    pub bytes_written: u64,
    pub io_ops_submitted: u64,
    pub io_ops_completed: u64,
    pub connections_active: u64,
    pub connections_total: u64,
    pub crc_failures: u64,
    pub backpressure_events: u64,
    pub pool_exhausted: u64,
    pub numa_misaligned: u64,
    pub follower_evictions: u64,
    pub follower_recoveries: u64,
    pub quorum_failures: u64,
    // HLC metrics
    pub hlc_drift_ms: u64,
    pub hlc_drift_warnings: u64,
    pub hlc_drift_critical: u64,
    pub hlc_logical_exhausted: u64,
    // Raft metrics
    pub raft_elections_started: u64,
    pub raft_elections_won: u64,
    pub raft_pre_votes_rejected: u64,
    pub raft_leader_stepdowns: u64,
    pub raft_fencing_rejections: u64,
    // Consumer read path metrics
    pub reads_total: u64,
    pub read_bytes_total: u64,
    pub consumer_throttled: u64,
    pub zero_copy_sends: u64,
    // Cluster health metrics
    pub cluster_leader_id: u64,
    pub cluster_current_term: u64,
    pub cluster_node_count: u64,
    pub cluster_healthy_nodes: u64,
    pub cluster_is_leader: u64,
    pub cluster_quorum_available: u64,
    // Replication lag metrics
    pub replication_lag_bytes: u64,
    pub replication_last_sync_ms: u64,
    pub replication_pending_ops: u64,
}

impl MetricsSnapshot {
    #[must_use]
    pub fn capture() -> Self {
        Self {
            records_ingested: RECORDS_INGESTED.load(Ordering::Relaxed),
            bytes_ingested: BYTES_INGESTED.load(Ordering::Relaxed),
            batches_written: BATCHES_WRITTEN.load(Ordering::Relaxed),
            bytes_written: BYTES_WRITTEN.load(Ordering::Relaxed),
            io_ops_submitted: IO_OPS_SUBMITTED.load(Ordering::Relaxed),
            io_ops_completed: IO_OPS_COMPLETED.load(Ordering::Relaxed),
            connections_active: CONNECTIONS_ACTIVE.load(Ordering::Relaxed),
            connections_total: CONNECTIONS_TOTAL.load(Ordering::Relaxed),
            crc_failures: CRC_FAILURES.load(Ordering::Relaxed),
            backpressure_events: BACKPRESSURE_EVENTS.load(Ordering::Relaxed),
            pool_exhausted: POOL_EXHAUSTED.load(Ordering::Relaxed),
            numa_misaligned: NUMA_MISALIGNED.load(Ordering::Relaxed),
            follower_evictions: FOLLOWER_EVICTIONS.load(Ordering::Relaxed),
            follower_recoveries: FOLLOWER_RECOVERIES.load(Ordering::Relaxed),
            quorum_failures: QUORUM_FAILURES.load(Ordering::Relaxed),
            // HLC metrics
            hlc_drift_ms: HLC_DRIFT_MS.load(Ordering::Relaxed),
            hlc_drift_warnings: HLC_DRIFT_WARNINGS.load(Ordering::Relaxed),
            hlc_drift_critical: HLC_DRIFT_CRITICAL.load(Ordering::Relaxed),
            hlc_logical_exhausted: HLC_LOGICAL_EXHAUSTED.load(Ordering::Relaxed),
            // Raft metrics
            raft_elections_started: RAFT_ELECTIONS_STARTED.load(Ordering::Relaxed),
            raft_elections_won: RAFT_ELECTIONS_WON.load(Ordering::Relaxed),
            raft_pre_votes_rejected: RAFT_PRE_VOTES_REJECTED.load(Ordering::Relaxed),
            raft_leader_stepdowns: RAFT_LEADER_STEPDOWNS.load(Ordering::Relaxed),
            raft_fencing_rejections: RAFT_FENCING_REJECTIONS.load(Ordering::Relaxed),
            // Consumer read path metrics
            reads_total: READS_TOTAL.load(Ordering::Relaxed),
            read_bytes_total: READ_BYTES_TOTAL.load(Ordering::Relaxed),
            consumer_throttled: CONSUMER_THROTTLED.load(Ordering::Relaxed),
            zero_copy_sends: ZERO_COPY_SENDS.load(Ordering::Relaxed),
            // Cluster health metrics
            cluster_leader_id: CLUSTER_LEADER_ID.load(Ordering::Relaxed),
            cluster_current_term: CLUSTER_CURRENT_TERM.load(Ordering::Relaxed),
            cluster_node_count: CLUSTER_NODE_COUNT.load(Ordering::Relaxed),
            cluster_healthy_nodes: CLUSTER_HEALTHY_NODES.load(Ordering::Relaxed),
            cluster_is_leader: CLUSTER_IS_LEADER.load(Ordering::Relaxed),
            cluster_quorum_available: CLUSTER_QUORUM_AVAILABLE.load(Ordering::Relaxed),
            // Replication lag metrics
            replication_lag_bytes: REPLICATION_LAG_BYTES.load(Ordering::Relaxed),
            replication_last_sync_ms: REPLICATION_LAST_SYNC_MS.load(Ordering::Relaxed),
            replication_pending_ops: REPLICATION_PENDING_OPS.load(Ordering::Relaxed),
        }
    }
}

pub fn init_prometheus_exporter(
    addr: SocketAddr,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let builder = metrics_exporter_prometheus::PrometheusBuilder::new();
    builder.with_http_listener(addr).install()?;

    metrics::describe_counter!(
        "lance_records_ingested_total",
        "Total number of records ingested"
    );
    metrics::describe_counter!("lance_bytes_ingested_total", "Total bytes ingested");
    metrics::describe_counter!(
        "lance_batches_written_total",
        "Total batches written to disk"
    );
    metrics::describe_counter!("lance_bytes_written_total", "Total bytes written to disk");
    metrics::describe_counter!(
        "lance_io_ops_submitted_total",
        "Total I/O operations submitted"
    );
    metrics::describe_counter!(
        "lance_io_ops_completed_total",
        "Total I/O operations completed"
    );
    metrics::describe_gauge!(
        "lance_connections_active",
        "Current number of active connections"
    );
    metrics::describe_counter!(
        "lance_connections_total",
        "Total connections ever established"
    );
    metrics::describe_counter!("lance_crc_failures_total", "Total CRC validation failures");
    metrics::describe_counter!(
        "lance_backpressure_events_total",
        "Total backpressure events"
    );
    metrics::describe_counter!(
        "lance_pool_exhausted_total",
        "Buffer pool exhaustion events"
    );
    metrics::describe_counter!("lance_numa_misaligned_total", "NUMA misalignment warnings");
    metrics::describe_counter!(
        "lance_follower_evictions_total",
        "Followers evicted from quorum"
    );
    metrics::describe_counter!(
        "lance_follower_recoveries_total",
        "Followers recovered to quorum"
    );
    metrics::describe_counter!("lance_quorum_failures_total", "Quorum not reached events");

    // HLC metrics
    metrics::describe_gauge!(
        "lance_hlc_drift_ms",
        "Current HLC drift from wall clock in milliseconds"
    );
    metrics::describe_counter!(
        "lance_hlc_drift_warnings_total",
        "HLC drift warning events (drift > 100ms)"
    );
    metrics::describe_counter!(
        "lance_hlc_drift_critical_total",
        "HLC drift critical events (drift > 1s)"
    );
    metrics::describe_counter!(
        "lance_hlc_logical_exhausted_total",
        "HLC logical counter exhaustion events"
    );

    // Raft consensus metrics
    metrics::describe_counter!(
        "lance_raft_elections_started_total",
        "Raft elections started"
    );
    metrics::describe_counter!(
        "lance_raft_elections_won_total",
        "Raft elections won (became leader)"
    );
    metrics::describe_counter!(
        "lance_raft_pre_votes_rejected_total",
        "Raft pre-votes rejected"
    );
    metrics::describe_counter!("lance_raft_leader_stepdowns_total", "Raft leader stepdowns");
    metrics::describe_counter!(
        "lance_raft_fencing_rejections_total",
        "Raft fencing token rejections"
    );

    // Consumer read path metrics
    metrics::describe_counter!("lance_reads_total", "Total read operations");
    metrics::describe_counter!("lance_read_bytes_total", "Total bytes read");
    metrics::describe_counter!(
        "lance_consumer_throttled_total",
        "Consumer rate limit events"
    );
    metrics::describe_counter!("lance_zero_copy_sends_total", "Zero-copy sends completed");

    // Cluster health metrics
    metrics::describe_gauge!("lance_cluster_leader_id", "Current cluster leader node ID");
    metrics::describe_gauge!("lance_cluster_current_term", "Current Raft term");
    metrics::describe_gauge!("lance_cluster_node_count", "Total nodes in cluster");
    metrics::describe_gauge!(
        "lance_cluster_healthy_nodes",
        "Number of healthy (connected) nodes"
    );
    metrics::describe_gauge!(
        "lance_cluster_is_leader",
        "Whether this node is the leader (1=yes, 0=no)"
    );
    metrics::describe_gauge!(
        "lance_cluster_quorum_available",
        "Whether quorum is available (1=yes, 0=no)"
    );

    // Replication lag metrics
    metrics::describe_gauge!(
        "lance_replication_lag_bytes",
        "Replication lag in bytes behind leader"
    );
    metrics::describe_gauge!(
        "lance_replication_last_sync_ms",
        "Time since last successful replication sync in milliseconds"
    );
    metrics::describe_gauge!(
        "lance_replication_pending_ops",
        "Number of pending replication operations"
    );

    // ==========================================================================
    // 4 GOLDEN SIGNALS METRICS
    // ==========================================================================

    // Latency histograms (Prometheus histogram buckets)
    metrics::describe_histogram!(
        "lance_ingest_latency_seconds",
        metrics::Unit::Seconds,
        "Ingest request latency"
    );
    metrics::describe_histogram!(
        "lance_fetch_latency_seconds",
        metrics::Unit::Seconds,
        "Fetch request latency"
    );
    metrics::describe_histogram!(
        "lance_io_latency_seconds",
        metrics::Unit::Seconds,
        "I/O operation latency"
    );
    metrics::describe_histogram!(
        "lance_replication_latency_seconds",
        metrics::Unit::Seconds,
        "Replication latency"
    );
    metrics::describe_histogram!(
        "lance_network_latency_seconds",
        metrics::Unit::Seconds,
        "Network round-trip latency"
    );

    // Traffic rates
    metrics::describe_gauge!(
        "lance_ingest_ops_per_second",
        "Ingest operations per second"
    );
    metrics::describe_gauge!(
        "lance_ingest_bytes_per_second",
        "Ingest throughput in bytes per second"
    );
    metrics::describe_gauge!("lance_read_ops_per_second", "Read operations per second");
    metrics::describe_gauge!(
        "lance_read_bytes_per_second",
        "Read throughput in bytes per second"
    );

    // Errors by type
    metrics::describe_counter!("lance_errors_total", "Total errors by type");

    // Saturation gauges
    metrics::describe_gauge!(
        "lance_queue_saturation_ratio",
        "Queue depth saturation (0.0-1.0)"
    );
    metrics::describe_gauge!(
        "lance_memory_saturation_ratio",
        "Memory saturation (0.0-1.0)"
    );
    metrics::describe_gauge!(
        "lance_buffer_pool_saturation_ratio",
        "Buffer pool saturation (0.0-1.0)"
    );
    metrics::describe_gauge!(
        "lance_connection_saturation_ratio",
        "Connection pool saturation (0.0-1.0)"
    );
    metrics::describe_gauge!("lance_pending_io_count", "Number of pending I/O operations");

    Ok(())
}

pub fn export_to_prometheus() {
    let snapshot = MetricsSnapshot::capture();

    metrics::counter!("lance_records_ingested_total").absolute(snapshot.records_ingested);
    metrics::counter!("lance_bytes_ingested_total").absolute(snapshot.bytes_ingested);
    metrics::counter!("lance_batches_written_total").absolute(snapshot.batches_written);
    metrics::counter!("lance_bytes_written_total").absolute(snapshot.bytes_written);
    metrics::counter!("lance_io_ops_submitted_total").absolute(snapshot.io_ops_submitted);
    metrics::counter!("lance_io_ops_completed_total").absolute(snapshot.io_ops_completed);
    metrics::gauge!("lance_connections_active").set(snapshot.connections_active as f64);
    metrics::counter!("lance_connections_total").absolute(snapshot.connections_total);
    metrics::counter!("lance_crc_failures_total").absolute(snapshot.crc_failures);
    metrics::counter!("lance_backpressure_events_total").absolute(snapshot.backpressure_events);
    metrics::counter!("lance_pool_exhausted_total").absolute(snapshot.pool_exhausted);
    metrics::counter!("lance_numa_misaligned_total").absolute(snapshot.numa_misaligned);
    metrics::counter!("lance_follower_evictions_total").absolute(snapshot.follower_evictions);
    metrics::counter!("lance_follower_recoveries_total").absolute(snapshot.follower_recoveries);
    metrics::counter!("lance_quorum_failures_total").absolute(snapshot.quorum_failures);

    // HLC metrics
    metrics::gauge!("lance_hlc_drift_ms").set(snapshot.hlc_drift_ms as f64);
    metrics::counter!("lance_hlc_drift_warnings_total").absolute(snapshot.hlc_drift_warnings);
    metrics::counter!("lance_hlc_drift_critical_total").absolute(snapshot.hlc_drift_critical);
    metrics::counter!("lance_hlc_logical_exhausted_total").absolute(snapshot.hlc_logical_exhausted);

    // Raft metrics
    metrics::counter!("lance_raft_elections_started_total")
        .absolute(snapshot.raft_elections_started);
    metrics::counter!("lance_raft_elections_won_total").absolute(snapshot.raft_elections_won);
    metrics::counter!("lance_raft_pre_votes_rejected_total")
        .absolute(snapshot.raft_pre_votes_rejected);
    metrics::counter!("lance_raft_leader_stepdowns_total").absolute(snapshot.raft_leader_stepdowns);
    metrics::counter!("lance_raft_fencing_rejections_total")
        .absolute(snapshot.raft_fencing_rejections);

    // Consumer read path metrics
    metrics::counter!("lance_reads_total").absolute(snapshot.reads_total);
    metrics::counter!("lance_read_bytes_total").absolute(snapshot.read_bytes_total);
    metrics::counter!("lance_consumer_throttled_total").absolute(snapshot.consumer_throttled);
    metrics::counter!("lance_zero_copy_sends_total").absolute(snapshot.zero_copy_sends);

    // Cluster health metrics
    metrics::gauge!("lance_cluster_leader_id").set(snapshot.cluster_leader_id as f64);
    metrics::gauge!("lance_cluster_current_term").set(snapshot.cluster_current_term as f64);
    metrics::gauge!("lance_cluster_node_count").set(snapshot.cluster_node_count as f64);
    metrics::gauge!("lance_cluster_healthy_nodes").set(snapshot.cluster_healthy_nodes as f64);
    metrics::gauge!("lance_cluster_is_leader").set(snapshot.cluster_is_leader as f64);
    metrics::gauge!("lance_cluster_quorum_available").set(snapshot.cluster_quorum_available as f64);

    // Replication lag metrics
    metrics::gauge!("lance_replication_lag_bytes").set(snapshot.replication_lag_bytes as f64);
    metrics::gauge!("lance_replication_last_sync_ms").set(snapshot.replication_last_sync_ms as f64);
    metrics::gauge!("lance_replication_pending_ops").set(snapshot.replication_pending_ops as f64);

    // ==========================================================================
    // 4 GOLDEN SIGNALS EXPORT
    // ==========================================================================

    let gs = GoldenSignalsSnapshot::capture();

    // Latency - export p50, p95, p99, p999 as gauges (histograms require different approach)
    export_latency_percentiles("lance_ingest_latency", &gs.latency_ingest);
    export_latency_percentiles("lance_fetch_latency", &gs.latency_fetch);
    export_latency_percentiles("lance_io_latency", &gs.latency_io);
    export_latency_percentiles("lance_replication_latency", &gs.latency_replication);
    export_latency_percentiles("lance_network_latency", &gs.latency_network);

    // Traffic rates
    metrics::gauge!("lance_ingest_ops_per_second").set(gs.traffic_ingest_ops_per_sec);
    metrics::gauge!("lance_ingest_bytes_per_second").set(gs.traffic_ingest_bytes_per_sec);
    metrics::gauge!("lance_read_ops_per_second").set(gs.traffic_read_ops_per_sec);
    metrics::gauge!("lance_read_bytes_per_second").set(gs.traffic_read_bytes_per_sec);

    // Errors by type
    for (i, &count) in gs.errors_by_type.iter().enumerate() {
        let error_type = match i {
            0 => "protocol",
            1 => "io",
            2 => "timeout",
            3 => "checksum",
            4 => "replication",
            5 => "backpressure",
            6 => "auth",
            7 => "internal",
            _ => "unknown",
        };
        metrics::counter!("lance_errors_total", "type" => error_type).absolute(count);
    }

    // Saturation
    metrics::gauge!("lance_queue_saturation_ratio").set(gs.saturation_queue);
    metrics::gauge!("lance_memory_saturation_ratio").set(gs.saturation_memory);
    metrics::gauge!("lance_buffer_pool_saturation_ratio").set(gs.saturation_buffer_pool);
    metrics::gauge!("lance_connection_saturation_ratio").set(gs.saturation_connections);
    metrics::gauge!("lance_pending_io_count").set(gs.saturation_pending_io as f64);
}

fn export_latency_percentiles(prefix: &str, snap: &LatencySnapshot) {
    let p50_sec = snap.p50() as f64 / 1_000_000.0;
    let p95_sec = snap.p95() as f64 / 1_000_000.0;
    let p99_sec = snap.p99() as f64 / 1_000_000.0;
    let p999_sec = snap.p999() as f64 / 1_000_000.0;
    let avg_sec = snap.avg_us() / 1_000_000.0;

    metrics::gauge!(format!("{}_p50_seconds", prefix)).set(p50_sec);
    metrics::gauge!(format!("{}_p95_seconds", prefix)).set(p95_sec);
    metrics::gauge!(format!("{}_p99_seconds", prefix)).set(p99_sec);
    metrics::gauge!(format!("{}_p999_seconds", prefix)).set(p999_sec);
    metrics::gauge!(format!("{}_avg_seconds", prefix)).set(avg_sec);
    metrics::gauge!(format!("{}_count", prefix)).set(snap.count as f64);
}