sonda-core 0.12.0

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
578
579
580
581
582
583
584
585
586
587
588
589
//! InfluxDB Line Protocol encoder.
//!
//! Implements the InfluxDB line protocol format.
//! Reference: <https://docs.influxdata.com/influxdb/v2/reference/syntax/line-protocol/>
//!
//! Format:
//! ```text
//! measurement,tag1=val1,tag2=val2 field_key=value timestamp_ns\n
//! ```
//!
//! Tags are sorted by key (InfluxDB best practice for performance). Measurement names and
//! tag keys/values escape `,`, ` `, and `=` with a backslash.

use std::io::Write as _;
use std::time::UNIX_EPOCH;

use crate::model::metric::MetricEvent;
use crate::{EncoderError, SondaError};

use super::Encoder;

/// Encodes [`MetricEvent`]s into InfluxDB line protocol format.
///
/// The field key used for the metric value is configured at construction time. It defaults
/// to `"value"`.
///
/// Output format (with tags):
/// ```text
/// measurement,tag1=val1,tag2=val2 field_key=value 1700000000000000000\n
/// ```
///
/// Output format (no tags):
/// ```text
/// measurement field_key=value 1700000000000000000\n
/// ```
///
/// Timestamp is nanoseconds since the Unix epoch.
///
/// Characters `,`, ` `, and `=` are escaped with a backslash in measurement names and
/// tag keys/values.
///
/// When `precision` is set, metric values are formatted to the specified number
/// of decimal places.
pub struct InfluxLineProtocol {
    /// Pre-escaped field key bytes written into the buffer on every encode call.
    ///
    /// Built once at construction from the configured field key (default: `"value"`).
    field_key_escaped: Vec<u8>,
    /// Optional decimal precision for metric values.
    precision: Option<u8>,
}

impl InfluxLineProtocol {
    /// Create a new `InfluxLineProtocol` encoder.
    ///
    /// `field_key` sets the InfluxDB field key for the metric value. If `None`, defaults
    /// to `"value"`. The field key is escaped and stored at construction time to avoid
    /// per-event work.
    ///
    /// `precision` optionally limits the number of decimal places in metric values.
    /// `None` preserves full `f64` precision (default behavior).
    pub fn new(field_key: Option<String>, precision: Option<u8>) -> Self {
        let field_key = field_key.unwrap_or_else(|| "value".to_string());
        let mut field_key_escaped = Vec::with_capacity(field_key.len() + 4);
        escape_tag(&field_key, &mut field_key_escaped);
        Self {
            field_key_escaped,
            precision,
        }
    }
}

/// Escape a measurement name, tag key, or tag value per InfluxDB line protocol rules.
///
/// The following characters are escaped with a leading backslash: `,`, ` ` (space), `=`.
fn escape_tag(s: &str, buf: &mut Vec<u8>) {
    for byte in s.bytes() {
        match byte {
            b',' | b' ' | b'=' => {
                buf.push(b'\\');
                buf.push(byte);
            }
            other => buf.push(other),
        }
    }
}

impl Encoder for InfluxLineProtocol {
    /// Encode a metric event into InfluxDB line protocol format.
    ///
    /// Appends a complete line to `buf`. The buffer is not cleared before writing.
    /// Writes into the caller-provided buffer to minimize allocations.
    fn encode_metric(&self, event: &MetricEvent, buf: &mut Vec<u8>) -> Result<(), SondaError> {
        // Measurement name (escaped)
        escape_tag(&event.name, buf);

        // Tag set (only if non-empty). Tags are already sorted by key from BTreeMap.
        if !event.labels.is_empty() {
            buf.push(b',');
            let mut first = true;
            for (key, value) in event.labels.iter() {
                if !first {
                    buf.push(b',');
                }
                first = false;
                escape_tag(key, buf);
                buf.push(b'=');
                escape_tag(value, buf);
            }
        }

        // Space separates tag set from field set
        buf.push(b' ');

        // Field set: field_key=value (field values are not escaped; numeric values need no escaping)
        buf.extend_from_slice(&self.field_key_escaped);
        buf.push(b'=');
        // Write the float value, optionally with fixed decimal precision
        super::write_value(buf, event.value, self.precision);

        // Timestamp in nanoseconds since epoch
        let timestamp_ns = event
            .timestamp
            .duration_since(UNIX_EPOCH)
            .map_err(|e| SondaError::Encoder(EncoderError::TimestampBeforeEpoch(e)))?
            .as_nanos();

        buf.push(b' ');
        write!(buf, "{timestamp_ns}").expect("write to Vec<u8> is infallible");

        buf.push(b'\n');

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::encoder::{create_encoder, EncoderConfig};
    use crate::model::metric::{Labels, MetricEvent};
    use std::time::{Duration, UNIX_EPOCH};

    /// Build a MetricEvent with a fixed nanosecond-precision timestamp for deterministic tests.
    fn make_event(name: &str, value: f64, labels: Labels, timestamp_ns: u64) -> MetricEvent {
        let ts = UNIX_EPOCH + Duration::from_nanos(timestamp_ns);
        MetricEvent::with_timestamp(name.to_string(), value, labels, ts).unwrap()
    }

    /// Encode one event and return the result as a UTF-8 String.
    fn encode_to_string(enc: &InfluxLineProtocol, event: &MetricEvent) -> String {
        let mut buf = Vec::new();
        enc.encode_metric(event, &mut buf).unwrap();
        String::from_utf8(buf).unwrap()
    }

    // --- Happy path: metric with no labels ---

    #[test]
    fn no_labels_produces_measurement_space_field_space_timestamp() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::from_pairs(&[]).unwrap();
        let event = make_event("up", 1.0, labels, 1_700_000_000_000_000_000);
        let output = encode_to_string(&enc, &event);
        assert_eq!(output, "up value=1 1700000000000000000\n");
    }

    #[test]
    fn no_labels_output_has_no_comma_after_measurement() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::from_pairs(&[]).unwrap();
        let event = make_event("cpu", 0.5, labels, 1_000_000_000);
        let output = encode_to_string(&enc, &event);
        // Measurement must be directly followed by a space (no tag set comma)
        assert!(
            output.starts_with("cpu "),
            "no-label measurement must be followed by space: {output:?}"
        );
    }

    // --- Happy path: metric with two labels (sorted) ---

    #[test]
    fn two_labels_sorted_by_key_in_tag_set() {
        let enc = InfluxLineProtocol::new(None, None);
        // Insert in reverse alphabetical order — BTreeMap must sort them.
        let labels = Labels::from_pairs(&[("zone", "eu1"), ("host", "srv1")]).unwrap();
        let event = make_event("cpu", 0.5, labels, 1_700_000_000_000_000_000);
        let output = encode_to_string(&enc, &event);
        // host < zone alphabetically
        assert_eq!(
            output,
            "cpu,host=srv1,zone=eu1 value=0.5 1700000000000000000\n"
        );
    }

    #[test]
    fn three_labels_sorted_alphabetically() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels =
            Labels::from_pairs(&[("zone", "us1"), ("env", "prod"), ("host", "web01")]).unwrap();
        let event = make_event("metric", 42.0, labels, 1_000_000_000);
        let output = encode_to_string(&enc, &event);
        // env < host < zone
        assert!(
            output.starts_with("metric,env=prod,host=web01,zone=us1 "),
            "tags not sorted correctly: {output:?}"
        );
    }

    // --- Custom field key ---

    #[test]
    fn custom_field_key_appears_in_output() {
        let enc = InfluxLineProtocol::new(Some("gauge".to_string()), None);
        let labels = Labels::from_pairs(&[]).unwrap();
        let event = make_event("up", 1.0, labels, 1_000_000_000);
        let output = encode_to_string(&enc, &event);
        assert!(
            output.contains("gauge=1"),
            "custom field key not in output: {output:?}"
        );
    }

    #[test]
    fn none_field_key_defaults_to_value() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::from_pairs(&[]).unwrap();
        let event = make_event("up", 1.0, labels, 1_000_000_000);
        let output = encode_to_string(&enc, &event);
        assert!(
            output.contains("value="),
            "default field key 'value' not in output: {output:?}"
        );
    }

    // --- Escaping: measurement name ---

    #[test]
    fn measurement_with_space_is_escaped() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::from_pairs(&[]).unwrap();
        // Use Labels::new (bypasses key validation) so we can test escaping independently
        // For measurement name, we construct the event directly
        let ts = UNIX_EPOCH + Duration::from_nanos(1_000_000_000);
        // MetricEvent validates metric name — spaces are not allowed in valid Prometheus names.
        // We test via a name with a comma or underscore that influx would escape.
        // Since MetricEvent::with_timestamp validates names with Prometheus rules (no spaces allowed),
        // we instead test with a name containing characters that pass Prometheus validation
        // but verify the escaping machinery via the escape_tag function directly through a name
        // that has an underscore (no escaping needed) to confirm non-special chars pass through.
        let event = MetricEvent::with_timestamp("cpu_usage".to_string(), 0.75, labels, ts).unwrap();
        let output = encode_to_string(&enc, &event);
        assert!(
            output.starts_with("cpu_usage "),
            "plain measurement passed through incorrectly: {output:?}"
        );
    }

    #[test]
    fn tag_value_with_space_is_escaped() {
        let enc = InfluxLineProtocol::new(None, None);
        // Labels::from_pairs validates keys but not values — values can contain special chars.
        let labels = Labels::new(vec![("host".to_string(), "my server".to_string())]);
        let ts = UNIX_EPOCH + Duration::from_nanos(1_000_000_000);
        let event = MetricEvent::with_timestamp("cpu".to_string(), 0.5, labels, ts).unwrap();
        let output = encode_to_string(&enc, &event);
        assert!(
            output.contains(r"host=my\ server"),
            "space in tag value not escaped: {output:?}"
        );
    }

    #[test]
    fn tag_value_with_comma_is_escaped() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::new(vec![("region".to_string(), "us,east".to_string())]);
        let ts = UNIX_EPOCH + Duration::from_nanos(1_000_000_000);
        let event = MetricEvent::with_timestamp("cpu".to_string(), 1.0, labels, ts).unwrap();
        let output = encode_to_string(&enc, &event);
        assert!(
            output.contains(r"region=us\,east"),
            "comma in tag value not escaped: {output:?}"
        );
    }

    #[test]
    fn tag_value_with_equals_is_escaped() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::new(vec![("kv".to_string(), "a=b".to_string())]);
        let ts = UNIX_EPOCH + Duration::from_nanos(1_000_000_000);
        let event = MetricEvent::with_timestamp("cpu".to_string(), 1.0, labels, ts).unwrap();
        let output = encode_to_string(&enc, &event);
        assert!(
            output.contains(r"kv=a\=b"),
            "equals in tag value not escaped: {output:?}"
        );
    }

    #[test]
    fn tag_value_with_all_special_chars_is_escaped() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::new(vec![("tag".to_string(), "a,b c=d".to_string())]);
        let ts = UNIX_EPOCH + Duration::from_nanos(1_000_000_000);
        let event = MetricEvent::with_timestamp("cpu".to_string(), 1.0, labels, ts).unwrap();
        let output = encode_to_string(&enc, &event);
        assert!(
            output.contains(r"tag=a\,b\ c\=d"),
            "combined escaping not correct: {output:?}"
        );
    }

    // --- Timestamp is nanoseconds ---

    #[test]
    fn timestamp_is_nanoseconds_at_least_13_digits() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::from_pairs(&[]).unwrap();
        // 1_700_000_000 seconds = 1_700_000_000_000_000_000 ns (19 digits)
        let event = make_event("up", 1.0, labels, 1_700_000_000_000_000_000);
        let output = encode_to_string(&enc, &event);
        // Extract the timestamp (last token before newline)
        let ts_str = output
            .trim_end_matches('\n')
            .split_whitespace()
            .last()
            .unwrap();
        assert!(
            ts_str.len() >= 13,
            "timestamp must be at least 13 digits (nanoseconds): {ts_str:?}"
        );
        assert_eq!(
            ts_str, "1700000000000000000",
            "timestamp is not nanoseconds: {ts_str:?}"
        );
    }

    #[test]
    fn timestamp_is_not_milliseconds() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::from_pairs(&[]).unwrap();
        // 1_000 ms = 1 second = 1_000_000_000 ns; ms would be "1000", ns would be "1000000000"
        let ts = UNIX_EPOCH + Duration::from_millis(1_000);
        let event = MetricEvent::with_timestamp("up".to_string(), 1.0, labels, ts).unwrap();
        let output = encode_to_string(&enc, &event);
        let ts_str = output
            .trim_end_matches('\n')
            .split_whitespace()
            .last()
            .unwrap();
        assert_eq!(
            ts_str, "1000000000",
            "timestamp should be nanoseconds, not milliseconds: got {ts_str:?}"
        );
    }

    #[test]
    fn timestamp_does_not_contain_decimal_point() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::from_pairs(&[]).unwrap();
        let event = make_event("up", 1.0, labels, 1_234_567_890_123_456_789);
        let output = encode_to_string(&enc, &event);
        let ts_str = output
            .trim_end_matches('\n')
            .split_whitespace()
            .last()
            .unwrap();
        assert!(
            !ts_str.contains('.'),
            "timestamp must be an integer: {ts_str:?}"
        );
    }

    // --- Regression anchor: hardcoded expected byte strings ---

    #[test]
    fn regression_anchor_no_labels_exact_bytes() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::from_pairs(&[]).unwrap();
        // Timestamp: exactly 1_700_000_000 seconds = 1_700_000_000_000_000_000 ns
        let event = make_event(
            "http_requests_total",
            123.456,
            labels,
            1_700_000_000_000_000_000,
        );
        let mut buf = Vec::new();
        enc.encode_metric(&event, &mut buf).unwrap();
        assert_eq!(
            buf,
            b"http_requests_total value=123.456 1700000000000000000\n"
        );
    }

    #[test]
    fn regression_anchor_two_labels_exact_bytes() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::from_pairs(&[("hostname", "t0-a1"), ("zone", "eu1")]).unwrap();
        let event = make_event("interface_state", 1.0, labels, 1_700_000_000_000_000_000);
        let mut buf = Vec::new();
        enc.encode_metric(&event, &mut buf).unwrap();
        assert_eq!(
            buf,
            b"interface_state,hostname=t0-a1,zone=eu1 value=1 1700000000000000000\n"
        );
    }

    #[test]
    fn regression_anchor_custom_field_key_exact_bytes() {
        let enc = InfluxLineProtocol::new(Some("gauge".to_string()), None);
        let labels = Labels::from_pairs(&[("host", "srv1")]).unwrap();
        let event = make_event("cpu", 0.75, labels, 1_000_000_000_000_000_000);
        let mut buf = Vec::new();
        enc.encode_metric(&event, &mut buf).unwrap();
        assert_eq!(buf, b"cpu,host=srv1 gauge=0.75 1000000000000000000\n");
    }

    // --- Output format ---

    #[test]
    fn output_ends_with_newline() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::from_pairs(&[("k", "v")]).unwrap();
        let event = make_event("metric", 3.14, labels, 999_000_000);
        let output = encode_to_string(&enc, &event);
        assert!(
            output.ends_with('\n'),
            "output must end with newline: {output:?}"
        );
    }

    #[test]
    fn encode_appends_to_existing_buffer_content() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::from_pairs(&[]).unwrap();
        let event = make_event("up", 1.0, labels, 1_000_000_000);
        let mut buf = b"existing\n".to_vec();
        enc.encode_metric(&event, &mut buf).unwrap();
        let output = String::from_utf8(buf).unwrap();
        assert!(
            output.starts_with("existing\n"),
            "encoder must append, not overwrite: {output:?}"
        );
        assert!(
            output.ends_with("up value=1 1000000000\n"),
            "appended content missing: {output:?}"
        );
    }

    #[test]
    fn multiple_encodes_accumulate_in_buffer() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::from_pairs(&[]).unwrap();
        let event1 = make_event("up", 1.0, labels.clone(), 1_000_000_000);
        let event2 = make_event("down", 0.0, labels, 2_000_000_000);
        let mut buf = Vec::new();
        enc.encode_metric(&event1, &mut buf).unwrap();
        enc.encode_metric(&event2, &mut buf).unwrap();
        let output = String::from_utf8(buf).unwrap();
        let lines: Vec<&str> = output.lines().collect();
        assert_eq!(lines.len(), 2, "expected 2 lines: {output:?}");
        assert_eq!(lines[0], "up value=1 1000000000");
        assert_eq!(lines[1], "down value=0 2000000000");
    }

    // --- Pre-epoch timestamp error ---

    #[test]
    fn pre_epoch_timestamp_returns_encoder_error() {
        let before_epoch = UNIX_EPOCH - Duration::from_secs(1);
        let labels = Labels::from_pairs(&[]).unwrap();
        let event =
            MetricEvent::with_timestamp("up".to_string(), 1.0, labels, before_epoch).unwrap();
        let enc = InfluxLineProtocol::new(None, None);
        let mut buf = Vec::new();
        let result = enc.encode_metric(&event, &mut buf);
        assert!(
            matches!(result, Err(SondaError::Encoder(_))),
            "expected Encoder error for pre-epoch timestamp, got: {result:?}"
        );
    }

    // --- Send + Sync contract ---

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

    // --- Factory and EncoderConfig ---

    #[test]
    fn create_encoder_returns_working_influx_encoder_with_default_field_key() {
        let config = EncoderConfig::InfluxLineProtocol {
            field_key: None,
            precision: None,
        };
        let enc = create_encoder(&config).unwrap();
        let labels = Labels::from_pairs(&[]).unwrap();
        let ts = UNIX_EPOCH + Duration::from_nanos(1_000_000_000);
        let event = MetricEvent::with_timestamp("up".to_string(), 1.0, labels, ts).unwrap();
        let mut buf = Vec::new();
        enc.encode_metric(&event, &mut buf).unwrap();
        let output = String::from_utf8(buf).unwrap();
        assert_eq!(output, "up value=1 1000000000\n");
    }

    #[test]
    fn create_encoder_returns_working_influx_encoder_with_custom_field_key() {
        let config = EncoderConfig::InfluxLineProtocol {
            field_key: Some("count".to_string()),
            precision: None,
        };
        let enc = create_encoder(&config).unwrap();
        let labels = Labels::from_pairs(&[]).unwrap();
        let ts = UNIX_EPOCH + Duration::from_nanos(1_000_000_000);
        let event = MetricEvent::with_timestamp("up".to_string(), 5.0, labels, ts).unwrap();
        let mut buf = Vec::new();
        enc.encode_metric(&event, &mut buf).unwrap();
        let output = String::from_utf8(buf).unwrap();
        assert!(
            output.contains("count=5"),
            "custom field key 'count' not in factory-created encoder output: {output:?}"
        );
    }

    #[cfg(feature = "config")]
    #[test]
    fn encoder_config_deserialization_influx_lp_no_field_key() {
        let config: EncoderConfig =
            serde_yaml_ng::from_str("type: influx_lp\nfield_key: null").unwrap();
        assert!(matches!(
            config,
            EncoderConfig::InfluxLineProtocol {
                field_key: None,
                precision: None
            }
        ));
    }

    #[cfg(feature = "config")]
    #[test]
    fn encoder_config_deserialization_influx_lp_with_field_key() {
        let config: EncoderConfig =
            serde_yaml_ng::from_str("type: influx_lp\nfield_key: requests").unwrap();
        assert!(matches!(
            config,
            EncoderConfig::InfluxLineProtocol {
                field_key: Some(ref k), ..
            } if k == "requests"
        ));
    }

    // --- Precision: 2 limits decimal places in field value ---

    #[test]
    fn precision_two_limits_decimals_influx() {
        let enc = InfluxLineProtocol::new(None, Some(2));
        let labels = Labels::from_pairs(&[]).unwrap();
        let event = make_event("cpu", 99.60573, labels, 1_700_000_000_000_000_000);
        let output = encode_to_string(&enc, &event);
        assert_eq!(output, "cpu value=99.61 1700000000000000000\n");
    }

    #[test]
    fn precision_none_preserves_full_output_influx() {
        let enc = InfluxLineProtocol::new(None, None);
        let labels = Labels::from_pairs(&[]).unwrap();
        let event = make_event("cpu", 99.60573506572389, labels, 1_000_000_000);
        let output = encode_to_string(&enc, &event);
        assert!(
            output.contains("value=99.60573506572389"),
            "full precision must be preserved: {output:?}"
        );
    }

    #[test]
    fn precision_zero_influx() {
        let enc = InfluxLineProtocol::new(None, Some(0));
        let labels = Labels::from_pairs(&[]).unwrap();
        let event = make_event("up", 42.9, labels, 1_000_000_000);
        let output = encode_to_string(&enc, &event);
        assert!(
            output.contains("value=43"),
            "precision=0 should round: {output:?}"
        );
    }
}