data-courier 0.1.0-beta.4

Async Rust framework for composable data pipelines
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
//! Metrics core for Courier.
//!
//! Single-stop wiring for the OpenTelemetry metrics SDK:
//! - [`init_metrics`] builds an [`ObsHandle`] from the parsed
//!   `[observability]` config (or returns a no-op handle when no
//!   exporter is configured).
//! - [`NodeCtx`] holds a pre-built attribute set and counter/histogram
//!   handles bound to a single node id. Hot loops in `BasicTransform`
//!   and `ManagedSink` record metrics without rebuilding attributes.
//! - [`ObsHandle::shutdown`] force-flushes any installed providers so
//!   the last batch survives a graceful drain.
//!
//! The OTLP push path (PeriodicReader + grpc-tonic exporter) is
//! activated when `[observability.metrics].otlp_endpoint` is set.

use std::sync::Arc;
use std::time::Duration;

use anyhow::{Context, Result};
use opentelemetry::KeyValue;
use opentelemetry::metrics::{Counter, Histogram, InstrumentProvider, Meter, MeterProvider};
use opentelemetry_otlp::{MetricExporter, WithExportConfig};
use opentelemetry_sdk::Resource;
use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider};

use crate::config::{ObservabilityConfig, redact_secret};

/// Coarse-grained classification of a runtime node, used as a metric
/// attribute so dashboards can slice "all sinks" or "all transforms"
/// without per-name aggregation.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum NodeKind {
    Source,
    Transform,
    Sink,
    /// The implicit broadcast splitter inserted by `spawn_pipeline`
    /// when a pipeline has more than one sink.
    Splitter,
    /// One mpsc edge between two adjacent nodes. Reported by the
    /// channel-depth sampler; not used by transforms or sinks.
    Edge,
}

impl NodeKind {
    fn as_str(self) -> &'static str {
        match self {
            NodeKind::Source => "source",
            NodeKind::Transform => "transform",
            NodeKind::Sink => "sink",
            NodeKind::Splitter => "splitter",
            NodeKind::Edge => "edge",
        }
    }
}

/// Shared metrics provider plus pre-built instrument handles.
///
/// One `ObsHandle` is constructed per `Courier` and cloned into every
/// `NodeCtx`. Cloning is cheap — instruments are `Arc` internally.
#[derive(Clone)]
pub struct ObsHandle {
    inner: Arc<ObsHandleInner>,
}

struct ObsHandleInner {
    /// `Some` when the SDK provider is owned (real or in-memory test
    /// reader); `None` for the global noop fallback.
    provider: Option<SdkMeterProvider>,
    instruments: Instruments,
    log_keys: bool,
}

struct Instruments {
    processed: Counter<u64>,
    failed: Counter<u64>,
    filtered: Counter<u64>,
    retries: Counter<u64>,
    dead_lettered: Counter<u64>,
    stage_duration: Histogram<f64>,
    end_to_end_latency: Histogram<f64>,
    channel_capacity_used: Histogram<u64>,
}

#[derive(Debug)]
struct NoopInstrumentProvider;

impl InstrumentProvider for NoopInstrumentProvider {}

impl ObsHandle {
    /// Build an `ObsHandle` with no exporter installed. Counters and
    /// histograms still work (so callers don't branch on `Option`),
    /// but observations are dropped. This uses a private no-op meter,
    /// not `opentelemetry::global`, so embedded hosts with a global
    /// provider do not receive Courier metrics when Courier metrics
    /// are disabled.
    pub fn noop() -> Self {
        Self::noop_with_log_keys(false)
    }

    fn noop_with_log_keys(log_keys: bool) -> Self {
        let meter = Meter::new(Arc::new(NoopInstrumentProvider));
        Self::from_meter(meter, None, log_keys)
    }

    /// Whether this handle owns a concrete SDK provider. False means
    /// observations go to a private no-op meter and callers can skip
    /// auxiliary sampling tasks.
    pub(crate) fn is_enabled(&self) -> bool {
        self.inner.provider.is_some()
    }

    fn from_meter(meter: Meter, provider: Option<SdkMeterProvider>, log_keys: bool) -> Self {
        let instruments = Instruments {
            processed: meter
                .u64_counter("courier_envelopes_processed_total")
                .with_description("Envelopes successfully processed by a node.")
                .build(),
            failed: meter
                .u64_counter("courier_envelopes_failed_total")
                .with_description(
                    "Envelopes that triggered an error in a node, after retries are exhausted.",
                )
                .build(),
            filtered: meter
                .u64_counter("courier_envelopes_filtered_total")
                .with_description("Envelopes intentionally dropped by a transform (MapOne returned None).")
                .build(),
            retries: meter
                .u64_counter("courier_retries_total")
                .with_description("Retry attempts performed by a sink.")
                .build(),
            dead_lettered: meter
                .u64_counter("courier_dead_lettered_total")
                .with_description("Envelopes routed to a dead-letter sink after retries were exhausted.")
                .build(),
            stage_duration: meter
                .f64_histogram("courier_stage_duration_milliseconds")
                .with_description("Wall-clock time a node spent processing one envelope.")
                .with_unit("ms")
                .build(),
            end_to_end_latency: meter
                .f64_histogram("courier_end_to_end_latency_milliseconds")
                .with_description(
                    "Time from envelope creation (meta.timestamp_ms) to sink write completion.",
                )
                .with_unit("ms")
                .build(),
            channel_capacity_used: meter
                .u64_histogram("courier_channel_capacity_used")
                .with_description(
                    "In-flight items on a pipeline edge, sampled periodically (capacity - sender.capacity()).",
                )
                .build(),
        };
        Self {
            inner: Arc::new(ObsHandleInner {
                provider,
                instruments,
                log_keys,
            }),
        }
    }

    /// Force-flush pending observations without tearing down the provider.
    pub fn force_flush(&self) {
        if let Some(provider) = &self.inner.provider {
            let _ = provider.force_flush();
        }
    }

    /// Drain observations and tear down the installed provider.
    pub fn shutdown(&self) {
        if let Some(provider) = &self.inner.provider {
            let _ = provider.shutdown();
        }
    }
}

/// Per-node bundle of metric attributes and instrument handles.
///
/// Constructed once per node by `spawn_pipeline` (or by tests). The
/// hot-path code in `BasicTransform` / `ManagedSink` stores the
/// `NodeCtx` alongside its other state and calls `processed_add(1)`
/// without hashmap lookups.
#[derive(Clone)]
pub struct NodeCtx {
    handle: ObsHandle,
    attrs: Arc<[KeyValue]>,
    pipeline: Arc<str>,
    node_id: Arc<str>,
    node_kind: NodeKind,
    log_keys: bool,
}

impl NodeCtx {
    /// Build a `NodeCtx` for a single node. The attribute set is
    /// `pipeline`, `node_id`, `node_kind` — kept tight on purpose to
    /// avoid cardinality blow-ups (no `meta.key`, no payload labels).
    pub fn for_node(pipeline: &str, node_id: &str, node_kind: NodeKind, handle: ObsHandle) -> Self {
        let attrs: Arc<[KeyValue]> = Arc::from(
            [
                KeyValue::new("pipeline", pipeline.to_string()),
                KeyValue::new("node_id", node_id.to_string()),
                KeyValue::new("node_kind", node_kind.as_str()),
            ]
            .as_slice(),
        );
        let log_keys = handle.inner.log_keys;
        Self {
            handle,
            attrs,
            pipeline: Arc::from(pipeline),
            node_id: Arc::from(node_id),
            node_kind,
            log_keys,
        }
    }

    /// No-op context with empty attributes, backed by a private
    /// no-op meter. Used by tests that build pipelines manually and
    /// by the default state of `BasicTransform` / `ManagedSink` until
    /// `spawn_pipeline` attaches a real ctx.
    pub fn noop() -> Self {
        Self {
            handle: ObsHandle::noop(),
            attrs: Arc::from([] as [KeyValue; 0]),
            pipeline: Arc::from(""),
            node_id: Arc::from(""),
            node_kind: NodeKind::Transform,
            log_keys: false,
        }
    }

    pub fn attrs(&self) -> &[KeyValue] {
        &self.attrs
    }

    pub fn handle(&self) -> &ObsHandle {
        &self.handle
    }

    pub fn pipeline(&self) -> &str {
        &self.pipeline
    }

    pub fn node_id(&self) -> &str {
        &self.node_id
    }

    pub fn node_kind(&self) -> NodeKind {
        self.node_kind
    }

    pub fn node_kind_str(&self) -> &'static str {
        self.node_kind.as_str()
    }

    pub fn log_keys(&self) -> bool {
        self.log_keys
    }

    pub fn record_processed(&self) {
        self.handle.inner.instruments.processed.add(1, &self.attrs);
    }

    pub fn record_filtered(&self) {
        self.handle.inner.instruments.filtered.add(1, &self.attrs);
    }

    pub fn record_failed(&self) {
        self.handle.inner.instruments.failed.add(1, &self.attrs);
    }

    pub fn record_retry(&self) {
        self.handle.inner.instruments.retries.add(1, &self.attrs);
    }

    pub fn record_dead_letter(&self) {
        self.handle
            .inner
            .instruments
            .dead_lettered
            .add(1, &self.attrs);
    }

    pub fn record_stage_duration_ms(&self, ms: f64) {
        self.handle
            .inner
            .instruments
            .stage_duration
            .record(ms, &self.attrs);
    }

    pub fn record_end_to_end_latency_ms(&self, ms: f64) {
        self.handle
            .inner
            .instruments
            .end_to_end_latency
            .record(ms, &self.attrs);
    }

    pub fn record_channel_capacity_used(&self, used: u64) {
        self.handle
            .inner
            .instruments
            .channel_capacity_used
            .record(used, &self.attrs);
    }
}

/// Build an `ObsHandle` from the parsed `[observability]` config.
///
/// When `config.metrics.otlp_endpoint` is set, installs a
/// `PeriodicReader` over the OTLP/gRPC exporter pushing every
/// `export_interval_ms`. When unset, returns a no-op handle so the
/// rest of the runtime is unaware of the difference.
pub fn init_metrics(config: Option<&ObservabilityConfig>) -> Result<ObsHandle> {
    let Some(obs) = config else {
        return Ok(ObsHandle::noop());
    };
    let Some(endpoint) = super::configured_endpoint(obs.metrics.otlp_endpoint.as_deref()) else {
        return Ok(ObsHandle::noop_with_log_keys(obs.log_keys));
    };

    let exporter = MetricExporter::builder()
        .with_tonic()
        .with_endpoint(endpoint)
        .build()
        .with_context(|| {
            format!(
                "failed to build OTLP metric exporter for {}",
                redact_secret(endpoint)
            )
        })?;

    let reader = PeriodicReader::builder(exporter)
        .with_interval(Duration::from_millis(obs.metrics.export_interval_ms))
        .build();

    let resource = Resource::builder()
        .with_service_name(obs.service_name.clone())
        .build();

    let provider = SdkMeterProvider::builder()
        .with_reader(reader)
        .with_resource(resource)
        .build();

    let meter = provider.meter("courier");
    Ok(ObsHandle::from_meter(meter, Some(provider), obs.log_keys))
}

#[cfg(test)]
pub(crate) mod testing {
    //! Test helpers — an `InMemoryMetricExporter` paired with a
    //! `PeriodicReader` so unit tests can drive a small pipeline,
    //! force a flush, and assert exact counter / histogram values
    //! without touching a real OTLP collector.

    use std::time::Duration;

    use opentelemetry::metrics::MeterProvider;
    use opentelemetry_sdk::Resource;
    use opentelemetry_sdk::metrics::data::{AggregatedMetrics, MetricData};
    use opentelemetry_sdk::metrics::{InMemoryMetricExporter, PeriodicReader, SdkMeterProvider};

    use super::ObsHandle;

    /// Build an `ObsHandle` backed by an in-memory exporter and return
    /// the exporter so tests can pull collected metrics out of it.
    pub fn obs_handle_in_memory() -> (ObsHandle, InMemoryMetricExporter) {
        let exporter = InMemoryMetricExporter::default();
        // 1-hour interval — tests must call `provider.force_flush()`
        // explicitly so the timing is deterministic. The reader still
        // exists so the SDK has somewhere to push.
        let reader = PeriodicReader::builder(exporter.clone())
            .with_interval(Duration::from_secs(3600))
            .build();
        let provider = SdkMeterProvider::builder()
            .with_reader(reader)
            .with_resource(Resource::builder().with_service_name("test").build())
            .build();
        let meter = provider.meter("courier_test");
        let handle = ObsHandle::from_meter(meter, Some(provider), false);
        (handle, exporter)
    }

    /// Sum a `u64` counter across every collected resource metrics
    /// snapshot, restricted to data points whose attribute set
    /// matches `expected_attrs` (subset match — extra attrs ignored).
    pub fn counter_sum(
        exporter: &InMemoryMetricExporter,
        metric_name: &str,
        expected_attrs: &[(&str, &str)],
    ) -> u64 {
        let mut total = 0u64;
        for rm in exporter.get_finished_metrics().unwrap_or_default() {
            for sm in rm.scope_metrics() {
                for metric in sm.metrics() {
                    if metric.name() != metric_name {
                        continue;
                    }
                    if let AggregatedMetrics::U64(MetricData::Sum(sum)) = metric.data() {
                        for dp in sum.data_points() {
                            if attrs_match(dp.attributes(), expected_attrs) {
                                total += dp.value();
                            }
                        }
                    }
                }
            }
        }
        total
    }

    /// Number of histogram observations for `metric_name` matching
    /// `expected_attrs`. Useful for "did this run record N samples?"
    pub fn histogram_count(
        exporter: &InMemoryMetricExporter,
        metric_name: &str,
        expected_attrs: &[(&str, &str)],
    ) -> u64 {
        let mut total = 0u64;
        for rm in exporter.get_finished_metrics().unwrap_or_default() {
            for sm in rm.scope_metrics() {
                for metric in sm.metrics() {
                    if metric.name() != metric_name {
                        continue;
                    }
                    match metric.data() {
                        AggregatedMetrics::F64(MetricData::Histogram(h)) => {
                            for dp in h.data_points() {
                                if attrs_match(dp.attributes(), expected_attrs) {
                                    total += dp.count();
                                }
                            }
                        }
                        AggregatedMetrics::U64(MetricData::Histogram(h)) => {
                            for dp in h.data_points() {
                                if attrs_match(dp.attributes(), expected_attrs) {
                                    total += dp.count();
                                }
                            }
                        }
                        _ => {}
                    }
                }
            }
        }
        total
    }

    fn attrs_match<'a>(
        actual: impl Iterator<Item = &'a opentelemetry::KeyValue>,
        expected: &[(&str, &str)],
    ) -> bool {
        let actual: Vec<_> = actual.collect();
        expected.iter().all(|(k, v)| {
            actual
                .iter()
                .any(|kv| kv.key.as_str() == *k && kv.value.as_str() == *v)
        })
    }
}

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

    use opentelemetry::global;
    use opentelemetry_sdk::Resource;
    use opentelemetry_sdk::metrics::{InMemoryMetricExporter, PeriodicReader, SdkMeterProvider};

    use super::testing::counter_sum;
    use super::*;

    #[test]
    fn noop_handle_does_not_record_to_global_meter_provider() {
        let exporter = InMemoryMetricExporter::default();
        let reader = PeriodicReader::builder(exporter.clone())
            .with_interval(Duration::from_secs(3600))
            .build();
        let provider = SdkMeterProvider::builder()
            .with_reader(reader)
            .with_resource(Resource::builder().with_service_name("host").build())
            .build();

        global::set_meter_provider(provider.clone());

        let ctx = NodeCtx::noop();
        ctx.record_processed();
        ctx.record_failed();
        ctx.record_stage_duration_ms(1.0);

        let _ = provider.force_flush();

        assert_eq!(
            counter_sum(&exporter, "courier_envelopes_processed_total", &[]),
            0
        );
        assert_eq!(
            counter_sum(&exporter, "courier_envelopes_failed_total", &[]),
            0
        );
    }

    #[test]
    fn init_metrics_preserves_log_keys_when_exporter_is_disabled() {
        let obs = ObservabilityConfig {
            log_keys: true,
            ..ObservabilityConfig::default()
        };

        let handle = init_metrics(Some(&obs)).unwrap();
        assert!(!handle.is_enabled());

        let ctx = NodeCtx::for_node("p", "p/source", NodeKind::Source, handle);
        assert!(ctx.log_keys());
    }
}