limen-core 0.1.0-alpha.1

Limen core contracts and primitives.
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
//! Telemetry sink trait and implementations.

use super::*;

/// Error type returned by event writers when writing fails.
///
/// Implementations use this to signal input output failures or buffer
/// exhaustion without tying the core telemetry code to any particular
/// error type.
#[non_exhaustive]
#[derive(Copy, Clone, Debug)]
pub enum TelemetrySinkError {
    /// The event could not be pushed to the underlying sink.
    PushFailed,
}

/// Write-only sink for structured telemetry events.
///
/// Event writers are responsible for transporting events to logs, sockets,
/// buffers, or any other destination. They are deliberately minimal and can
/// be implemented in both no_std and std environments.
pub trait TelemetrySink {
    /// Write a single telemetry event to the underlying sink.
    #[inline]
    fn push_event(&mut self, _event: &TelemetryEvent) -> Result<(), TelemetrySinkError> {
        Ok(())
    }

    /// Write a snapshot of the current graph metrics.
    ///
    /// Implementations that are only interested in events can ignore this
    /// by keeping the default no-op implementation.
    #[inline]
    fn push_metrics<const MAX_NODES: usize, const MAX_EDGES: usize>(
        &mut self,
        _graph: &GraphMetrics<MAX_NODES, MAX_EDGES>,
    ) -> Result<(), TelemetrySinkError> {
        Ok(())
    }

    /// Flush any buffered events to the underlying sink.
    ///
    /// The default implementation is a no operation; implementations that
    /// buffer data should override this.
    #[inline]
    fn flush(&mut self) -> Result<(), TelemetrySinkError> {
        Ok(())
    }
}

/// Convert a watermark state into a stable string representation.
///
/// This helper is used by line based writers to render human readable
/// representations of watermark transitions.
#[inline]
fn wm_str(wm: WatermarkState) -> &'static str {
    match wm {
        WatermarkState::BelowSoft => "BelowSoft",
        WatermarkState::BetweenSoftAndHard => "BetweenSoftAndHard",
        WatermarkState::AtOrAboveHard => "AtOrAboveHard",
    }
}

/// Write an unsigned integer in base ten to a `fmt::Write` target.
///
/// This helper is a small, allocation free formatter used by line based
/// writers in both no_std and std configurations.
#[inline]
pub fn write_u64<W: fmt::Write>(writer: &mut W, mut value: u64) -> fmt::Result {
    if value == 0 {
        return writer.write_str("0");
    }

    let mut buffer = [0u8; 20];
    let mut write_index = buffer.len();

    while value != 0 {
        write_index -= 1;
        let digit = (value % 10) as u8;
        buffer[write_index] = b'0' + digit;
        value /= 10;
    }

    // This cannot fail because we only wrote ASCII digits.
    let string_slice = core::str::from_utf8(&buffer[write_index..]).unwrap();
    writer.write_str(string_slice)
}

/// Format a telemetry event as a single line of text.
///
/// The exact format is stable and intended for machine parsing as well as
/// human reading. It does not allocate and only uses `fmt::Write`.
pub fn fmt_event<W: fmt::Write>(w: &mut W, e: &TelemetryEvent) -> fmt::Result {
    match e {
        TelemetryEvent::Runtime(ev) => {
            w.write_str("runtime id=")?;
            write_u64(w, *ev.graph_id() as u64)?;
            w.write_str(" ts=")?;
            write_u64(w, *ev.timestamp_ns())?;
            w.write_str(" kind=")?;
            w.write_str(match ev.event_kind() {
                RuntimeTelemetryEventKind::GraphStarted => "GraphStarted",
                RuntimeTelemetryEventKind::GraphStopped => "GraphStopped",
                RuntimeTelemetryEventKind::GraphPanicked => "GraphPanicked",
                RuntimeTelemetryEventKind::SensorDisconnected => "SensorDisconnected",
                RuntimeTelemetryEventKind::SensorRecovered => "SensorRecovered",
                RuntimeTelemetryEventKind::ModelLoadFailed => "ModelLoadFailed",
                RuntimeTelemetryEventKind::ModelRecovered => "ModelRecovered",
                RuntimeTelemetryEventKind::MqttDisconnected => "MqttDisconnected",
                RuntimeTelemetryEventKind::MqttRecovered => "MqttRecovered",
                RuntimeTelemetryEventKind::DataGapDetected => "DataGapDetected",
                RuntimeTelemetryEventKind::InvalidDataSeen => "InvalidDataSeen",
            })?;
            w.write_str(" msg=")?;
            if let Some(msg) = ev.message() {
                w.write_str(msg.as_str())?;
            } else {
                w.write_str("-")?;
            }
            w.write_str("\n")
        }
        TelemetryEvent::NodeStep(ev) => {
            w.write_str("node-step gid=")?;
            write_u64(w, *ev.graph_id() as u64)?;
            w.write_str(" nin=")?;
            write_u64(w, *ev.node_index().as_usize() as u64)?;
            w.write_str(" ts_start=")?;
            write_u64(w, *ev.timestamp_start_ns())?;
            w.write_str(" ts_end=")?;
            write_u64(w, *ev.timestamp_end_ns())?;
            w.write_str(" dur=")?;
            w.write_str(" msg_processed=")?;
            write_u64(w, *ev.processed_count())?;
            write_u64(w, *ev.duration_ns())?;
            w.write_str(" dl=")?;
            if let Some(d) = *ev.deadline_ns() {
                write_u64(w, d)?;
            } else {
                w.write_str("-")?;
            }
            w.write_str(" miss=")?;
            w.write_str(if *ev.deadline_missed() { "1" } else { "0" })?;
            w.write_str(" err=")?;
            if let Some(k) = ev.error_kind() {
                w.write_str(match k {
                    NodeStepError::NoInput => "NoInput",
                    NodeStepError::Backpressured => "BackPressured",
                    NodeStepError::OverBudget => "OverBudget",
                    NodeStepError::ExternalUnavailable => "ExternalUnavailable",
                    NodeStepError::ExecutionFailed => "ExecutionFailed",
                })?;
            } else {
                w.write_str("-")?;
            }
            w.write_str("\n")
        }
        TelemetryEvent::EdgeSnapshot(ev) => {
            w.write_str("edge-snap gid=")?;
            write_u64(w, *ev.graph_id() as u64)?;
            w.write_str(" eid=")?;
            write_u64(w, *ev.edge_index().as_usize() as u64)?;
            w.write_str(" ts=")?;
            write_u64(w, *ev.timestamp_ns())?;
            w.write_str(" occ=")?;
            write_u64(w, *ev.current_occupancy() as u64)?;
            w.write_str(" wm=")?;
            w.write_str(wm_str(*ev.watermark_state()))?;
            w.write_str("\n")
        }
    }
}

// ---- no_std sink ----

/// Event writer that formats events as lines of text using `core::fmt::Write`.
///
/// This is a no_std friendly sink that can write to any type that implements
/// `fmt::Write`, such as a ring buffer or fixed size string.
pub struct FmtLineWriter<W: fmt::Write> {
    /// Inner writer that receives formatted event lines.
    inner: W,
}

impl<W: fmt::Write> FmtLineWriter<W> {
    /// Create a new line based event writer around the given writer.
    pub fn new(writer: W) -> Self {
        Self { inner: writer }
    }

    /// Access the inner `fmt::Write` target.
    #[inline]
    pub fn inner(&self) -> &W {
        &self.inner
    }
}

impl<W: fmt::Write> TelemetrySink for FmtLineWriter<W> {
    fn push_event(&mut self, e: &TelemetryEvent) -> Result<(), TelemetrySinkError> {
        fmt_event(&mut self.inner, e).map_err(|_| TelemetrySinkError::PushFailed)
    }

    fn push_metrics<const MAX_NODES: usize, const MAX_EDGES: usize>(
        &mut self,
        graph: &GraphMetrics<MAX_NODES, MAX_EDGES>,
    ) -> Result<(), TelemetrySinkError> {
        graph
            .fmt(&mut self.inner)
            .map_err(|_| TelemetrySinkError::PushFailed)
    }
}

impl<W: fmt::Write + Clone> Clone for FmtLineWriter<W> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

/// Fixed-size, owned buffer implementing `core::fmt::Write`.
///
/// - Completely no_std and allocation-free.
/// - Capacity is a const generic `N`.
/// - On overflow, `write_str` returns `Err(fmt::Error)` (no partial writes).
#[derive(Clone, Copy)]
pub struct FixedBuffer<const N: usize> {
    buffer: [u8; N],
    length: usize,
}

impl<const N: usize> Default for FixedBuffer<N> {
    fn default() -> Self {
        Self::new()
    }
}

impl<const N: usize> FixedBuffer<N> {
    /// Create a new empty buffer.
    pub const fn new() -> Self {
        Self {
            buffer: [0u8; N],
            length: 0,
        }
    }

    /// Maximum capacity in bytes.
    #[inline]
    pub const fn capacity(&self) -> usize {
        N
    }

    /// Number of bytes currently written.
    #[inline]
    pub fn len(&self) -> &usize {
        &self.length
    }

    /// Returns true if the buffer is empty.
    #[inline]
    pub const fn is_empty(&self) -> bool {
        self.length == 0
    }

    /// Access the written portion as bytes.
    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        &self.buffer[..self.length]
    }

    /// Access the written portion as `&str`.
    ///
    /// Panics if the contents are not valid UTF-8, which is fine for telemetry
    /// because we only ever write ASCII.
    #[inline]
    pub fn as_str(&self) -> &str {
        core::str::from_utf8(self.as_bytes()).unwrap()
    }

    /// Clear the buffer without reallocating.
    #[inline]
    pub fn clear(&mut self) {
        self.length = 0;
    }
}

impl<const N: usize> fmt::Write for FixedBuffer<N> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        let bytes = s.as_bytes();
        if self.length + bytes.len() > N {
            return Err(fmt::Error);
        }
        let start = self.length;
        let end = start + bytes.len();
        self.buffer[start..end].copy_from_slice(bytes);
        self.length = end;
        Ok(())
    }
}

/// Convenience constructor for a line writer over a fixed owned buffer.
///
/// This is pure no_std: no heap, no std::io.
pub fn fixed_buffer_line_writer<const N: usize>() -> FmtLineWriter<FixedBuffer<N>> {
    FmtLineWriter::new(FixedBuffer::<N>::new())
}

// ---- std sink ----

#[cfg(feature = "std")]
struct BufWriter<'a> {
    data: &'a mut [u8],
    len: usize,
}

#[cfg(feature = "std")]
impl<'a> BufWriter<'a> {
    fn new(storage: &'a mut [u8]) -> Self {
        Self {
            data: storage,
            len: 0,
        }
    }

    fn as_slice(&self) -> &[u8] {
        &self.data[..self.len]
    }
}

#[cfg(feature = "std")]
impl<'a> fmt::Write for BufWriter<'a> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        let bytes = s.as_bytes();
        if self.len + bytes.len() > self.data.len() {
            return Err(fmt::Error);
        }
        self.data[self.len..self.len + bytes.len()].copy_from_slice(bytes);
        self.len += bytes.len();
        Ok(())
    }
}

/// Event writer that formats events as lines and writes them to a `std::io::Write`
/// target.
///
/// This is a std only sink suitable for writing to files, sockets, or standard
/// output without any heap allocation in the formatting path.
#[cfg(feature = "std")]
pub struct IoLineWriter<W: std::io::Write> {
    /// Inner writer that receives encoded event bytes.
    inner: W,
}

#[cfg(feature = "std")]
impl<W: std::io::Write> IoLineWriter<W> {
    /// Create a new input output backed event writer around the given writer.
    pub fn new(writer: W) -> Self {
        Self { inner: writer }
    }

    /// Create an `IoLineWriter` that writes telemetry lines to `stdout`.
    pub fn stdout_writer() -> IoLineWriter<std::io::Stdout> {
        IoLineWriter::new(std::io::stdout())
    }

    /// Create an `IoLineWriter` that writes telemetry lines to a file at `path`.
    pub fn file_writer(path: &str) -> std::io::Result<IoLineWriter<std::fs::File>> {
        let file = std::fs::File::create(path)?;
        Ok(IoLineWriter::new(file))
    }
}

#[cfg(feature = "std")]
impl<W: std::io::Write> TelemetrySink for IoLineWriter<W> {
    fn push_event(&mut self, event: &TelemetryEvent) -> Result<(), TelemetrySinkError> {
        let mut buffer = [0u8; 256];
        let mut writer = BufWriter::new(&mut buffer);

        if fmt_event(&mut writer, event).is_err() {
            let mut heap_buffer = String::new();
            fmt_event(&mut heap_buffer, event).map_err(|_| TelemetrySinkError::PushFailed)?;
            self.inner
                .write_all(heap_buffer.as_bytes())
                .map_err(|_| TelemetrySinkError::PushFailed)
        } else {
            self.inner
                .write_all(writer.as_slice())
                .map_err(|_| TelemetrySinkError::PushFailed)
        }
    }

    fn push_metrics<const MAX_NODES: usize, const MAX_EDGES: usize>(
        &mut self,
        graph: &GraphMetrics<MAX_NODES, MAX_EDGES>,
    ) -> Result<(), TelemetrySinkError> {
        let mut buffer = [0u8; 4096];
        let mut writer = BufWriter::new(&mut buffer);

        if graph.fmt(&mut writer).is_err() {
            let mut heap_buffer = String::new();
            graph
                .fmt(&mut heap_buffer)
                .map_err(|_| TelemetrySinkError::PushFailed)?;
            self.inner
                .write_all(heap_buffer.as_bytes())
                .map_err(|_| TelemetrySinkError::PushFailed)
        } else {
            self.inner
                .write_all(writer.as_slice())
                .map_err(|_| TelemetrySinkError::PushFailed)
        }
    }

    fn flush(&mut self) -> Result<(), TelemetrySinkError> {
        self.inner
            .flush()
            .map_err(|_| TelemetrySinkError::PushFailed)
    }
}

#[cfg(feature = "std")]
impl<W: std::io::Write + Clone> Clone for IoLineWriter<W> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl TelemetrySink for () {}