dataexcept 0.1.0-beta.2

Structured errors and failure observability for data, ML, and data-engineering systems
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//! Framework-neutral observability boundary adapters.
//!
//! The adapters accept plain metadata and stable operation identifiers so
//! HTTP servers, workers, brokers, and workflow engines can integrate without
//! becoming runtime dependencies of `dataexcept`.

use std::collections::BTreeMap;

use thiserror::Error;

use crate::{
    InvalidOperationContext, OperationContext, TraceContextConflict, W3CTraceContext,
    trace_context_from_mapping,
};

const REQUEST_ID_HEADERS: [&str; 2] = ["x-request-id", "request-id"];
const CORRELATION_ID_HEADERS: [&str; 2] = ["x-correlation-id", "correlation-id"];
const JOB_ID_KEYS: [&str; 3] = ["job_id", "task_id", "id"];
const WORKER_CORRELATION_KEYS: [&str; 3] = ["correlation_id", "x-correlation-id", "correlation-id"];

/// Validation failure while constructing a boundary context.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum BoundaryContextError {
    /// A required text field was empty after trimming.
    #[error("{0} must not be empty")]
    EmptyField(&'static str),
    /// A text field contained a carriage return or newline.
    #[error("{0} must be single-line text")]
    MultilineField(&'static str),
    /// The HTTP method was not a valid token.
    #[error("method must be a valid HTTP method token")]
    InvalidHttpMethod,
    /// The HTTP route included a query string or fragment.
    #[error("route must be a route template without query or fragment")]
    InvalidRoute,
    /// The resulting operation context was invalid.
    #[error(transparent)]
    InvalidOperationContext(#[from] InvalidOperationContext),
    /// Incoming trace provenance conflicted with existing context.
    #[error(transparent)]
    TraceContextConflict(#[from] TraceContextConflict),
}

/// Framework-neutral HTTP request context.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HttpContext {
    operation_context: OperationContext,
    trace_context: Option<W3CTraceContext>,
}

impl HttpContext {
    /// Returns the normalized operation context.
    #[must_use]
    pub const fn operation_context(&self) -> &OperationContext {
        &self.operation_context
    }

    /// Returns propagated W3C trace context, when present.
    #[must_use]
    pub const fn trace_context(&self) -> Option<&W3CTraceContext> {
        self.trace_context.as_ref()
    }
}

/// Worker/task operation context.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorkerContext {
    operation_context: OperationContext,
    trace_context: Option<W3CTraceContext>,
    attempt: Option<u32>,
}

impl WorkerContext {
    /// Returns the normalized operation context.
    #[must_use]
    pub const fn operation_context(&self) -> &OperationContext {
        &self.operation_context
    }

    /// Returns propagated W3C trace context, when present.
    #[must_use]
    pub const fn trace_context(&self) -> Option<&W3CTraceContext> {
        self.trace_context.as_ref()
    }

    /// Returns the retry attempt number, when supplied.
    #[must_use]
    pub const fn attempt(&self) -> Option<u32> {
        self.attempt
    }
}

/// Stable broker boundary operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum BrokerOperation {
    /// Publishing a message.
    Publish,
    /// Consuming a message.
    Consume,
    /// Acknowledging a message.
    Acknowledge,
}

impl BrokerOperation {
    const fn as_str(self) -> &'static str {
        match self {
            Self::Publish => "publish",
            Self::Consume => "consume",
            Self::Acknowledge => "acknowledge",
        }
    }
}

/// Optional metadata for a broker boundary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct BrokerContextOptions<'a> {
    component: Option<&'a str>,
    correlation_id: Option<&'a str>,
    partition: Option<u32>,
    offset: Option<u64>,
    consumer_group: Option<&'a str>,
    message_id: Option<&'a str>,
}

impl<'a> BrokerContextOptions<'a> {
    /// Sets the logical component name.
    #[must_use]
    pub const fn component(mut self, value: &'a str) -> Self {
        self.component = Some(value);
        self
    }

    /// Sets the correlation identifier.
    #[must_use]
    pub const fn correlation_id(mut self, value: &'a str) -> Self {
        self.correlation_id = Some(value);
        self
    }

    /// Sets the partition number.
    #[must_use]
    pub const fn partition(mut self, value: u32) -> Self {
        self.partition = Some(value);
        self
    }

    /// Sets the broker offset.
    #[must_use]
    pub const fn offset(mut self, value: u64) -> Self {
        self.offset = Some(value);
        self
    }

    /// Sets the consumer-group identifier.
    #[must_use]
    pub const fn consumer_group(mut self, value: &'a str) -> Self {
        self.consumer_group = Some(value);
        self
    }

    /// Sets the message identifier.
    #[must_use]
    pub const fn message_id(mut self, value: &'a str) -> Self {
        self.message_id = Some(value);
        self
    }
}

/// Broker or stream-processing boundary context.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BrokerContext {
    operation_context: OperationContext,
    trace_context: Option<W3CTraceContext>,
    topic: String,
    partition: Option<u32>,
    offset: Option<u64>,
    consumer_group: Option<String>,
    message_id: Option<String>,
}

impl BrokerContext {
    /// Returns the normalized operation context.
    #[must_use]
    pub const fn operation_context(&self) -> &OperationContext {
        &self.operation_context
    }

    /// Returns propagated W3C trace context, when present.
    #[must_use]
    pub const fn trace_context(&self) -> Option<&W3CTraceContext> {
        self.trace_context.as_ref()
    }

    /// Returns the topic name.
    #[must_use]
    pub fn topic(&self) -> &str {
        &self.topic
    }

    /// Returns the partition, when supplied.
    #[must_use]
    pub const fn partition(&self) -> Option<u32> {
        self.partition
    }

    /// Returns the offset, when supplied.
    #[must_use]
    pub const fn offset(&self) -> Option<u64> {
        self.offset
    }

    /// Returns the consumer group, when supplied.
    #[must_use]
    pub fn consumer_group(&self) -> Option<&str> {
        self.consumer_group.as_deref()
    }

    /// Returns the message identifier, when supplied.
    #[must_use]
    pub fn message_id(&self) -> Option<&str> {
        self.message_id.as_deref()
    }
}

/// Optional metadata for an orchestrator boundary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct OrchestratorContextOptions<'a> {
    run_id: Option<&'a str>,
    step_run_id: Option<&'a str>,
    component: Option<&'a str>,
    correlation_id: Option<&'a str>,
    attempt: Option<u32>,
}

impl<'a> OrchestratorContextOptions<'a> {
    /// Sets the workflow-run identifier.
    #[must_use]
    pub const fn run_id(mut self, value: &'a str) -> Self {
        self.run_id = Some(value);
        self
    }

    /// Sets the step-run identifier.
    #[must_use]
    pub const fn step_run_id(mut self, value: &'a str) -> Self {
        self.step_run_id = Some(value);
        self
    }

    /// Sets the logical component name.
    #[must_use]
    pub const fn component(mut self, value: &'a str) -> Self {
        self.component = Some(value);
        self
    }

    /// Sets the correlation identifier.
    #[must_use]
    pub const fn correlation_id(mut self, value: &'a str) -> Self {
        self.correlation_id = Some(value);
        self
    }

    /// Sets the retry attempt number.
    #[must_use]
    pub const fn attempt(mut self, value: u32) -> Self {
        self.attempt = Some(value);
        self
    }
}

/// Workflow/orchestrator step context.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OrchestratorContext {
    operation_context: OperationContext,
    trace_context: Option<W3CTraceContext>,
    step_run_id: Option<String>,
    attempt: Option<u32>,
}

impl OrchestratorContext {
    /// Returns the normalized operation context.
    #[must_use]
    pub const fn operation_context(&self) -> &OperationContext {
        &self.operation_context
    }

    /// Returns propagated W3C trace context, when present.
    #[must_use]
    pub const fn trace_context(&self) -> Option<&W3CTraceContext> {
        self.trace_context.as_ref()
    }

    /// Returns the step-run identifier, when supplied.
    #[must_use]
    pub fn step_run_id(&self) -> Option<&str> {
        self.step_run_id.as_deref()
    }

    /// Returns the retry attempt number, when supplied.
    #[must_use]
    pub const fn attempt(&self) -> Option<u32> {
        self.attempt
    }
}

/// Builds HTTP observability context from plain headers and a route template.
///
/// `route` should be a low-cardinality route template such as `/users/{id}`,
/// not a raw request path with query parameters.
///
/// # Errors
///
/// Returns [`BoundaryContextError`] for an invalid method, route, component,
/// or conflicting W3C trace context.
pub fn http_context_from_request(
    method: &str,
    route: Option<&str>,
    headers: &BTreeMap<String, String>,
    component: Option<&str>,
) -> Result<HttpContext, BoundaryContextError> {
    let method = normalize_http_method(method)?;
    let operation = match route {
        Some(route) => format!("{method} {}", validate_route(route)?),
        None => method,
    };

    let request_id = first_metadata_value(headers, &REQUEST_ID_HEADERS);
    let correlation_id = first_metadata_value(headers, &CORRELATION_ID_HEADERS);
    let mut builder = OperationContext::builder()
        .system("http")
        .operation(operation);

    if let Some(component) = component {
        builder = builder.component(component);
    }
    if let Some(request_id) = request_id {
        builder = builder.request_id(request_id);
    }
    if let Some(correlation_id) = correlation_id {
        builder = builder.correlation_id(correlation_id);
    }

    let operation_context = builder.build()?;
    let trace_context = trace_from_metadata(headers);
    let operation_context = correlate_trace(operation_context, trace_context.as_ref())?;

    Ok(HttpContext {
        operation_context,
        trace_context,
    })
}

/// Builds worker observability context from stable task metadata.
///
/// Explicit job and correlation identifiers take precedence over metadata
/// fallbacks. Task arguments and payloads are intentionally excluded.
///
/// # Errors
///
/// Returns [`BoundaryContextError`] for invalid task metadata or conflicting
/// W3C trace context.
pub fn worker_context_from_task(
    task_name: &str,
    metadata: &BTreeMap<String, String>,
    job_id: Option<&str>,
    correlation_id: Option<&str>,
    component: Option<&str>,
    attempt: Option<u32>,
) -> Result<WorkerContext, BoundaryContextError> {
    let task_name = single_line(task_name, "task_name")?;
    let job_id = optional_single_line(job_id, "job_id")?
        .or_else(|| first_metadata_value(metadata, &JOB_ID_KEYS));
    let correlation_id = optional_single_line(correlation_id, "correlation_id")?
        .or_else(|| first_metadata_value(metadata, &WORKER_CORRELATION_KEYS));

    let mut builder = OperationContext::builder()
        .system("worker")
        .operation(task_name);
    if let Some(component) = component {
        builder = builder.component(component);
    }
    if let Some(job_id) = job_id {
        builder = builder.job_id(job_id);
    }
    if let Some(correlation_id) = correlation_id {
        builder = builder.correlation_id(correlation_id);
    }

    let operation_context = builder.build()?;
    let trace_context = trace_from_metadata(metadata);
    let operation_context = correlate_trace(operation_context, trace_context.as_ref())?;

    Ok(WorkerContext {
        operation_context,
        trace_context,
        attempt,
    })
}

/// Builds broker observability context without capturing message payloads.
///
/// Partition, offset, consumer-group, and message identifiers remain wrapper
/// metadata instead of becoming low-cardinality operation labels.
///
/// # Errors
///
/// Returns [`BoundaryContextError`] for invalid textual metadata or conflicting
/// W3C trace context.
pub fn broker_context_from_message(
    operation: BrokerOperation,
    topic: &str,
    metadata: &BTreeMap<String, String>,
    options: BrokerContextOptions<'_>,
) -> Result<BrokerContext, BoundaryContextError> {
    let BrokerContextOptions {
        component,
        correlation_id,
        partition,
        offset,
        consumer_group,
        message_id,
    } = options;
    let topic = single_line(topic, "topic")?;
    let correlation_id = optional_single_line(correlation_id, "correlation_id")?;
    let consumer_group = optional_single_line(consumer_group, "consumer_group")?;
    let message_id = optional_single_line(message_id, "message_id")?;
    let operation_name = format!("{} {topic}", operation.as_str());

    let mut builder = OperationContext::builder()
        .system("broker")
        .operation(operation_name);
    if let Some(component) = component {
        builder = builder.component(component);
    }
    if let Some(correlation_id) = correlation_id {
        builder = builder.correlation_id(correlation_id);
    }

    let operation_context = builder.build()?;
    let trace_context = trace_from_metadata(metadata);
    let operation_context = correlate_trace(operation_context, trace_context.as_ref())?;

    Ok(BrokerContext {
        operation_context,
        trace_context,
        topic,
        partition,
        offset,
        consumer_group,
        message_id,
    })
}

/// Builds workflow-step observability context from stable orchestrator metadata.
///
/// The operation name is the stable `workflow:step` pair. Run identifiers are
/// correlation metadata and are never folded into the operation name.
///
/// # Errors
///
/// Returns [`BoundaryContextError`] for invalid textual metadata or conflicting
/// W3C trace context.
pub fn orchestrator_context_from_step(
    workflow: &str,
    step: &str,
    metadata: &BTreeMap<String, String>,
    options: OrchestratorContextOptions<'_>,
) -> Result<OrchestratorContext, BoundaryContextError> {
    let OrchestratorContextOptions {
        run_id,
        step_run_id,
        component,
        correlation_id,
        attempt,
    } = options;
    let workflow = single_line(workflow, "workflow")?;
    let step = single_line(step, "step")?;
    let run_id = optional_single_line(run_id, "run_id")?;
    let step_run_id = optional_single_line(step_run_id, "step_run_id")?;
    let correlation_id = optional_single_line(correlation_id, "correlation_id")?;

    let mut builder = OperationContext::builder()
        .system("orchestrator")
        .operation(format!("{workflow}:{step}"));
    if let Some(component) = component {
        builder = builder.component(component);
    }
    if let Some(run_id) = run_id {
        builder = builder.job_id(run_id);
    }
    if let Some(correlation_id) = correlation_id {
        builder = builder.correlation_id(correlation_id);
    }

    let operation_context = builder.build()?;
    let trace_context = trace_from_metadata(metadata);
    let operation_context = correlate_trace(operation_context, trace_context.as_ref())?;

    Ok(OrchestratorContext {
        operation_context,
        trace_context,
        step_run_id,
        attempt,
    })
}

fn normalize_http_method(method: &str) -> Result<String, BoundaryContextError> {
    let method = method.trim().to_ascii_uppercase();
    if method.is_empty() || !method.bytes().all(is_http_token_byte) {
        return Err(BoundaryContextError::InvalidHttpMethod);
    }
    Ok(method)
}

const fn is_http_token_byte(byte: u8) -> bool {
    matches!(
        byte,
        b'A'..=b'Z'
            | b'0'..=b'9'
            | b'!'
            | b'#'
            | b'$'
            | b'%'
            | b'&'
            | b'\''
            | b'*'
            | b'+'
            | b'-'
            | b'.'
            | b'^'
            | b'_'
            | b'`'
            | b'|'
            | b'~'
    )
}

fn validate_route(route: &str) -> Result<String, BoundaryContextError> {
    let route = single_line(route, "route")?;
    if route.contains(['?', '#']) {
        return Err(BoundaryContextError::InvalidRoute);
    }
    Ok(route)
}

fn single_line(value: &str, field: &'static str) -> Result<String, BoundaryContextError> {
    let normalized = value.trim();
    if normalized.is_empty() {
        return Err(BoundaryContextError::EmptyField(field));
    }
    if value.contains('\r') || value.contains('\n') {
        return Err(BoundaryContextError::MultilineField(field));
    }
    Ok(normalized.to_owned())
}

fn optional_single_line(
    value: Option<&str>,
    field: &'static str,
) -> Result<Option<String>, BoundaryContextError> {
    value.map(|value| single_line(value, field)).transpose()
}

fn first_metadata_value(metadata: &BTreeMap<String, String>, names: &[&str]) -> Option<String> {
    names.iter().find_map(|name| {
        metadata.iter().find_map(|(key, value)| {
            if key.eq_ignore_ascii_case(name) {
                safe_metadata_text(value)
            } else {
                None
            }
        })
    })
}

fn safe_metadata_text(value: &str) -> Option<String> {
    let value = value.trim();
    if value.is_empty() || value.contains('\r') || value.contains('\n') {
        None
    } else {
        Some(value.to_owned())
    }
}

fn trace_from_metadata(metadata: &BTreeMap<String, String>) -> Option<W3CTraceContext> {
    trace_context_from_mapping(
        metadata
            .iter()
            .map(|(key, value)| (key.as_str(), value.as_str())),
    )
}

fn correlate_trace(
    operation_context: OperationContext,
    trace_context: Option<&W3CTraceContext>,
) -> Result<OperationContext, BoundaryContextError> {
    match trace_context {
        Some(trace_context) => Ok(trace_context.to_operation_context(Some(operation_context))?),
        None => Ok(operation_context),
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use super::{
        BrokerContextOptions, BrokerOperation, OrchestratorContextOptions,
        broker_context_from_message, http_context_from_request, orchestrator_context_from_step,
        worker_context_from_task,
    };

    const TRACEPARENT: &str = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01";

    #[test]
    fn http_uses_route_template_and_propagates_trace() {
        let headers = BTreeMap::from([
            ("X-Request-Id".to_owned(), "req-42".to_owned()),
            ("traceparent".to_owned(), TRACEPARENT.to_owned()),
        ]);

        let context =
            http_context_from_request("post", Some("/users/{id}"), &headers, Some("accounts"))
                .expect("HTTP context should be valid");

        assert_eq!(context.operation_context().system(), Some("http"));
        assert_eq!(
            context.operation_context().operation(),
            Some("POST /users/{id}")
        );
        assert_eq!(context.operation_context().request_id(), Some("req-42"));
        assert_eq!(
            context.operation_context().trace_id(),
            Some("4bf92f3577b34da6a3ce929d0e0e4736")
        );
        assert_eq!(context.operation_context().span_id(), None);
    }

    #[test]
    fn worker_prefers_explicit_identifiers() {
        let metadata = BTreeMap::from([
            ("task_id".to_owned(), "metadata-job".to_owned()),
            ("correlation_id".to_owned(), "metadata-corr".to_owned()),
        ]);

        let context = worker_context_from_task(
            "billing.settle_invoice",
            &metadata,
            Some("job-42"),
            Some("corr-9"),
            None,
            Some(2),
        )
        .expect("worker context should be valid");

        assert_eq!(context.operation_context().job_id(), Some("job-42"));
        assert_eq!(context.operation_context().correlation_id(), Some("corr-9"));
        assert_eq!(context.attempt(), Some(2));
    }

    #[test]
    fn broker_keeps_coordinates_out_of_operation_name() {
        let metadata = BTreeMap::new();
        let context = broker_context_from_message(
            BrokerOperation::Consume,
            "orders",
            &metadata,
            BrokerContextOptions::default()
                .component("billing")
                .correlation_id("corr-9")
                .partition(3)
                .offset(1042)
                .consumer_group("billing")
                .message_id("msg-7"),
        )
        .expect("broker context should be valid");

        assert_eq!(
            context.operation_context().operation(),
            Some("consume orders")
        );
        assert_eq!(context.partition(), Some(3));
        assert_eq!(context.offset(), Some(1042));
        assert_eq!(context.consumer_group(), Some("billing"));
        assert_eq!(context.message_id(), Some("msg-7"));
    }

    #[test]
    fn orchestrator_uses_stable_workflow_step_pair() {
        let metadata = BTreeMap::new();
        let context = orchestrator_context_from_step(
            "daily_etl",
            "load_customers",
            &metadata,
            OrchestratorContextOptions::default()
                .run_id("run-42")
                .step_run_id("step-run-7")
                .correlation_id("corr-9")
                .attempt(1),
        )
        .expect("orchestrator context should be valid");

        assert_eq!(
            context.operation_context().operation(),
            Some("daily_etl:load_customers")
        );
        assert_eq!(context.operation_context().job_id(), Some("run-42"));
        assert_eq!(context.step_run_id(), Some("step-run-7"));
        assert_eq!(context.attempt(), Some(1));
    }

    #[test]
    fn http_rejects_raw_query_routes() {
        let result = http_context_from_request("GET", Some("/users?id=42"), &BTreeMap::new(), None);

        assert!(result.is_err());
    }
}