sonda-core 1.6.4

Core engine for Sonda — synthetic telemetry generation library
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
//! Log event model.
//!
//! Defines [`LogEvent`] and [`Severity`] — the canonical in-memory representation
//! of a structured log entry. Format-agnostic: encoding to JSON Lines or Syslog is
//! the encoder's concern, not this module's.

use std::collections::BTreeMap;
use std::time::SystemTime;

use serde::Serialize;

use crate::model::metric::Labels;

/// The severity level of a log event.
///
/// Variants map to the conventional log severity ladder. Serializes to and from
/// lowercase strings (e.g., `"info"`, `"error"`) for YAML and JSON compatibility.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "config", derive(serde::Deserialize))]
// `rename_all` is not behind cfg_attr because it applies to both Serialize
// (unconditional — used by the JSON encoder) and Deserialize (config-gated).
// Splitting it would require duplicating the attribute under two cfg_attr guards.
#[serde(rename_all = "lowercase")]
pub enum Severity {
    /// Extremely detailed diagnostic information.
    Trace,
    /// Diagnostic information useful during development.
    Debug,
    /// General informational messages.
    Info,
    /// Potentially harmful situations that warrant attention.
    Warn,
    /// Error events that may allow the application to continue.
    Error,
    /// Severe error events that will likely cause the application to abort.
    Fatal,
}

impl Severity {
    /// Returns a numeric rank for this severity level.
    ///
    /// Lower ranks represent less-severe levels. This defines the canonical
    /// ordering independently of enum variant declaration order, so reordering
    /// the variants in source code never silently breaks `Ord`.
    const fn rank(self) -> u8 {
        match self {
            Severity::Trace => 0,
            Severity::Debug => 1,
            Severity::Info => 2,
            Severity::Warn => 3,
            Severity::Error => 4,
            Severity::Fatal => 5,
        }
    }
}

impl Ord for Severity {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.rank().cmp(&other.rank())
    }
}

impl PartialOrd for Severity {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

/// A structured log entry with a timestamp, severity, message, labels, and arbitrary fields.
///
/// Labels are scenario-level key-value pairs (injected by the log runner).
/// Fields are event-level key-value metadata (produced by the generator).
/// Both are stored in sorted containers for deterministic serialization.
#[derive(Debug, Clone)]
pub struct LogEvent {
    /// The time at which the event was generated.
    pub timestamp: SystemTime,
    /// The severity level of the event.
    pub severity: Severity,
    /// The human-readable log message.
    pub message: String,
    /// Scenario-level static labels attached to every event in this scenario.
    pub labels: Labels,
    /// Arbitrary key-value metadata attached to the event.
    pub fields: BTreeMap<String, String>,
}

impl LogEvent {
    /// Create a new [`LogEvent`] with the current system time as its timestamp.
    ///
    /// # Arguments
    ///
    /// * `severity` — The severity level.
    /// * `message` — The human-readable message.
    /// * `labels` — Scenario-level static labels.
    /// * `fields` — Arbitrary key-value metadata.
    pub fn new(
        severity: Severity,
        message: String,
        labels: Labels,
        fields: BTreeMap<String, String>,
    ) -> Self {
        Self {
            timestamp: SystemTime::now(),
            severity,
            message,
            labels,
            fields,
        }
    }

    /// Create a [`LogEvent`] with an explicit timestamp.
    ///
    /// Useful for deterministic testing and log replay scenarios where the original
    /// timestamp must be preserved.
    ///
    /// # Arguments
    ///
    /// * `timestamp` — The exact timestamp to record.
    /// * `severity` — The severity level.
    /// * `message` — The human-readable message.
    /// * `labels` — Scenario-level static labels.
    /// * `fields` — Arbitrary key-value metadata.
    pub fn with_timestamp(
        timestamp: SystemTime,
        severity: Severity,
        message: String,
        labels: Labels,
        fields: BTreeMap<String, String>,
    ) -> Self {
        Self {
            timestamp,
            severity,
            message,
            labels,
            fields,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::time::{Duration, UNIX_EPOCH};

    use super::*;

    // -----------------------------------------------------------------------
    // LogEvent::new — creates event with current timestamp
    // -----------------------------------------------------------------------

    #[test]
    fn new_uses_current_timestamp() {
        let before = SystemTime::now();
        let event = LogEvent::new(
            Severity::Info,
            "hello".to_string(),
            Labels::default(),
            BTreeMap::new(),
        );
        let after = SystemTime::now();

        assert!(
            event.timestamp >= before,
            "timestamp should not precede the call"
        );
        assert!(
            event.timestamp <= after,
            "timestamp should not exceed the call"
        );
    }

    #[test]
    fn new_stores_severity_message_and_fields() {
        let mut fields = BTreeMap::new();
        fields.insert("host".to_string(), "web-01".to_string());

        let event = LogEvent::new(
            Severity::Error,
            "connection failed".to_string(),
            Labels::default(),
            fields,
        );

        assert_eq!(event.severity, Severity::Error);
        assert_eq!(event.message, "connection failed");
        assert_eq!(event.fields.get("host").map(String::as_str), Some("web-01"));
    }

    #[test]
    fn new_with_empty_fields_succeeds() {
        let event = LogEvent::new(
            Severity::Debug,
            "empty".to_string(),
            Labels::default(),
            BTreeMap::new(),
        );
        assert!(event.fields.is_empty());
    }

    // -----------------------------------------------------------------------
    // LogEvent::with_timestamp — uses exact provided timestamp
    // -----------------------------------------------------------------------

    #[test]
    fn with_timestamp_uses_exact_provided_timestamp() {
        let ts = UNIX_EPOCH + Duration::from_secs(1_700_000_000);
        let event = LogEvent::with_timestamp(
            ts,
            Severity::Warn,
            "test message".to_string(),
            Labels::default(),
            BTreeMap::new(),
        );

        assert_eq!(
            event.timestamp, ts,
            "timestamp must be exactly the one provided"
        );
    }

    #[test]
    fn with_timestamp_stores_all_fields_correctly() {
        let ts = UNIX_EPOCH + Duration::from_secs(42);
        let mut fields = BTreeMap::new();
        fields.insert("service".to_string(), "api".to_string());
        fields.insert("region".to_string(), "us-east-1".to_string());

        let event = LogEvent::with_timestamp(
            ts,
            Severity::Fatal,
            "system crash".to_string(),
            Labels::default(),
            fields,
        );

        assert_eq!(event.timestamp, ts);
        assert_eq!(event.severity, Severity::Fatal);
        assert_eq!(event.message, "system crash");
        assert_eq!(event.fields.get("service").map(String::as_str), Some("api"));
        assert_eq!(
            event.fields.get("region").map(String::as_str),
            Some("us-east-1")
        );
    }

    #[test]
    fn with_timestamp_at_unix_epoch_is_valid() {
        let event = LogEvent::with_timestamp(
            UNIX_EPOCH,
            Severity::Trace,
            "epoch".to_string(),
            Labels::default(),
            BTreeMap::new(),
        );
        assert_eq!(event.timestamp, UNIX_EPOCH);
    }

    // -----------------------------------------------------------------------
    // LogEvent: fields use BTreeMap (sorted key order)
    // -----------------------------------------------------------------------

    #[test]
    fn fields_are_sorted_by_key() {
        let mut fields = BTreeMap::new();
        fields.insert("zebra".to_string(), "z".to_string());
        fields.insert("alpha".to_string(), "a".to_string());
        fields.insert("mango".to_string(), "m".to_string());

        let event = LogEvent::new(
            Severity::Info,
            "sorted".to_string(),
            Labels::default(),
            fields,
        );

        let keys: Vec<&str> = event.fields.keys().map(String::as_str).collect();
        assert_eq!(keys, vec!["alpha", "mango", "zebra"]);
    }

    // -----------------------------------------------------------------------
    // Severity: serializes to lowercase JSON
    // -----------------------------------------------------------------------

    #[test]
    fn severity_trace_serializes_to_lowercase_json() {
        let s = serde_json::to_string(&Severity::Trace).unwrap();
        assert_eq!(s, r#""trace""#);
    }

    #[test]
    fn severity_debug_serializes_to_lowercase_json() {
        let s = serde_json::to_string(&Severity::Debug).unwrap();
        assert_eq!(s, r#""debug""#);
    }

    #[test]
    fn severity_info_serializes_to_lowercase_json() {
        let s = serde_json::to_string(&Severity::Info).unwrap();
        assert_eq!(s, r#""info""#);
    }

    #[test]
    fn severity_warn_serializes_to_lowercase_json() {
        let s = serde_json::to_string(&Severity::Warn).unwrap();
        assert_eq!(s, r#""warn""#);
    }

    #[test]
    fn severity_error_serializes_to_lowercase_json() {
        let s = serde_json::to_string(&Severity::Error).unwrap();
        assert_eq!(s, r#""error""#);
    }

    #[test]
    fn severity_fatal_serializes_to_lowercase_json() {
        let s = serde_json::to_string(&Severity::Fatal).unwrap();
        assert_eq!(s, r#""fatal""#);
    }

    // -----------------------------------------------------------------------
    // Severity: deserializes from lowercase JSON
    // These tests require the `config` feature (Deserialize impl).
    // -----------------------------------------------------------------------

    #[cfg(feature = "config")]
    #[test]
    fn severity_deserializes_from_lowercase_trace() {
        let s: Severity = serde_json::from_str(r#""trace""#).unwrap();
        assert_eq!(s, Severity::Trace);
    }

    #[cfg(feature = "config")]
    #[test]
    fn severity_deserializes_from_lowercase_debug() {
        let s: Severity = serde_json::from_str(r#""debug""#).unwrap();
        assert_eq!(s, Severity::Debug);
    }

    #[cfg(feature = "config")]
    #[test]
    fn severity_deserializes_from_lowercase_info() {
        let s: Severity = serde_json::from_str(r#""info""#).unwrap();
        assert_eq!(s, Severity::Info);
    }

    #[cfg(feature = "config")]
    #[test]
    fn severity_deserializes_from_lowercase_warn() {
        let s: Severity = serde_json::from_str(r#""warn""#).unwrap();
        assert_eq!(s, Severity::Warn);
    }

    #[cfg(feature = "config")]
    #[test]
    fn severity_deserializes_from_lowercase_error() {
        let s: Severity = serde_json::from_str(r#""error""#).unwrap();
        assert_eq!(s, Severity::Error);
    }

    #[cfg(feature = "config")]
    #[test]
    fn severity_deserializes_from_lowercase_fatal() {
        let s: Severity = serde_json::from_str(r#""fatal""#).unwrap();
        assert_eq!(s, Severity::Fatal);
    }

    #[cfg(feature = "config")]
    #[test]
    fn severity_rejects_uppercase_deserialization() {
        let result: Result<Severity, _> = serde_json::from_str(r#""INFO""#);
        assert!(
            result.is_err(),
            "uppercase severity string must be rejected"
        );
    }

    #[cfg(feature = "config")]
    #[test]
    fn severity_rejects_unknown_variant() {
        let result: Result<Severity, _> = serde_json::from_str(r#""critical""#);
        assert!(result.is_err(), "unknown severity variant must be rejected");
    }

    // -----------------------------------------------------------------------
    // Severity: serializes to lowercase YAML
    // -----------------------------------------------------------------------

    #[cfg(feature = "config")]
    #[test]
    fn severity_info_serializes_to_lowercase_yaml() {
        let s = serde_yaml_ng::to_string(&Severity::Info).unwrap();
        assert!(s.trim() == "info", "expected 'info', got: {s}");
    }

    #[cfg(feature = "config")]
    #[test]
    fn severity_error_serializes_to_lowercase_yaml() {
        let s = serde_yaml_ng::to_string(&Severity::Error).unwrap();
        assert!(s.trim() == "error", "expected 'error', got: {s}");
    }

    // -----------------------------------------------------------------------
    // Severity: Send + Sync contract
    // -----------------------------------------------------------------------

    #[test]
    fn severity_is_send_and_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<Severity>();
    }

    // -----------------------------------------------------------------------
    // Severity: explicit ordering tests
    // -----------------------------------------------------------------------

    #[test]
    fn severity_ordering_follows_severity_ladder() {
        assert!(Severity::Trace < Severity::Debug);
        assert!(Severity::Debug < Severity::Info);
        assert!(Severity::Info < Severity::Warn);
        assert!(Severity::Warn < Severity::Error);
        assert!(Severity::Error < Severity::Fatal);
    }

    #[test]
    fn severity_equal_variants_compare_as_equal() {
        assert_eq!(
            Severity::Info.cmp(&Severity::Info),
            std::cmp::Ordering::Equal
        );
        assert_eq!(
            Severity::Fatal.cmp(&Severity::Fatal),
            std::cmp::Ordering::Equal
        );
    }

    #[test]
    fn severity_partial_ord_consistent_with_ord() {
        assert_eq!(
            Severity::Trace.partial_cmp(&Severity::Fatal),
            Some(std::cmp::Ordering::Less)
        );
        assert_eq!(
            Severity::Fatal.partial_cmp(&Severity::Trace),
            Some(std::cmp::Ordering::Greater)
        );
    }

    #[test]
    fn severity_sort_produces_ascending_order() {
        let mut levels = vec![
            Severity::Fatal,
            Severity::Trace,
            Severity::Warn,
            Severity::Debug,
            Severity::Error,
            Severity::Info,
        ];
        levels.sort();
        assert_eq!(
            levels,
            vec![
                Severity::Trace,
                Severity::Debug,
                Severity::Info,
                Severity::Warn,
                Severity::Error,
                Severity::Fatal,
            ]
        );
    }

    // -----------------------------------------------------------------------
    // LogEvent: Send + Sync contract
    // -----------------------------------------------------------------------

    #[test]
    fn log_event_is_send_and_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<LogEvent>();
    }

    // -----------------------------------------------------------------------
    // LogEvent: Clone produces independent copies
    // -----------------------------------------------------------------------

    #[test]
    fn log_event_clone_is_independent() {
        let ts = UNIX_EPOCH + Duration::from_secs(1000);
        let mut fields = BTreeMap::new();
        fields.insert("k".to_string(), "v".to_string());

        let original = LogEvent::with_timestamp(
            ts,
            Severity::Info,
            "msg".to_string(),
            Labels::default(),
            fields,
        );
        let mut cloned = original.clone();

        cloned.message = "different".to_string();
        cloned.fields.insert("k".to_string(), "changed".to_string());

        assert_eq!(original.message, "msg");
        assert_eq!(original.fields.get("k").map(String::as_str), Some("v"));
    }

    // -----------------------------------------------------------------------
    // LogEvent: labels field — stores scenario-level static labels
    // -----------------------------------------------------------------------

    #[test]
    fn new_stores_labels_correctly() {
        let labels = Labels::from_pairs(&[("device", "wlan0"), ("hostname", "router-01")]).unwrap();
        let event = LogEvent::new(Severity::Info, "test".to_string(), labels, BTreeMap::new());

        assert_eq!(event.labels.len(), 2);
        let label_pairs: Vec<(&str, &str)> = event.labels.iter().collect();
        assert_eq!(label_pairs[0].0, "device");
        assert_eq!(label_pairs[0].1, "wlan0");
        assert_eq!(label_pairs[1].0, "hostname");
        assert_eq!(label_pairs[1].1, "router-01");
    }

    #[test]
    fn with_timestamp_stores_labels_correctly() {
        let ts = UNIX_EPOCH + Duration::from_secs(1_700_000_000);
        let labels = Labels::from_pairs(&[("env", "staging"), ("region", "us_west")]).unwrap();
        let event = LogEvent::with_timestamp(
            ts,
            Severity::Warn,
            "warning event".to_string(),
            labels,
            BTreeMap::new(),
        );

        assert_eq!(event.labels.len(), 2);
        let label_pairs: Vec<(&str, &str)> = event.labels.iter().collect();
        assert_eq!(label_pairs[0].0, "env");
        assert_eq!(label_pairs[0].1, "staging");
        assert_eq!(label_pairs[1].0, "region");
        assert_eq!(label_pairs[1].1, "us_west");
    }

    #[test]
    fn log_event_clone_preserves_labels() {
        let ts = UNIX_EPOCH + Duration::from_secs(1000);
        let labels = Labels::from_pairs(&[("service", "api"), ("zone", "eu1")]).unwrap();
        let original = LogEvent::with_timestamp(
            ts,
            Severity::Error,
            "cloned".to_string(),
            labels,
            BTreeMap::new(),
        );

        let cloned = original.clone();

        assert_eq!(cloned.labels.len(), 2);
        let original_pairs: Vec<(&str, &str)> = original.labels.iter().collect();
        let cloned_pairs: Vec<(&str, &str)> = cloned.labels.iter().collect();
        assert_eq!(original_pairs, cloned_pairs);
    }

    #[test]
    fn new_with_empty_labels_has_no_labels() {
        let event = LogEvent::new(
            Severity::Info,
            "no labels".to_string(),
            Labels::default(),
            BTreeMap::new(),
        );
        assert!(event.labels.is_empty());
        assert_eq!(event.labels.len(), 0);
    }
}