obs-proto 0.2.0

Canonical obs/v1 protobuf schemas: ObsEnvelope, ObsBatch, options, built-in events.
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
//! Ergonomic supplements for proto-generated enums.
//!
//! Phase 3b (obs-migration spec § 4): obs-types is retired. The seven
//! hand-rolled Rust enums collapse into the proto-generated enums in
//! [`crate::obs::v1`]. buffa-codegen doesn't yet emit the full
//! ergonomic surface (`Ord`, `FromStr` with aliases, short-name
//! constants, domain helpers like [`Severity::otlp_number`]), so this
//! module adds them as hand-written supplements.
//!
//! Each enum gains:
//!
//! - **Short-name const aliases** — `Severity::Info`, `Tier::Log`, `Cardinality::High` —
//!   non-wire-breaking re-exports of the canonical SCREAM_CASE variants. Keeps call sites compiling
//!   without the long `SEVERITY_INFO` form every time.
//! - **Ordering** — `PartialOrd + Ord` via discriminant order so `Severity::Info < Severity::Warn`
//!   works.
//! - **Default** — already emitted by buffa-codegen (variant 0).
//! - **Serde** — `Serialize`/`Deserialize` by short lowercased name (`"info"`, `"warn"`) to match
//!   `obs.yaml` config.
//! - **FromStr** — accepts short name + proto name + aliases (`"warning"` → `Warn`, `"err"` →
//!   `Error`). Errors via [`UnknownVariant`].
//! - **Domain methods** — `as_str`, `otlp_number`, `cap`, `is_label_compatible`,
//!   `is_envelope_lifted`, etc.
//!
//! When buffa 0.6 lands with `generate_rich`, this file shrinks to
//! whatever the codegen doesn't cover (likely just serde config
//! naming).

use std::str::FromStr;

use crate::obs::v1::{
    Cardinality, Classification, FieldKind, MetricKind, SamplingReason, Severity, Tier,
};

/// Error returned by `FromStr` parsers when an enum variant isn't
/// recognised. Uniform shape across every enum in this module so
/// callers can match on one type.
#[derive(Debug, thiserror::Error)]
#[error("unknown {kind} variant: {value:?}")]
pub struct UnknownVariant {
    /// Enum name (e.g. `"Severity"`).
    pub kind: &'static str,
    /// Unrecognised input.
    pub value: String,
}

// ============================================================================
// Tier
// ============================================================================

#[allow(non_upper_case_globals)]
impl Tier {
    /// Short-name alias for [`Self::TIER_UNSPECIFIED`].
    pub const Unspecified: Self = Self::TIER_UNSPECIFIED;
    /// Short-name alias for [`Self::TIER_LOG`].
    pub const Log: Self = Self::TIER_LOG;
    /// Short-name alias for [`Self::TIER_METRIC`].
    pub const Metric: Self = Self::TIER_METRIC;
    /// Short-name alias for [`Self::TIER_TRACE`].
    pub const Trace: Self = Self::TIER_TRACE;
    /// Short-name alias for [`Self::TIER_AUDIT`].
    pub const Audit: Self = Self::TIER_AUDIT;

    /// Stable string label; used by sinks (`labels["tier"]`) and the
    /// CLI when rendering.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::TIER_LOG => "log",
            Self::TIER_METRIC => "metric",
            Self::TIER_TRACE => "trace",
            Self::TIER_AUDIT => "audit",
            _ => "unspecified",
        }
    }
}

impl Ord for Tier {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (*self as i32).cmp(&(*other as i32))
    }
}

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

impl FromStr for Tier {
    type Err = UnknownVariant;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_ascii_lowercase().as_str() {
            "log" | "tier_log" => Ok(Self::TIER_LOG),
            "metric" | "tier_metric" => Ok(Self::TIER_METRIC),
            "trace" | "tier_trace" => Ok(Self::TIER_TRACE),
            "audit" | "tier_audit" => Ok(Self::TIER_AUDIT),
            "unspecified" | "tier_unspecified" => Ok(Self::TIER_UNSPECIFIED),
            _ => Err(UnknownVariant {
                kind: "Tier",
                value: s.to_string(),
            }),
        }
    }
}

impl serde::Serialize for Tier {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(self.as_str())
    }
}

impl<'de> serde::Deserialize<'de> for Tier {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let s = <std::string::String as serde::Deserialize>::deserialize(d)?;
        Self::from_str(&s).map_err(serde::de::Error::custom)
    }
}

// ============================================================================
// Severity
// ============================================================================

#[allow(non_upper_case_globals)]
impl Severity {
    /// Short-name alias for [`Self::SEVERITY_UNSPECIFIED`].
    pub const Unspecified: Self = Self::SEVERITY_UNSPECIFIED;
    /// Short-name alias for [`Self::SEVERITY_TRACE`].
    pub const Trace: Self = Self::SEVERITY_TRACE;
    /// Short-name alias for [`Self::SEVERITY_DEBUG`].
    pub const Debug: Self = Self::SEVERITY_DEBUG;
    /// Short-name alias for [`Self::SEVERITY_INFO`].
    pub const Info: Self = Self::SEVERITY_INFO;
    /// Short-name alias for [`Self::SEVERITY_WARN`].
    pub const Warn: Self = Self::SEVERITY_WARN;
    /// Short-name alias for [`Self::SEVERITY_ERROR`].
    pub const Error: Self = Self::SEVERITY_ERROR;
    /// Short-name alias for [`Self::SEVERITY_FATAL`].
    pub const Fatal: Self = Self::SEVERITY_FATAL;

    /// Stable string label used in label values and CLI rendering.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::SEVERITY_TRACE => "trace",
            Self::SEVERITY_DEBUG => "debug",
            Self::SEVERITY_INFO => "info",
            Self::SEVERITY_WARN => "warn",
            Self::SEVERITY_ERROR => "error",
            Self::SEVERITY_FATAL => "fatal",
            _ => "unspecified",
        }
    }

    /// Map to OTLP `SeverityNumber` (1..=24 with 4 buckets per band).
    #[must_use]
    pub const fn otlp_number(self) -> i32 {
        match self {
            Self::SEVERITY_TRACE => 1,
            Self::SEVERITY_DEBUG => 5,
            Self::SEVERITY_INFO => 9,
            Self::SEVERITY_WARN => 13,
            Self::SEVERITY_ERROR => 17,
            Self::SEVERITY_FATAL => 21,
            _ => 0,
        }
    }
}

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

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

impl FromStr for Severity {
    type Err = UnknownVariant;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_ascii_lowercase().as_str() {
            "trace" | "severity_trace" => Ok(Self::SEVERITY_TRACE),
            "debug" | "severity_debug" => Ok(Self::SEVERITY_DEBUG),
            "info" | "severity_info" => Ok(Self::SEVERITY_INFO),
            "warn" | "warning" | "severity_warn" => Ok(Self::SEVERITY_WARN),
            "error" | "err" | "severity_error" => Ok(Self::SEVERITY_ERROR),
            "fatal" | "severity_fatal" => Ok(Self::SEVERITY_FATAL),
            "unspecified" | "severity_unspecified" => Ok(Self::SEVERITY_UNSPECIFIED),
            _ => Err(UnknownVariant {
                kind: "Severity",
                value: s.to_string(),
            }),
        }
    }
}

impl serde::Serialize for Severity {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(self.as_str())
    }
}

impl<'de> serde::Deserialize<'de> for Severity {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let s = <std::string::String as serde::Deserialize>::deserialize(d)?;
        Self::from_str(&s).map_err(serde::de::Error::custom)
    }
}

// ============================================================================
// FieldKind
// ============================================================================

#[allow(non_upper_case_globals)]
impl FieldKind {
    /// Short-name alias for `FIELD_KIND_UNSPECIFIED`.
    pub const Unspecified: Self = Self::FIELD_KIND_UNSPECIFIED;
    /// Short-name alias for `LABEL`.
    pub const Label: Self = Self::LABEL;
    /// Short-name alias for `ATTRIBUTE`.
    pub const Attribute: Self = Self::ATTRIBUTE;
    /// Short-name alias for `MEASUREMENT`.
    pub const Measurement: Self = Self::MEASUREMENT;
    /// Short-name alias for `TRACE_ID`.
    pub const TraceId: Self = Self::TRACE_ID;
    /// Short-name alias for `SPAN_ID`.
    pub const SpanId: Self = Self::SPAN_ID;
    /// Short-name alias for `PARENT_SPAN_ID`.
    pub const ParentSpanId: Self = Self::PARENT_SPAN_ID;
    /// Short-name alias for `TIMESTAMP_NS`.
    pub const TimestampNs: Self = Self::TIMESTAMP_NS;
    /// Short-name alias for `DURATION_NS`.
    pub const DurationNs: Self = Self::DURATION_NS;
    /// Short-name alias for `FORENSIC`.
    pub const Forensic: Self = Self::FORENSIC;

    /// Stable string label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::LABEL => "label",
            Self::ATTRIBUTE => "attribute",
            Self::MEASUREMENT => "measurement",
            Self::TRACE_ID => "trace_id",
            Self::SPAN_ID => "span_id",
            Self::PARENT_SPAN_ID => "parent_span_id",
            Self::TIMESTAMP_NS => "timestamp_ns",
            Self::DURATION_NS => "duration_ns",
            Self::FORENSIC => "forensic",
            _ => "unspecified",
        }
    }

    /// True if a value of this kind is lifted from the typed payload
    /// to a dedicated envelope slot.
    #[must_use]
    pub const fn is_envelope_lifted(self) -> bool {
        matches!(
            self,
            Self::TRACE_ID | Self::SPAN_ID | Self::PARENT_SPAN_ID | Self::TIMESTAMP_NS
        )
    }
}

impl Ord for FieldKind {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (*self as i32).cmp(&(*other as i32))
    }
}

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

impl FromStr for FieldKind {
    type Err = UnknownVariant;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_ascii_lowercase().as_str() {
            "label" => Ok(Self::LABEL),
            "attribute" => Ok(Self::ATTRIBUTE),
            "measurement" => Ok(Self::MEASUREMENT),
            "trace_id" => Ok(Self::TRACE_ID),
            "span_id" => Ok(Self::SPAN_ID),
            "parent_span_id" => Ok(Self::PARENT_SPAN_ID),
            "timestamp_ns" => Ok(Self::TIMESTAMP_NS),
            "duration_ns" => Ok(Self::DURATION_NS),
            "forensic" => Ok(Self::FORENSIC),
            "unspecified" | "field_kind_unspecified" => Ok(Self::FIELD_KIND_UNSPECIFIED),
            _ => Err(UnknownVariant {
                kind: "FieldKind",
                value: s.to_string(),
            }),
        }
    }
}

impl serde::Serialize for FieldKind {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(self.as_str())
    }
}

impl<'de> serde::Deserialize<'de> for FieldKind {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let s = <std::string::String as serde::Deserialize>::deserialize(d)?;
        Self::from_str(&s).map_err(serde::de::Error::custom)
    }
}

// ============================================================================
// Cardinality
// ============================================================================

#[allow(non_upper_case_globals)]
impl Cardinality {
    /// Short-name alias for `CARDINALITY_UNSPECIFIED`.
    pub const Unspecified: Self = Self::CARDINALITY_UNSPECIFIED;
    /// Short-name alias for `LOW`.
    pub const Low: Self = Self::LOW;
    /// Short-name alias for `MEDIUM`.
    pub const Medium: Self = Self::MEDIUM;
    /// Short-name alias for `HIGH`.
    pub const High: Self = Self::HIGH;
    /// Short-name alias for `UNBOUNDED`.
    pub const Unbounded: Self = Self::UNBOUNDED;

    /// Stable string label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::LOW => "low",
            Self::MEDIUM => "medium",
            Self::HIGH => "high",
            Self::UNBOUNDED => "unbounded",
            _ => "unspecified",
        }
    }

    /// Numeric cap: the maximum distinct value count permitted at this
    /// level. Returns [`u64::MAX`] for `Unbounded`.
    #[must_use]
    pub const fn cap(self) -> u64 {
        match self {
            Self::LOW => 10,
            Self::MEDIUM => 10_000,
            Self::HIGH => 1_000_000,
            Self::UNBOUNDED => u64::MAX,
            _ => 0,
        }
    }

    /// True if this cardinality is permitted on a `FieldKind::Label`
    /// field.
    #[must_use]
    pub const fn is_label_compatible(self) -> bool {
        matches!(self, Self::LOW | Self::MEDIUM)
    }

    /// True if this cardinality is permitted on a
    /// `FieldKind::Measurement` field.
    #[must_use]
    pub const fn is_measurement_compatible(self) -> bool {
        !matches!(self, Self::UNBOUNDED)
    }
}

impl Ord for Cardinality {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (*self as i32).cmp(&(*other as i32))
    }
}

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

impl FromStr for Cardinality {
    type Err = UnknownVariant;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_ascii_lowercase().as_str() {
            "low" => Ok(Self::LOW),
            "medium" => Ok(Self::MEDIUM),
            "high" => Ok(Self::HIGH),
            "unbounded" => Ok(Self::UNBOUNDED),
            "unspecified" | "cardinality_unspecified" => Ok(Self::CARDINALITY_UNSPECIFIED),
            _ => Err(UnknownVariant {
                kind: "Cardinality",
                value: s.to_string(),
            }),
        }
    }
}

impl serde::Serialize for Cardinality {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(self.as_str())
    }
}

impl<'de> serde::Deserialize<'de> for Cardinality {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let s = <std::string::String as serde::Deserialize>::deserialize(d)?;
        Self::from_str(&s).map_err(serde::de::Error::custom)
    }
}

// ============================================================================
// Classification
// ============================================================================

#[allow(non_upper_case_globals)]
impl Classification {
    /// Short-name alias for `CLASSIFICATION_UNSPECIFIED`.
    pub const Unspecified: Self = Self::CLASSIFICATION_UNSPECIFIED;
    /// Short-name alias for `INTERNAL`.
    pub const Internal: Self = Self::INTERNAL;
    /// Short-name alias for `PII`.
    pub const Pii: Self = Self::PII;
    /// Short-name alias for `SECRET`.
    pub const Secret: Self = Self::SECRET;

    /// Stable string label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::INTERNAL => "internal",
            Self::PII => "pii",
            Self::SECRET => "secret",
            _ => "unspecified",
        }
    }
}

impl Ord for Classification {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (*self as i32).cmp(&(*other as i32))
    }
}

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

impl FromStr for Classification {
    type Err = UnknownVariant;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_ascii_lowercase().as_str() {
            "internal" => Ok(Self::INTERNAL),
            "pii" => Ok(Self::PII),
            "secret" => Ok(Self::SECRET),
            "unspecified" | "classification_unspecified" => Ok(Self::CLASSIFICATION_UNSPECIFIED),
            _ => Err(UnknownVariant {
                kind: "Classification",
                value: s.to_string(),
            }),
        }
    }
}

impl serde::Serialize for Classification {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(self.as_str())
    }
}

impl<'de> serde::Deserialize<'de> for Classification {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let s = <std::string::String as serde::Deserialize>::deserialize(d)?;
        Self::from_str(&s).map_err(serde::de::Error::custom)
    }
}

// ============================================================================
// MetricKind
// ============================================================================

#[allow(non_upper_case_globals)]
impl MetricKind {
    /// Short-name alias for `METRIC_KIND_UNSPECIFIED`.
    pub const Unspecified: Self = Self::METRIC_KIND_UNSPECIFIED;
    /// Short-name alias for `METRIC_KIND_COUNTER`.
    pub const Counter: Self = Self::METRIC_KIND_COUNTER;
    /// Short-name alias for `METRIC_KIND_GAUGE`.
    pub const Gauge: Self = Self::METRIC_KIND_GAUGE;
    /// Short-name alias for `METRIC_KIND_HISTOGRAM`.
    pub const Histogram: Self = Self::METRIC_KIND_HISTOGRAM;

    /// Stable string label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::METRIC_KIND_COUNTER => "counter",
            Self::METRIC_KIND_GAUGE => "gauge",
            Self::METRIC_KIND_HISTOGRAM => "histogram",
            _ => "unspecified",
        }
    }
}

impl Ord for MetricKind {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (*self as i32).cmp(&(*other as i32))
    }
}

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

impl FromStr for MetricKind {
    type Err = UnknownVariant;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_ascii_lowercase().as_str() {
            "counter" | "metric_kind_counter" => Ok(Self::METRIC_KIND_COUNTER),
            "gauge" | "metric_kind_gauge" => Ok(Self::METRIC_KIND_GAUGE),
            "histogram" | "metric_kind_histogram" => Ok(Self::METRIC_KIND_HISTOGRAM),
            "unspecified" | "metric_kind_unspecified" => Ok(Self::METRIC_KIND_UNSPECIFIED),
            _ => Err(UnknownVariant {
                kind: "MetricKind",
                value: s.to_string(),
            }),
        }
    }
}

impl serde::Serialize for MetricKind {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(self.as_str())
    }
}

impl<'de> serde::Deserialize<'de> for MetricKind {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let s = <std::string::String as serde::Deserialize>::deserialize(d)?;
        Self::from_str(&s).map_err(serde::de::Error::custom)
    }
}

// ============================================================================
// SamplingReason
// ============================================================================

#[allow(non_upper_case_globals)]
impl SamplingReason {
    /// Short-name alias for `SAMPLING_REASON_UNSPECIFIED`.
    pub const Unspecified: Self = Self::SAMPLING_REASON_UNSPECIFIED;
    /// Short-name alias for `SAMPLING_REASON_HEAD_RATE`.
    pub const HeadRate: Self = Self::SAMPLING_REASON_HEAD_RATE;
    /// Short-name alias for `SAMPLING_REASON_TAIL_ERROR`.
    pub const TailError: Self = Self::SAMPLING_REASON_TAIL_ERROR;
    /// Short-name alias for `SAMPLING_REASON_SLOW`.
    pub const Slow: Self = Self::SAMPLING_REASON_SLOW;
    /// Short-name alias for `SAMPLING_REASON_FORENSIC`.
    pub const Forensic: Self = Self::SAMPLING_REASON_FORENSIC;
    /// Short-name alias for `SAMPLING_REASON_AUDIT`.
    pub const Audit: Self = Self::SAMPLING_REASON_AUDIT;
    /// Short-name alias for `SAMPLING_REASON_RUNTIME`.
    pub const Runtime: Self = Self::SAMPLING_REASON_RUNTIME;
    /// Short-name alias for `SAMPLING_REASON_OVERRIDE`.
    pub const Override: Self = Self::SAMPLING_REASON_OVERRIDE;

    /// Stable string label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::SAMPLING_REASON_HEAD_RATE => "head_rate",
            Self::SAMPLING_REASON_TAIL_ERROR => "tail_error",
            Self::SAMPLING_REASON_SLOW => "slow",
            Self::SAMPLING_REASON_FORENSIC => "forensic",
            Self::SAMPLING_REASON_AUDIT => "audit",
            Self::SAMPLING_REASON_RUNTIME => "runtime",
            Self::SAMPLING_REASON_OVERRIDE => "override",
            _ => "unspecified",
        }
    }
}

impl Ord for SamplingReason {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (*self as i32).cmp(&(*other as i32))
    }
}

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

impl FromStr for SamplingReason {
    type Err = UnknownVariant;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_ascii_lowercase().as_str() {
            "head_rate" => Ok(Self::SAMPLING_REASON_HEAD_RATE),
            "tail_error" => Ok(Self::SAMPLING_REASON_TAIL_ERROR),
            "slow" => Ok(Self::SAMPLING_REASON_SLOW),
            "forensic" => Ok(Self::SAMPLING_REASON_FORENSIC),
            "audit" => Ok(Self::SAMPLING_REASON_AUDIT),
            "runtime" => Ok(Self::SAMPLING_REASON_RUNTIME),
            "override" => Ok(Self::SAMPLING_REASON_OVERRIDE),
            "unspecified" | "sampling_reason_unspecified" => Ok(Self::SAMPLING_REASON_UNSPECIFIED),
            _ => Err(UnknownVariant {
                kind: "SamplingReason",
                value: s.to_string(),
            }),
        }
    }
}

impl serde::Serialize for SamplingReason {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(self.as_str())
    }
}

impl<'de> serde::Deserialize<'de> for SamplingReason {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let s = <std::string::String as serde::Deserialize>::deserialize(d)?;
        Self::from_str(&s).map_err(serde::de::Error::custom)
    }
}

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

    #[test]
    fn test_severity_short_name_aliases() {
        assert_eq!(Severity::Info, Severity::SEVERITY_INFO);
        assert_eq!(Severity::Warn, Severity::SEVERITY_WARN);
    }

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

    #[test]
    fn test_severity_otlp_number() {
        assert_eq!(Severity::Info.otlp_number(), 9);
        assert_eq!(Severity::Fatal.otlp_number(), 21);
    }

    #[test]
    fn test_severity_from_str_aliases() {
        assert_eq!("warning".parse::<Severity>().unwrap(), Severity::Warn);
        assert_eq!("err".parse::<Severity>().unwrap(), Severity::Error);
        assert_eq!("INFO".parse::<Severity>().unwrap(), Severity::Info);
        assert_eq!("SEVERITY_WARN".parse::<Severity>().unwrap(), Severity::Warn,);
    }

    #[test]
    fn test_tier_as_str_and_from_str() {
        assert_eq!(Tier::Log.as_str(), "log");
        assert_eq!("AUDIT".parse::<Tier>().unwrap(), Tier::Audit);
    }

    #[test]
    fn test_cardinality_caps_and_compat() {
        assert_eq!(Cardinality::Low.cap(), 10);
        assert_eq!(Cardinality::Medium.cap(), 10_000);
        assert_eq!(Cardinality::High.cap(), 1_000_000);
        assert_eq!(Cardinality::Unbounded.cap(), u64::MAX);
        assert!(Cardinality::Low.is_label_compatible());
        assert!(!Cardinality::High.is_label_compatible());
    }

    #[test]
    fn test_field_kind_envelope_lifted() {
        assert!(FieldKind::TraceId.is_envelope_lifted());
        assert!(FieldKind::SpanId.is_envelope_lifted());
        assert!(!FieldKind::Label.is_envelope_lifted());
    }

    #[test]
    fn test_serde_roundtrip_via_short_name() {
        let json = serde_json::to_string(&Severity::Info).unwrap();
        assert_eq!(json, "\"info\"");
        let back: Severity = serde_json::from_str(&json).unwrap();
        assert_eq!(back, Severity::Info);
    }
}