talaris 0.9.0

Low-latency HFT transport toolkit for Linux: io_uring proactor plus WebSocket/TLS/HTTP building blocks.
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
//! Opt-in observability types for marked data-pump APIs.
//!
//! The default `pump_data*` path does not construct these values and does not
//! read clocks. Callers opt in by using `pump_data*_marked`.

use std::fmt;
use std::sync::OnceLock;
use std::time::{Instant, SystemTime, UNIX_EPOCH};

use thiserror::Error;

const LATENCY_HISTOGRAM_LOWEST_NS: u64 = 1;
const LATENCY_HISTOGRAM_HIGHEST_NS: u64 = 60_000_000_000;
const LATENCY_HISTOGRAM_SIGFIG: u8 = 3;
const PROMETHEUS_QUANTILES: &[(f64, &str)] = &[
    (0.50, "0.5"),
    (0.90, "0.9"),
    (0.95, "0.95"),
    (0.99, "0.99"),
    (0.999, "0.999"),
    (0.9999, "0.9999"),
];

#[derive(Debug, Error)]
pub enum ObservabilityError {
    #[error("latency histogram setup failed: {0}")]
    Histogram(#[from] hdrhistogram::CreationError),
}

/// Deterministic sampling rate for marked observability.
///
/// The unit is basis points: `10_000` means 100%, `1_000` means 10%, and
/// `0` means disabled. Sampling is deterministic by sequence number and does
/// not read random state on the hot path.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ObservabilitySampleRate {
    basis_points: u16,
}

impl Default for ObservabilitySampleRate {
    #[inline]
    fn default() -> Self {
        Self::always()
    }
}

impl ObservabilitySampleRate {
    pub const MAX_BASIS_POINTS: u16 = 10_000;

    #[inline]
    #[must_use]
    pub const fn always() -> Self {
        Self {
            basis_points: Self::MAX_BASIS_POINTS,
        }
    }

    #[inline]
    #[must_use]
    pub const fn never() -> Self {
        Self { basis_points: 0 }
    }

    #[inline]
    #[must_use]
    pub const fn from_basis_points(basis_points: u16) -> Self {
        let basis_points = if basis_points > Self::MAX_BASIS_POINTS {
            Self::MAX_BASIS_POINTS
        } else {
            basis_points
        };
        Self { basis_points }
    }

    #[inline]
    #[must_use]
    pub const fn basis_points(self) -> u16 {
        self.basis_points
    }

    #[inline]
    #[must_use]
    pub(crate) const fn should_sample_sequence(self, sequence: u64) -> bool {
        if self.basis_points == 0 {
            return false;
        }
        if self.basis_points >= Self::MAX_BASIS_POINTS {
            return true;
        }
        let bucket = sequence.wrapping_mul(0x9E37_79B9_7F4A_7C15) % 10_000;
        bucket < self.basis_points as u64
    }
}

/// HdrHistogram-backed latency recorder for marked WebSocket data events.
///
/// The recorder keeps both cumulative and interval histograms. Cumulative
/// histograms cover the connection lifetime; interval histograms can be exported
/// and reset on each scrape.
#[derive(Debug)]
pub struct LatencyHistograms {
    cumulative: LatencyHistogramSet,
    interval: LatencyHistogramSet,
}

impl LatencyHistograms {
    pub fn new() -> Result<Self, ObservabilityError> {
        Ok(Self {
            cumulative: LatencyHistogramSet::new()?,
            interval: LatencyHistogramSet::new()?,
        })
    }

    #[inline]
    pub(crate) fn record_plaintext_chunk(&mut self, meta: DataEventMeta) {
        self.cumulative.record_plaintext_chunk(meta);
        self.interval.record_plaintext_chunk(meta);
    }

    #[inline]
    pub(crate) fn record_message(&mut self, meta: DataEventMeta) {
        self.cumulative.record_message(meta);
        self.interval.record_message(meta);
    }

    pub fn write_prometheus_help<W: fmt::Write>(out: &mut W) -> fmt::Result {
        writeln!(
            out,
            "# HELP talaris_ws_latency_quantile_ns HdrHistogram client-side quantiles for sampled WebSocket data-message latency stages."
        )?;
        writeln!(out, "# TYPE talaris_ws_latency_quantile_ns gauge")?;
        writeln!(
            out,
            "# HELP talaris_ws_latency_samples Sample count for local HdrHistogram latency stages."
        )?;
        writeln!(out, "# TYPE talaris_ws_latency_samples gauge")?;
        writeln!(
            out,
            "# HELP talaris_ws_latency_sum_ns Sum of sampled WebSocket data-message latency in nanoseconds."
        )?;
        writeln!(out, "# TYPE talaris_ws_latency_sum_ns gauge")?;
        writeln!(
            out,
            "# HELP talaris_ws_latency_max_ns Maximum sampled WebSocket data-message latency in nanoseconds."
        )?;
        writeln!(out, "# TYPE talaris_ws_latency_max_ns gauge")
    }

    pub fn write_prometheus_cumulative<W: fmt::Write>(
        &self,
        conn_id: u32,
        out: &mut W,
    ) -> fmt::Result {
        self.cumulative.write_prometheus(conn_id, "cumulative", out)
    }

    pub fn write_prometheus_interval<W: fmt::Write>(
        &self,
        conn_id: u32,
        out: &mut W,
    ) -> fmt::Result {
        self.interval.write_prometheus(conn_id, "interval", out)
    }

    pub fn write_prometheus_interval_and_reset<W: fmt::Write>(
        &mut self,
        conn_id: u32,
        out: &mut W,
    ) -> fmt::Result {
        self.write_prometheus_interval(conn_id, out)?;
        self.interval.reset();
        Ok(())
    }
}

#[derive(Debug)]
struct LatencyHistogramSet {
    recv_to_plaintext: StageHistogram,
    plaintext_to_ws_all: StageHistogram,
    plaintext_to_ws_first: StageHistogram,
    plaintext_to_ws_queued: StageHistogram,
    plaintext_to_ws_excluding_prior_sink_all: StageHistogram,
    plaintext_to_ws_excluding_prior_sink_first: StageHistogram,
    plaintext_to_ws_excluding_prior_sink_queued: StageHistogram,
    recv_to_ws_all: StageHistogram,
    recv_to_ws_first: StageHistogram,
    recv_to_ws_queued: StageHistogram,
    recv_to_ws_excluding_prior_sink_all: StageHistogram,
    recv_to_ws_excluding_prior_sink_first: StageHistogram,
    recv_to_ws_excluding_prior_sink_queued: StageHistogram,
    chunk_prior_sink_service_queued: StageHistogram,
}

impl LatencyHistogramSet {
    fn new() -> Result<Self, ObservabilityError> {
        Ok(Self {
            recv_to_plaintext: StageHistogram::new()?,
            plaintext_to_ws_all: StageHistogram::new()?,
            plaintext_to_ws_first: StageHistogram::new()?,
            plaintext_to_ws_queued: StageHistogram::new()?,
            plaintext_to_ws_excluding_prior_sink_all: StageHistogram::new()?,
            plaintext_to_ws_excluding_prior_sink_first: StageHistogram::new()?,
            plaintext_to_ws_excluding_prior_sink_queued: StageHistogram::new()?,
            recv_to_ws_all: StageHistogram::new()?,
            recv_to_ws_first: StageHistogram::new()?,
            recv_to_ws_queued: StageHistogram::new()?,
            recv_to_ws_excluding_prior_sink_all: StageHistogram::new()?,
            recv_to_ws_excluding_prior_sink_first: StageHistogram::new()?,
            recv_to_ws_excluding_prior_sink_queued: StageHistogram::new()?,
            chunk_prior_sink_service_queued: StageHistogram::new()?,
        })
    }

    #[inline]
    fn record_plaintext_chunk(&mut self, meta: DataEventMeta) {
        if let Some(nanos) = meta.recv_to_plaintext_nanos() {
            self.recv_to_plaintext.record(nanos);
        }
    }

    #[inline]
    fn record_message(&mut self, meta: DataEventMeta) {
        let position = MessageChunkPosition::from_meta(meta);
        if let Some(nanos) = meta.plaintext_to_ws_nanos() {
            self.plaintext_to_ws_all.record(nanos);
            match position {
                MessageChunkPosition::First => self.plaintext_to_ws_first.record(nanos),
                MessageChunkPosition::Queued => self.plaintext_to_ws_queued.record(nanos),
            }
        }
        if let Some(nanos) = meta.plaintext_to_ws_excluding_prior_sink_nanos() {
            self.plaintext_to_ws_excluding_prior_sink_all.record(nanos);
            match position {
                MessageChunkPosition::First => self
                    .plaintext_to_ws_excluding_prior_sink_first
                    .record(nanos),
                MessageChunkPosition::Queued => self
                    .plaintext_to_ws_excluding_prior_sink_queued
                    .record(nanos),
            }
        }
        if let Some(nanos) = meta.recv_to_ws_nanos() {
            self.recv_to_ws_all.record(nanos);
            match position {
                MessageChunkPosition::First => self.recv_to_ws_first.record(nanos),
                MessageChunkPosition::Queued => self.recv_to_ws_queued.record(nanos),
            }
        }
        if let Some(nanos) = meta.recv_to_ws_excluding_prior_sink_nanos() {
            self.recv_to_ws_excluding_prior_sink_all.record(nanos);
            match position {
                MessageChunkPosition::First => {
                    self.recv_to_ws_excluding_prior_sink_first.record(nanos);
                }
                MessageChunkPosition::Queued => {
                    self.recv_to_ws_excluding_prior_sink_queued.record(nanos);
                }
            }
        }
        if let (MessageChunkPosition::Queued, Some(nanos)) =
            (position, meta.chunk_prior_sink_service_nanos())
        {
            self.chunk_prior_sink_service_queued.record(nanos);
        }
    }

    fn reset(&mut self) {
        self.recv_to_plaintext.reset();
        self.plaintext_to_ws_all.reset();
        self.plaintext_to_ws_first.reset();
        self.plaintext_to_ws_queued.reset();
        self.plaintext_to_ws_excluding_prior_sink_all.reset();
        self.plaintext_to_ws_excluding_prior_sink_first.reset();
        self.plaintext_to_ws_excluding_prior_sink_queued.reset();
        self.recv_to_ws_all.reset();
        self.recv_to_ws_first.reset();
        self.recv_to_ws_queued.reset();
        self.recv_to_ws_excluding_prior_sink_all.reset();
        self.recv_to_ws_excluding_prior_sink_first.reset();
        self.recv_to_ws_excluding_prior_sink_queued.reset();
        self.chunk_prior_sink_service_queued.reset();
    }

    fn write_prometheus<W: fmt::Write>(
        &self,
        conn_id: u32,
        window: &str,
        out: &mut W,
    ) -> fmt::Result {
        self.recv_to_plaintext.write_prometheus(
            conn_id,
            window,
            "chunk",
            "recv_to_plaintext",
            "chunk",
            out,
        )?;
        Self::write_message_position_stage(
            conn_id,
            window,
            "plaintext_to_ws",
            [
                &self.plaintext_to_ws_all,
                &self.plaintext_to_ws_first,
                &self.plaintext_to_ws_queued,
            ],
            out,
        )?;
        Self::write_message_position_stage(
            conn_id,
            window,
            "plaintext_to_ws_excluding_prior_sink",
            [
                &self.plaintext_to_ws_excluding_prior_sink_all,
                &self.plaintext_to_ws_excluding_prior_sink_first,
                &self.plaintext_to_ws_excluding_prior_sink_queued,
            ],
            out,
        )?;
        Self::write_message_position_stage(
            conn_id,
            window,
            "recv_to_ws",
            [
                &self.recv_to_ws_all,
                &self.recv_to_ws_first,
                &self.recv_to_ws_queued,
            ],
            out,
        )?;
        Self::write_message_position_stage(
            conn_id,
            window,
            "recv_to_ws_excluding_prior_sink",
            [
                &self.recv_to_ws_excluding_prior_sink_all,
                &self.recv_to_ws_excluding_prior_sink_first,
                &self.recv_to_ws_excluding_prior_sink_queued,
            ],
            out,
        )?;
        self.chunk_prior_sink_service_queued.write_prometheus(
            conn_id,
            window,
            "message",
            "chunk_prior_sink_service",
            "queued",
            out,
        )
    }

    fn write_message_position_stage<W: fmt::Write>(
        conn_id: u32,
        window: &str,
        stage: &str,
        histograms: [&StageHistogram; 3],
        out: &mut W,
    ) -> fmt::Result {
        for (chunk_position, histogram) in ["all", "first", "queued"].into_iter().zip(histograms) {
            histogram.write_prometheus(conn_id, window, "message", stage, chunk_position, out)?;
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug)]
enum MessageChunkPosition {
    First,
    Queued,
}

impl MessageChunkPosition {
    #[inline]
    const fn from_meta(meta: DataEventMeta) -> Self {
        if meta.chunk_message_index == 0 {
            Self::First
        } else {
            Self::Queued
        }
    }
}

#[derive(Debug)]
struct StageHistogram {
    hist: hdrhistogram::Histogram<u64>,
    total_nanos: u64,
}

impl StageHistogram {
    fn new() -> Result<Self, ObservabilityError> {
        Ok(Self {
            hist: hdrhistogram::Histogram::new_with_bounds(
                LATENCY_HISTOGRAM_LOWEST_NS,
                LATENCY_HISTOGRAM_HIGHEST_NS,
                LATENCY_HISTOGRAM_SIGFIG,
            )?,
            total_nanos: 0,
        })
    }

    #[inline]
    fn record(&mut self, nanos: u64) {
        self.hist.saturating_record(nanos.max(1));
        self.total_nanos = self.total_nanos.saturating_add(nanos);
    }

    fn reset(&mut self) {
        self.hist.reset();
        self.total_nanos = 0;
    }

    fn write_prometheus<W: fmt::Write>(
        &self,
        conn_id: u32,
        window: &str,
        scope: &str,
        stage: &str,
        chunk_position: &str,
        out: &mut W,
    ) -> fmt::Result {
        for &(quantile, quantile_label) in PROMETHEUS_QUANTILES {
            let value = if self.hist.is_empty() {
                0
            } else {
                self.hist.value_at_quantile(quantile)
            };
            writeln!(
                out,
                "talaris_ws_latency_quantile_ns{{conn_id=\"{conn_id}\",window=\"{window}\",scope=\"{scope}\",stage=\"{stage}\",chunk_position=\"{chunk_position}\",quantile=\"{quantile_label}\"}} {value}"
            )?;
        }
        writeln!(
            out,
            "talaris_ws_latency_samples{{conn_id=\"{conn_id}\",window=\"{window}\",scope=\"{scope}\",stage=\"{stage}\",chunk_position=\"{chunk_position}\"}} {}",
            self.hist.len()
        )?;
        writeln!(
            out,
            "talaris_ws_latency_sum_ns{{conn_id=\"{conn_id}\",window=\"{window}\",scope=\"{scope}\",stage=\"{stage}\",chunk_position=\"{chunk_position}\"}} {}",
            self.total_nanos
        )?;
        let max = if self.hist.is_empty() {
            0
        } else {
            self.hist.max()
        };
        writeln!(
            out,
            "talaris_ws_latency_max_ns{{conn_id=\"{conn_id}\",window=\"{window}\",scope=\"{scope}\",stage=\"{stage}\",chunk_position=\"{chunk_position}\"}} {max}"
        )
    }
}

/// Per-message transport timing metadata emitted by marked data-pump APIs.
///
/// `source_recv_time_nanos` is a Unix epoch timestamp sampled when the user
/// thread observes the recv CQE. It is suitable for embedding into downstream
/// wire messages if host clocks are synchronized.
///
/// The `*_mono_nanos` fields are process-local monotonic timestamps. They are
/// only meaningful for deltas on the same host/process.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct DataEventMeta {
    /// Whether this event carries latency timestamps. Sequence and index fields
    /// remain populated even when sampling skips timestamp reads.
    pub sampled: bool,
    /// Unix epoch nanos **sampled** when the recv CQE is observed by user space.
    pub source_recv_time_nanos: i64,
    /// Per-connection sequence assigned to positive-length recv CQEs observed by
    /// the marked data-pump path. Unmarked pumps do not advance this sequence.
    pub recv_sequence: u64,
    /// Monotonic nanos **sampled** when the recv CQE is observed by user space.
    pub transport_recv_mono_nanos: u64,
    /// Monotonic nanos **sampled** when a TLS plaintext chunk is ready for WS parse.
    /// For plain TCP connections this equals `transport_recv_mono_nanos`.
    pub tls_plaintext_ready_mono_nanos: u64,
    /// Monotonic nanos **sampled** immediately before handing the WS payload to the
    /// user's sink.
    pub ws_payload_ready_mono_nanos: u64,
    /// Per-connection sequence assigned to data messages emitted by the marked
    /// data-pump path. Unmarked pumps do not advance this sequence.
    pub message_sequence: u64,
    /// Index of the TLS plaintext chunk emitted from this recv CQE. Saturates at
    /// `u16::MAX` if a single recv CQE yields more chunks than fit in `u16`.
    pub tls_plaintext_chunk_index: u16,
    /// Index of the WS data message emitted from this plaintext chunk. Saturates
    /// at `u16::MAX` if a single plaintext chunk yields more data messages than
    /// fit in `u16`.
    pub chunk_message_index: u16,
    /// Accumulated wall-clock time spent inside prior user sink callbacks for
    /// earlier data messages in this same plaintext chunk.
    ///
    /// This field is zero for the first message in a chunk and when this recv
    /// sequence is not sampled. Subtract it from `*_to_ws` deltas to estimate the
    /// parser/dispatch portion that is not caused by synchronous sink backpressure.
    pub chunk_prior_sink_service_nanos: u64,
}

impl DataEventMeta {
    #[inline]
    #[must_use]
    pub(crate) fn recv_observed_now(recv_sequence: u64, sampled: bool) -> Self {
        Self {
            sampled,
            source_recv_time_nanos: if sampled { unix_epoch_nanos_now() } else { 0 },
            recv_sequence,
            transport_recv_mono_nanos: if sampled { monotonic_nanos_now() } else { 0 },
            tls_plaintext_ready_mono_nanos: 0,
            ws_payload_ready_mono_nanos: 0,
            message_sequence: 0,
            tls_plaintext_chunk_index: 0,
            chunk_message_index: 0,
            chunk_prior_sink_service_nanos: 0,
        }
    }

    #[inline]
    #[must_use]
    pub(crate) const fn plaintext_ready_at(mut self, mono_nanos: u64, chunk_index: u16) -> Self {
        self.tls_plaintext_ready_mono_nanos = if self.sampled { mono_nanos } else { 0 };
        self.tls_plaintext_chunk_index = chunk_index;
        self.chunk_message_index = 0;
        self.chunk_prior_sink_service_nanos = 0;
        self
    }

    #[inline]
    #[must_use]
    pub(crate) fn plaintext_ready_now(self, chunk_index: u16) -> Self {
        let mono_nanos = if self.sampled {
            monotonic_nanos_now()
        } else {
            0
        };
        self.plaintext_ready_at(mono_nanos, chunk_index)
    }

    #[inline]
    #[must_use]
    pub(crate) fn ws_payload_ready_now(
        mut self,
        chunk_message_index: u16,
        message_sequence: u64,
    ) -> Self {
        self.ws_payload_ready_mono_nanos = if self.sampled {
            monotonic_nanos_now()
        } else {
            0
        };
        self.message_sequence = message_sequence;
        self.chunk_message_index = chunk_message_index;
        self
    }

    #[inline]
    #[must_use]
    pub(crate) const fn with_chunk_prior_sink_service_nanos(mut self, nanos: u64) -> Self {
        self.chunk_prior_sink_service_nanos = if self.sampled { nanos } else { 0 };
        self
    }

    #[inline]
    #[must_use]
    pub fn recv_to_plaintext_nanos(self) -> Option<u64> {
        if !self.sampled {
            return None;
        }
        self.tls_plaintext_ready_mono_nanos
            .checked_sub(self.transport_recv_mono_nanos)
    }

    #[inline]
    #[must_use]
    pub fn plaintext_to_ws_nanos(self) -> Option<u64> {
        if !self.sampled {
            return None;
        }
        self.ws_payload_ready_mono_nanos
            .checked_sub(self.tls_plaintext_ready_mono_nanos)
    }

    #[inline]
    #[must_use]
    pub fn recv_to_ws_nanos(self) -> Option<u64> {
        if !self.sampled {
            return None;
        }
        self.ws_payload_ready_mono_nanos
            .checked_sub(self.transport_recv_mono_nanos)
    }

    #[inline]
    #[must_use]
    pub fn chunk_prior_sink_service_nanos(self) -> Option<u64> {
        if self.sampled {
            Some(self.chunk_prior_sink_service_nanos)
        } else {
            None
        }
    }

    #[inline]
    #[must_use]
    pub fn plaintext_to_ws_excluding_prior_sink_nanos(self) -> Option<u64> {
        self.plaintext_to_ws_nanos()?
            .checked_sub(self.chunk_prior_sink_service_nanos()?)
    }

    #[inline]
    #[must_use]
    pub fn recv_to_ws_excluding_prior_sink_nanos(self) -> Option<u64> {
        self.recv_to_ws_nanos()?
            .checked_sub(self.chunk_prior_sink_service_nanos()?)
    }
}

/// Maximum number of marked events delivered in one batch sink call.
pub const MARKED_DATA_EVENT_BATCH_CAPACITY: usize = 32;

/// Data-only WebSocket event carrying transport timing metadata.
#[derive(Clone, Copy, Debug)]
pub enum MarkedDataEvent<'a> {
    Text {
        payload: &'a str,
        meta: DataEventMeta,
    },
    Binary {
        payload: &'a [u8],
        meta: DataEventMeta,
    },
}

impl MarkedDataEvent<'_> {
    #[inline]
    #[must_use]
    pub const fn meta(&self) -> DataEventMeta {
        match self {
            Self::Text { meta, .. } | Self::Binary { meta, .. } => *meta,
        }
    }
}

/// Fixed-capacity view of marked WebSocket data events delivered together.
#[derive(Debug)]
pub struct MarkedDataEventBatch<'a> {
    events: [Option<MarkedDataEvent<'a>>; MARKED_DATA_EVENT_BATCH_CAPACITY],
    len: usize,
    chunk_end: bool,
}

impl<'a> MarkedDataEventBatch<'a> {
    #[inline]
    pub(crate) const fn new() -> Self {
        Self {
            events: [None; MARKED_DATA_EVENT_BATCH_CAPACITY],
            len: 0,
            chunk_end: false,
        }
    }

    #[inline]
    pub(crate) fn single(event: MarkedDataEvent<'a>) -> Self {
        let mut batch = Self::new();
        let pushed = batch.push(event);
        debug_assert!(pushed);
        batch.chunk_end = true;
        batch
    }

    #[inline]
    pub(crate) fn push(&mut self, event: MarkedDataEvent<'a>) -> bool {
        if self.is_full() {
            return false;
        }
        self.events[self.len] = Some(event);
        self.len += 1;
        true
    }

    #[inline]
    #[must_use]
    pub const fn len(&self) -> usize {
        self.len
    }

    #[inline]
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.len == 0
    }

    #[inline]
    #[must_use]
    pub const fn is_full(&self) -> bool {
        self.len == MARKED_DATA_EVENT_BATCH_CAPACITY
    }

    /// Whether this is the last marked data batch emitted for the current
    /// plaintext chunk.
    ///
    /// A large chunk may produce multiple fixed-capacity batches. Callers that
    /// coalesce by chunk should accumulate until this returns `true`.
    #[inline]
    #[must_use]
    pub const fn is_chunk_end(&self) -> bool {
        self.chunk_end
    }

    #[inline]
    pub(crate) const fn set_chunk_end(&mut self, chunk_end: bool) {
        self.chunk_end = chunk_end;
    }

    #[inline]
    #[must_use]
    pub const fn capacity(&self) -> usize {
        MARKED_DATA_EVENT_BATCH_CAPACITY
    }

    #[inline]
    #[must_use]
    pub fn iter(&self) -> impl ExactSizeIterator<Item = MarkedDataEvent<'a>> + '_ {
        self.events[..self.len].iter().map(|event| match event {
            Some(event) => *event,
            None => unreachable!("marked data event batch contains only initialized slots"),
        })
    }

    #[inline]
    #[must_use]
    pub fn text_count(&self) -> usize {
        self.iter()
            .filter(|event| matches!(event, MarkedDataEvent::Text { .. }))
            .count()
    }

    #[inline]
    #[must_use]
    pub fn binary_count(&self) -> usize {
        self.len() - self.text_count()
    }
}

#[inline]
pub(crate) fn monotonic_nanos_now() -> u64 {
    static START: OnceLock<Instant> = OnceLock::new();
    let start = START.get_or_init(Instant::now);
    let nanos = start.elapsed().as_nanos();
    u64::try_from(nanos).unwrap_or(u64::MAX)
}

#[inline]
fn unix_epoch_nanos_now() -> i64 {
    let Ok(duration) = SystemTime::now().duration_since(UNIX_EPOCH) else {
        return 0;
    };
    i64::try_from(duration.as_nanos()).unwrap_or(i64::MAX)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sampling_rate_respects_boundaries() {
        let never = ObservabilitySampleRate::never();
        assert!(!never.should_sample_sequence(0));
        assert!(!never.should_sample_sequence(u64::MAX));

        let always = ObservabilitySampleRate::always();
        assert!(always.should_sample_sequence(0));
        assert!(always.should_sample_sequence(u64::MAX));

        let saturated = ObservabilitySampleRate::from_basis_points(10_001);
        assert_eq!(
            saturated.basis_points(),
            ObservabilitySampleRate::MAX_BASIS_POINTS
        );
    }

    #[test]
    fn unsampled_meta_does_not_report_deltas() {
        let meta = DataEventMeta::recv_observed_now(7, false)
            .plaintext_ready_now(0)
            .ws_payload_ready_now(0, 11);

        assert!(!meta.sampled);
        assert_eq!(meta.recv_sequence, 7);
        assert_eq!(meta.message_sequence, 11);
        assert_eq!(meta.source_recv_time_nanos, 0);
        assert_eq!(meta.transport_recv_mono_nanos, 0);
        assert_eq!(meta.tls_plaintext_ready_mono_nanos, 0);
        assert_eq!(meta.ws_payload_ready_mono_nanos, 0);
        assert_eq!(meta.recv_to_plaintext_nanos(), None);
        assert_eq!(meta.plaintext_to_ws_nanos(), None);
        assert_eq!(meta.recv_to_ws_nanos(), None);
    }

    #[test]
    fn latency_histograms_export_prometheus() -> Result<(), Box<dyn std::error::Error>> {
        let mut histograms = LatencyHistograms::new()?;
        let first = DataEventMeta {
            sampled: true,
            source_recv_time_nanos: 1,
            recv_sequence: 2,
            transport_recv_mono_nanos: 100,
            tls_plaintext_ready_mono_nanos: 160,
            ws_payload_ready_mono_nanos: 250,
            message_sequence: 3,
            tls_plaintext_chunk_index: 0,
            chunk_message_index: 0,
            chunk_prior_sink_service_nanos: 0,
        };
        let queued = DataEventMeta {
            chunk_message_index: 1,
            message_sequence: 4,
            ws_payload_ready_mono_nanos: 330,
            chunk_prior_sink_service_nanos: 80,
            ..first
        };
        histograms.record_plaintext_chunk(first);
        histograms.record_message(first);
        histograms.record_message(queued);

        let mut out = String::new();
        LatencyHistograms::write_prometheus_help(&mut out)?;
        histograms.write_prometheus_cumulative(7, &mut out)?;

        assert!(out.contains("# TYPE talaris_ws_latency_quantile_ns gauge"));
        assert!(out.contains(
            "talaris_ws_latency_samples{conn_id=\"7\",window=\"cumulative\",scope=\"chunk\",stage=\"recv_to_plaintext\",chunk_position=\"chunk\"} 1"
        ));
        assert!(out.contains(
            "talaris_ws_latency_sum_ns{conn_id=\"7\",window=\"cumulative\",scope=\"chunk\",stage=\"recv_to_plaintext\",chunk_position=\"chunk\"} 60"
        ));
        assert!(out.contains(
            "talaris_ws_latency_samples{conn_id=\"7\",window=\"cumulative\",scope=\"message\",stage=\"plaintext_to_ws\",chunk_position=\"all\"} 2"
        ));
        assert!(
            out.contains("talaris_ws_latency_sum_ns{conn_id=\"7\",window=\"cumulative\",scope=\"message\",stage=\"plaintext_to_ws\",chunk_position=\"queued\"} 170")
        );
        assert!(out.contains(
            "talaris_ws_latency_sum_ns{conn_id=\"7\",window=\"cumulative\",scope=\"message\",stage=\"plaintext_to_ws_excluding_prior_sink\",chunk_position=\"queued\"} 90"
        ));
        assert!(
            out.contains("talaris_ws_latency_max_ns{conn_id=\"7\",window=\"cumulative\",scope=\"message\",stage=\"recv_to_ws\",chunk_position=\"all\"} 230")
        );
        assert!(out.contains(
            "talaris_ws_latency_sum_ns{conn_id=\"7\",window=\"cumulative\",scope=\"message\",stage=\"chunk_prior_sink_service\",chunk_position=\"queued\"} 80"
        ));
        Ok(())
    }

    #[test]
    fn interval_export_resets_interval_histograms() -> Result<(), Box<dyn std::error::Error>> {
        let mut histograms = LatencyHistograms::new()?;
        let meta = DataEventMeta {
            sampled: true,
            source_recv_time_nanos: 1,
            recv_sequence: 2,
            transport_recv_mono_nanos: 100,
            tls_plaintext_ready_mono_nanos: 160,
            ws_payload_ready_mono_nanos: 250,
            message_sequence: 3,
            tls_plaintext_chunk_index: 0,
            chunk_message_index: 0,
            chunk_prior_sink_service_nanos: 0,
        };
        histograms.record_plaintext_chunk(meta);
        histograms.record_message(meta);

        let mut first = String::new();
        histograms.write_prometheus_interval_and_reset(9, &mut first)?;
        assert!(first.contains(
            "talaris_ws_latency_samples{conn_id=\"9\",window=\"interval\",scope=\"message\",stage=\"recv_to_ws\",chunk_position=\"all\"} 1"
        ));

        let mut second = String::new();
        histograms.write_prometheus_interval(9, &mut second)?;
        assert!(second.contains(
            "talaris_ws_latency_samples{conn_id=\"9\",window=\"interval\",scope=\"message\",stage=\"recv_to_ws\",chunk_position=\"all\"} 0"
        ));

        let mut cumulative = String::new();
        histograms.write_prometheus_cumulative(9, &mut cumulative)?;
        assert!(cumulative.contains(
            "talaris_ws_latency_samples{conn_id=\"9\",window=\"cumulative\",scope=\"message\",stage=\"recv_to_ws\",chunk_position=\"all\"} 1"
        ));
        Ok(())
    }
}