dial9-core 0.5.1

Telemetry event bus for dial9
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
//! `ThreadLocalBuffer` is the entrypoint for almost all dial9 events
//!
//! The TL buffer is created lazily the first time an event is sent. Events are encoded directly
//! into a thread-local `Encoder<Vec<u8>>` and flushed to the central collector when the encoded
//! batch reaches the configured batch size (default 1 MB).
//!
//! Each buffer is wrapped in `Arc<Mutex<…>>` so the flush thread can intrusively
//! drain idle/silent threads via `TlBufferHandle`s registered in `SharedState`.
use crate::collector::CentralCollector;
use crate::primitives::sync::atomic::{AtomicU64, Ordering};
use crate::primitives::sync::{Arc, Mutex, Weak};
use dial9_trace_format::encoder::{Encoder, FxHashMap};
use dial9_trace_format::{InternedStackFrames, InternedString};
use std::panic::Location;
use std::time::Duration;

// ── Public API types ────────────────────────────────────────────────────────

/// Scoped encoder for writing events into the thread-local trace buffer.
///
/// Provides access to string interning and event encoding. The borrow lifetime
/// ensures that [`InternedString`] handles created via [`intern_string`](Self::intern_string)
/// are used within the same batch — they become invalid after the buffer flushes.
///
/// You don't construct this directly; it's passed to [`Encodable::encode`].
pub struct ThreadLocalEncoder<'a> {
    encoder: &'a mut Encoder<Vec<u8>>,
    location_cache: &'a mut FxHashMap<&'static Location<'static>, String>,
    /// Events written through this handle; feeds the buffer's batch count so
    /// callers that decline to write (e.g. the metrique sink dropping an
    /// entry) are not counted.
    events_written: &'a mut usize,
}

impl std::fmt::Debug for ThreadLocalEncoder<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ThreadLocalEncoder").finish_non_exhaustive()
    }
}

impl ThreadLocalEncoder<'_> {
    /// Intern a string into the trace's string pool, returning a compact handle.
    ///
    /// If the string was already interned in this batch, returns the existing handle
    /// (no duplicate wire data). The returned [`InternedString`] is only valid for
    /// encoding within this same [`Encodable::encode`] call.
    pub fn intern_string(&mut self, s: &str) -> InternedString {
        self.encoder.intern_string_infallible(s)
    }

    /// Intern a stack-frame vector into the trace's stack pool, returning a compact handle.
    ///
    /// If the stack was already interned in this batch, returns the existing handle
    /// (no duplicate wire data). The returned [`InternedStackFrames`] is only valid for
    /// encoding within this same [`Encodable::encode`] call.
    pub fn intern_stack_frames(&mut self, frames: &[u64]) -> InternedStackFrames {
        self.encoder.intern_stack_frames_infallible(frames)
    }

    /// Encode a [`TraceEvent`](dial9_trace_format::TraceEvent) struct into the buffer.
    pub fn encode(&mut self, event: &impl dial9_trace_format::TraceEvent) {
        self.encoder.write_infallible(event);
        *self.events_written += 1;
    }

    /// Write an event with a dynamically-registered schema.
    ///
    /// `timestamp_ns` is the event's monotonic clock timestamp.
    /// `values` must match the schema's field definitions in order (excluding the timestamp).
    /// The schema is auto-registered on first use per buffer flush cycle.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use dial9_trace_format::types::FieldValue;
    ///
    /// enc.write_event(&schema, timestamp_ns, &[
    ///     FieldValue::Varint(worker_id),
    ///     FieldValue::PooledString(enc.intern_string("hello")),
    /// ]);
    /// ```
    #[doc(hidden)]
    /// Errors on validation failure; the event is dropped and the calling
    /// thread is never panicked. The caller decides whether and how to
    /// report (it knows the event's provenance; rate-limit in loops).
    #[must_use = "a validation failure means the event was dropped"]
    pub fn write_event(
        &mut self,
        schema: &dial9_trace_format::encoder::Schema,
        timestamp_ns: u64,
        values: &[dial9_trace_format::types::FieldValue],
    ) -> std::io::Result<()> {
        self.encoder.write_event(schema, timestamp_ns, values)?;
        *self.events_written += 1;
        Ok(())
    }

    /// Intern a `&'static Location` (caching the `to_string()` result).
    #[doc(hidden)]
    pub fn intern_location(&mut self, location: &'static Location<'static>) -> InternedString {
        let s = self
            .location_cache
            .entry(location)
            .or_insert_with(|| location.to_string());
        self.encoder.intern_string_infallible(s)
    }
}

/// Trait for types that can be encoded into a dial9 trace.
///
/// # Simple case — `#[derive(TraceEvent)]`
///
/// Any type implementing [`TraceEvent`](dial9_trace_format::TraceEvent) automatically
/// implements `Encodable` via a blanket impl, so you can pass it directly to
/// `Dial9Handle::record_event`:
///
/// ```ignore
/// #[derive(TraceEvent)]
/// struct MyEvent {
///     #[traceevent(timestamp)]
///     timestamp_ns: u64,
///     request_count: u32,
/// }
/// handle.record_event(MyEvent { timestamp_ns: now, request_count: 42 });
/// ```
///
/// # Advanced case — string interning
///
/// Implement `Encodable` manually when you need [`InternedString`] fields
/// for efficient repeated-string encoding:
///
/// ```ignore
/// struct HttpRequest { timestamp_ns: u64, method: String, status: u32 }
///
/// impl Encodable for HttpRequest {
///     fn encode(&self, enc: &mut ThreadLocalEncoder<'_>) {
///         let method = enc.intern_string(&self.method);
///         enc.encode(&HttpRequestWire {
///             timestamp_ns: self.timestamp_ns,
///             method,
///             status: self.status,
///         });
///     }
/// }
/// ```
///
/// # Wire event naming
///
/// The event name in the trace comes from the struct passed to
/// [`ThreadLocalEncoder::encode`], not from the type implementing `Encodable`.
/// In the example above, the trace will contain events named `"HttpRequestWire"`,
/// not `"HttpRequest"`.
///
pub trait Encodable {
    /// Encode this event into the thread-local trace buffer.
    ///
    /// Implementations should call [`ThreadLocalEncoder::encode`] exactly once.
    /// Each `encode` call is counted as one event for buffer flush decisions;
    /// calling `encode` multiple times will produce multiple wire events but
    /// only one event will be counted.
    fn encode(&self, encoder: &mut ThreadLocalEncoder<'_>);
}

impl<T: dial9_trace_format::TraceEvent> Encodable for T {
    fn encode(&self, encoder: &mut ThreadLocalEncoder<'_>) {
        encoder.encode(self);
    }
}

// ── Thread-local buffer internals ───────────────────────────────────────────

/// Tracks the last drain epoch at which a particular thread-local buffer
/// was flushed. The flush thread reads this (relaxed) to skip buffers
/// that have self-flushed recently, avoiding contention with busy workers.
#[derive(Clone)]
pub(crate) struct FlushEpoch(Arc<AtomicU64>);

impl FlushEpoch {
    pub(crate) fn new() -> Self {
        Self(Arc::new(AtomicU64::new(0)))
    }

    pub(crate) fn store(&self, epoch: u64) {
        self.0.store(epoch, Ordering::Relaxed);
    }

    pub(crate) fn load(&self) -> u64 {
        self.0.load(Ordering::Relaxed)
    }
}

/// Default maximum encoded batch size before flushing (~1MB).
const DEFAULT_BATCH_SIZE: usize = 1023 * 1024;

pub(crate) struct ThreadLocalBuffer {
    encoder: Encoder<Vec<u8>>,
    event_count: usize,
    batch_size: usize,
    collector: Option<Arc<CentralCollector>>,
    /// Caches `Location::to_string()` to avoid re-formatting on every event.
    /// Bounded by the number of `#[track_caller]` call sites in the program,
    /// which is fixed at compile time, so this does not grow unboundedly.
    location_cache: FxHashMap<&'static Location<'static>, String>,
    /// Last drain epoch at which this buffer was flushed. Shared with the
    /// flush thread via `TlBufferHandle` so it can skip busy workers.
    pub(crate) flush_epoch: FlushEpoch,
}

impl Default for ThreadLocalBuffer {
    fn default() -> Self {
        Self::new()
    }
}

impl ThreadLocalBuffer {
    fn new() -> Self {
        Self::with_batch_size(DEFAULT_BATCH_SIZE)
    }

    fn with_batch_size(batch_size: usize) -> Self {
        Self {
            // Allocate 1KB extra headroom so typical events never trigger a realloc.
            encoder: Encoder::new_to(Vec::with_capacity(batch_size + 1024))
                .expect("Vec::write_all cannot fail"),
            event_count: 0,
            batch_size,
            collector: None,
            location_cache: FxHashMap::default(),
            flush_epoch: FlushEpoch::new(),
        }
    }

    /// Ensure the collector reference is set. Called on every record_event;
    /// only the first call per thread actually stores the Arc.
    /// Returns `true` on the first call (when the collector was not yet set).
    fn set_collector(&mut self, collector: &Arc<CentralCollector>) -> bool {
        if self.collector.is_none() {
            self.collector = Some(Arc::clone(collector));
            return true;
        }
        false
    }

    fn thread_local_encoder(&mut self) -> ThreadLocalEncoder<'_> {
        ThreadLocalEncoder {
            encoder: &mut self.encoder,
            location_cache: &mut self.location_cache,
            events_written: &mut self.event_count,
        }
    }

    // Only reached via `encode_single` (test-util) and core's own tests.
    #[cfg_attr(not(feature = "test-util"), allow(dead_code))]
    fn record_encodable(&mut self, event: &dyn Encodable) {
        event.encode(&mut self.thread_local_encoder());
    }

    fn should_flush(&self) -> bool {
        self.encoder.bytes_written() as usize >= self.batch_size
    }

    pub(crate) fn flush(&mut self) -> crate::collector::Batch {
        let event_count = self.event_count as u64;
        let encoded_bytes = self
            .encoder
            .reset_to_infallible(Vec::with_capacity(self.batch_size));
        self.event_count = 0;
        crate::collector::Batch::new(encoded_bytes, event_count)
    }

    pub(crate) fn has_pending_events(&self) -> bool {
        self.event_count > 0
    }
}

crate::test_util_pub! {
/// Encode a single event into a self-contained batch (header + event).
fn encode_single(event: &dyn Encodable) -> Vec<u8> {
    let mut buf = ThreadLocalBuffer::with_batch_size(1024);
    buf.record_encodable(event);
    buf.flush().into_encoded_bytes()
}
}

impl Drop for ThreadLocalBuffer {
    fn drop(&mut self) {
        if self.event_count > 0 {
            if let Some(collector) = self.collector.take() {
                collector.accept_flush(self.flush());
            } else {
                crate::rate_limit::rate_limited!(Duration::from_secs(60), {
                    tracing::warn!(
                        "dial9-tokio-telemetry: dropping {} unflushed events (no collector registered on this thread)",
                        self.event_count
                    );
                });
            }
        }
    }
}

/// A handle to a thread-local buffer, held by `SharedState` so the flush
/// thread can intrusively drain idle/silent buffers.
pub(crate) struct TlBufferHandle {
    pub(crate) buffer: Weak<Mutex<ThreadLocalBuffer>>,
    pub(crate) flush_epoch: FlushEpoch,
}

crate::primitives::thread_local! {
    static BUFFER: Arc<Mutex<ThreadLocalBuffer>> = Arc::new(Mutex::new(ThreadLocalBuffer::new()));
}

/// Drain the current thread's buffer into `collector`, even if not full.
/// Used at shutdown and before flush cycles to avoid losing events.
pub(crate) fn drain_to_collector(collector: &CentralCollector) {
    BUFFER.with(|buf| {
        let mut buf = match buf.lock() {
            Ok(guard) => guard,
            Err(_) => {
                crate::rate_limit::rate_limited!(Duration::from_secs(60), {
                    tracing::error!("dial9: thread-local buffer mutex poisoned in drain_to_collector; skipping drain");
                });
                return;
            }
        };
        if buf.event_count > 0 {
            collector.accept_flush(buf.flush());
        }
    });
}

pub(crate) fn record_encodable_event(
    event: &dyn Encodable,
    collector: &Arc<CentralCollector>,
    drain_epoch: &AtomicU64,
) -> Option<TlBufferHandle> {
    with_encoder(|enc| event.encode(enc), collector, drain_epoch)
}

pub(crate) fn with_encoder(
    f: impl FnOnce(&mut ThreadLocalEncoder<'_>),
    collector: &Arc<CentralCollector>,
    drain_epoch: &AtomicU64,
) -> Option<TlBufferHandle> {
    BUFFER.with(|arc| {
        let mut buf = match arc.lock() {
            Ok(guard) => guard,
            Err(_) => {
                crate::rate_limit::rate_limited!(Duration::from_secs(60), {
                    tracing::error!("dial9: thread-local buffer mutex poisoned in with_encoder; dropping events for this thread");
                });
                return None;
            }
        };
        let first_call = buf.set_collector(collector);
        f(&mut buf.thread_local_encoder());
        let current_epoch = drain_epoch.load(Ordering::Relaxed);
        if buf.should_flush() || buf.flush_epoch.load() < current_epoch {
            collector.accept_flush(buf.flush());
            buf.flush_epoch.store(current_epoch);
        }
        if first_call {
            Some(TlBufferHandle {
                buffer: Arc::downgrade(arc),
                flush_epoch: buf.flush_epoch.clone(),
            })
        } else {
            None
        }
    })
}

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

    fn sample_event() -> crate::format::ClockSyncEvent {
        crate::format::ClockSyncEvent {
            timestamp_ns: 1000,
            realtime_ns: 2000,
        }
    }

    #[derive(dial9_trace_format::TraceEvent)]
    struct BorrowedEvent<'a> {
        #[traceevent(timestamp)]
        timestamp_ns: u64,
        value: &'a str,
    }

    #[test]
    fn test_buffer_creation() {
        let buffer = ThreadLocalBuffer::new();
        assert_eq!(buffer.event_count, 0);
        assert_eq!(buffer.batch_size, DEFAULT_BATCH_SIZE);
    }

    #[test]
    fn test_record_event() {
        let mut buffer = ThreadLocalBuffer::new();
        buffer.record_encodable(&sample_event());
        assert_eq!(buffer.event_count, 1);
        assert!(buffer.encoder.bytes_written() > 0);
    }

    #[test]
    fn borrowed_trace_event_is_encodable() {
        let value = String::from("borrowed");
        let event = BorrowedEvent {
            timestamp_ns: 1000,
            value: &value,
        };
        let mut buffer = ThreadLocalBuffer::new();
        buffer.record_encodable(&event);
        assert_eq!(buffer.event_count, 1);
        assert!(buffer.encoder.bytes_written() > 0);
    }

    #[test]
    fn test_should_flush_respects_batch_size() {
        // Use a tiny batch size so a single event triggers flush.
        let mut buffer = ThreadLocalBuffer::with_batch_size(1);
        assert!(!buffer.should_flush());
        buffer.record_encodable(&sample_event());
        assert!(buffer.should_flush());
    }

    #[test]
    fn test_should_flush_default_batch_size() {
        let mut buffer = ThreadLocalBuffer::new();
        assert!(!buffer.should_flush());
        buffer.record_encodable(&sample_event());
        // A single small event should not exceed 1 MB.
        assert!(!buffer.should_flush());
    }

    #[test]
    fn test_flush() {
        let mut buffer = ThreadLocalBuffer::new();
        buffer.record_encodable(&sample_event());
        let batch = buffer.flush();
        assert!(!batch.encoded_bytes().is_empty());
        assert_eq!(buffer.event_count, 0);
    }

    #[test]
    fn test_flush_epoch_store_load() {
        let epoch = FlushEpoch::new();
        assert_eq!(epoch.load(), 0);
        epoch.store(42);
        assert_eq!(epoch.load(), 42);
    }

    #[test]
    fn test_flush_epoch_shared_across_threads() {
        let epoch = FlushEpoch::new();
        let epoch_clone = epoch.clone();
        let handle = std::thread::spawn(move || {
            epoch_clone.store(7);
        });
        handle.join().unwrap();
        assert_eq!(epoch.load(), 7);
    }

    #[test]
    fn test_flush_epoch_stamped_on_self_flush() {
        let collector = Arc::new(CentralCollector::new());
        let drain_epoch = AtomicU64::new(5);
        // Use a tiny batch size so a single event triggers self-flush.
        // We can't use record_event (thread-local) easily, so test the
        // logic directly: flush + stamp.
        let mut buffer = ThreadLocalBuffer::with_batch_size(1);
        buffer.set_collector(&collector);
        buffer.record_encodable(&sample_event());
        assert!(buffer.should_flush());
        buffer
            .flush_epoch
            .store(drain_epoch.load(Ordering::Relaxed));
        collector.accept_flush(buffer.flush());
        assert_eq!(buffer.flush_epoch.load(), 5);
    }

    #[test]
    fn test_mutex_accessible_from_another_thread() {
        let buf = Arc::new(Mutex::new(ThreadLocalBuffer::new()));
        let buf_clone = Arc::clone(&buf);
        // Write an event from a different thread.
        let handle = std::thread::spawn(move || {
            let mut guard = buf_clone.lock().unwrap();
            guard.record_encodable(&sample_event());
            assert_eq!(guard.event_count, 1);
        });
        handle.join().unwrap();
        // Main thread can also access it.
        let guard = buf.lock().unwrap();
        assert_eq!(guard.event_count, 1);
    }
}