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
use std::collections::HashMap;
use std::time::Duration;

use anyhow::{Context, Result, anyhow};
use async_trait::async_trait;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use reqwest::{Client, Method, Url};
use serde::Deserialize;
use serde_json::Value;

use crate::config::{parse_config, redact_secret};
use crate::envelope::Envelope;
use crate::observability::trace_context::{TRACEPARENT, TRACESTATE};
use crate::pipeline::ErrorPolicy;
use crate::retry::RetryPolicy;
use crate::sinks::{ManagedSink, Sink, WriteOne};

/// HTTP sink. Sends each envelope to a configured endpoint as a JSON body.
///
/// Returns `Err` for any non-2xx response, network error, or timeout — so
/// `ManagedSink` applies the configured retry / `on_error` policy uniformly.
pub struct ApiSink {
    id: String,
    url: String,
    method: Method,
    headers: HeaderMap,
    body_format: BodyFormat,
    client: Client,
}

/// Shape of the JSON body sent to the endpoint.
///
/// `Payload` (default) sends only `env.payload` — the common case for
/// webhook receivers that already know the schema. `Envelope` sends the
/// whole envelope (`meta` + `payload`) for receivers that need the
/// metadata as well.
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BodyFormat {
    #[default]
    Payload,
    Envelope,
}

impl ApiSink {
    pub fn new(
        id: impl Into<String>,
        url: impl Into<String>,
        method: Method,
        headers: HeaderMap,
        body_format: BodyFormat,
        timeout: Option<Duration>,
    ) -> Result<Self> {
        let mut builder = Client::builder();
        if let Some(t) = timeout {
            builder = builder.timeout(t);
        }
        let client = builder
            .build()
            .map_err(|e| anyhow!("failed to build HTTP client: {e}"))?;
        Ok(Self {
            id: id.into(),
            url: url.into(),
            method,
            headers,
            body_format,
            client,
        })
    }
}

#[async_trait]
impl WriteOne for ApiSink {
    fn id(&self) -> &str {
        &self.id
    }

    async fn write(&self, env: &Envelope) -> Result<()> {
        let body = match self.body_format {
            BodyFormat::Payload => &env.payload,
            BodyFormat::Envelope => &serde_json::to_value(env)?,
        };

        let mut headers = self.headers.clone();
        for key in [TRACEPARENT, TRACESTATE] {
            if let Some(value) = env.meta.headers.get(key) {
                let name = HeaderName::from_static(key);
                match HeaderValue::try_from(value) {
                    Ok(value) => {
                        headers.insert(name, value);
                    }
                    Err(_) => {
                        log::warn!("skipping invalid trace context header value for {key}");
                    }
                }
            }
        }

        let resp = self
            .client
            .request(self.method.clone(), &self.url)
            .headers(headers)
            .json(body)
            .send()
            .await
            .map_err(|e| {
                let e = e.without_url();
                anyhow!("HTTP request to {} failed: {e}", redact_secret(&self.url))
            })?;

        let status = resp.status();
        if !status.is_success() {
            // Surface the response body when present so failures are
            // diagnosable from logs / dead-letter entries.
            let body = resp.text().await.unwrap_or_default();
            return Err(anyhow!("HTTP error {status}: {body}"));
        }

        log::debug!(
            "[{}] {} {} -> {}",
            redact_secret(&self.id),
            self.method,
            redact_secret(&self.url),
            status
        );
        Ok(())
    }
}

#[derive(Debug, Deserialize)]
struct ApiSinkConfig {
    url: String,
    #[serde(default)]
    method: Option<String>,
    #[serde(default)]
    headers: HashMap<String, String>,
    #[serde(default)]
    body: BodyFormat,
    #[serde(default)]
    timeout_secs: Option<u64>,
}

/// Registry factory for [`ApiSink`]. Registered by
/// `courier::registry::register_builtin` under kind `"api"`.
///
/// Retry and error policy are managed centrally by the registry and applied
/// to every sink uniformly — no per-sink config needed.
pub fn api_sink_factory(
    id: &str,
    config: Value,
    on_error: ErrorPolicy,
    retry: Option<RetryPolicy>,
) -> Result<Box<dyn Sink>> {
    let config: ApiSinkConfig = parse_config("api", config)?;
    Url::parse(&config.url).with_context(|| {
        format!(
            "invalid config for component type 'api': invalid url '{}'",
            redact_secret(&config.url)
        )
    })?;

    let method = match config.method.as_deref() {
        None => Method::POST,
        Some(m) => m.parse::<Method>().map_err(|_| {
            anyhow!(
                "invalid config for component type 'api': unsupported HTTP method '{}'",
                redact_secret(m)
            )
        })?,
    };

    let mut headers = HeaderMap::with_capacity(config.headers.len());
    for (k, v) in config.headers {
        let name = HeaderName::try_from(&k).map_err(|_| {
            anyhow!("invalid config for component type 'api': invalid header name '{k}'")
        })?;
        let value = HeaderValue::try_from(&v).map_err(|_| {
            anyhow!("invalid config for component type 'api': invalid value for header '{k}'")
        })?;
        headers.insert(name, value);
    }

    let timeout = config.timeout_secs.map(Duration::from_secs);
    let api = ApiSink::new(id, config.url, method, headers, config.body, timeout)?;

    let mut sink = ManagedSink::new(api).with_error_policy(on_error);
    if let Some(policy) = retry {
        sink = sink.with_retry(policy);
    }
    Ok(Box::new(sink))
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use wiremock::matchers::{body_json, header, method as method_matcher, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn build_sink(
        url: String,
        method: Method,
        headers: HeaderMap,
        body_format: BodyFormat,
    ) -> ApiSink {
        ApiSink::new("api-sink", url, method, headers, body_format, None).unwrap()
    }

    fn closing_local_url(path: &str) -> String {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        std::thread::spawn(move || {
            if let Ok((stream, _)) = listener.accept() {
                drop(stream);
            }
        });
        format!("http://{addr}{path}")
    }

    #[test]
    fn factory_rejects_invalid_url() {
        let err = api_sink_factory(
            "api",
            json!({
                "url": "not a url"
            }),
            ErrorPolicy::Drop,
            None,
        )
        .err()
        .expect("expected invalid URL to fail");
        let msg = format!("{err:#}");
        assert!(
            msg.contains("invalid config for component type 'api'"),
            "{msg}"
        );
        assert!(msg.contains("invalid url"), "{msg}");
    }

    #[tokio::test]
    async fn posts_payload_as_json_by_default() {
        let server = MockServer::start().await;
        Mock::given(method_matcher("POST"))
            .and(path("/hook"))
            .and(body_json(json!({ "n": 7 })))
            .respond_with(ResponseTemplate::new(202))
            .expect(1)
            .mount(&server)
            .await;

        let sink = build_sink(
            format!("{}/hook", server.uri()),
            Method::POST,
            HeaderMap::new(),
            BodyFormat::Payload,
        );

        let env = Envelope::new("src", json!({ "n": 7 }));
        sink.write(&env).await.expect("write should succeed");
    }

    #[tokio::test]
    async fn sends_full_envelope_when_body_envelope() {
        let server = MockServer::start().await;
        Mock::given(method_matcher("POST"))
            .and(path("/hook"))
            // Match on a payload field nested under "payload" — proves the
            // envelope wrapper is present, not just the bare payload.
            .and(wiremock::matchers::body_partial_json(json!({
                "payload": { "n": 1 },
                "meta": { "source_id": "src" }
            })))
            .respond_with(ResponseTemplate::new(200))
            .expect(1)
            .mount(&server)
            .await;

        let sink = build_sink(
            format!("{}/hook", server.uri()),
            Method::POST,
            HeaderMap::new(),
            BodyFormat::Envelope,
        );

        let env = Envelope::new("src", json!({ "n": 1 }));
        sink.write(&env).await.unwrap();
    }

    #[tokio::test]
    async fn forwards_custom_headers_and_method() {
        let server = MockServer::start().await;
        Mock::given(method_matcher("PUT"))
            .and(path("/items/42"))
            .and(header("authorization", "Bearer token-123"))
            .and(header("x-courier-source", "courier"))
            .respond_with(ResponseTemplate::new(204))
            .expect(1)
            .mount(&server)
            .await;

        let mut headers = HeaderMap::new();
        headers.insert(
            "authorization",
            HeaderValue::from_static("Bearer token-123"),
        );
        headers.insert("x-courier-source", HeaderValue::from_static("courier"));

        let sink = build_sink(
            format!("{}/items/42", server.uri()),
            Method::PUT,
            headers,
            BodyFormat::Payload,
        );

        sink.write(&Envelope::new("src", json!({}))).await.unwrap();
    }

    #[tokio::test]
    async fn forwards_trace_context_headers() {
        let server = MockServer::start().await;
        let traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01";
        Mock::given(method_matcher("POST"))
            .and(path("/hook"))
            .and(header("traceparent", traceparent))
            .respond_with(ResponseTemplate::new(202))
            .expect(1)
            .mount(&server)
            .await;

        let sink = build_sink(
            format!("{}/hook", server.uri()),
            Method::POST,
            HeaderMap::new(),
            BodyFormat::Payload,
        );

        let mut env = Envelope::new("src", json!({}));
        env.meta
            .headers
            .insert(TRACEPARENT.to_string(), traceparent.to_string());
        sink.write(&env).await.unwrap();
    }

    #[tokio::test]
    async fn skips_invalid_trace_context_headers() {
        let server = MockServer::start().await;
        Mock::given(method_matcher("POST"))
            .and(path("/hook"))
            .and(body_json(json!({ "n": 1 })))
            .respond_with(ResponseTemplate::new(202))
            .expect(1)
            .mount(&server)
            .await;

        let sink = build_sink(
            format!("{}/hook", server.uri()),
            Method::POST,
            HeaderMap::new(),
            BodyFormat::Payload,
        );

        let mut env = Envelope::new("src", json!({ "n": 1 }));
        env.meta
            .headers
            .insert(TRACEPARENT.to_string(), "invalid\ntraceparent".to_string());
        env.meta
            .headers
            .insert(TRACESTATE.to_string(), "invalid\ntracestate".to_string());

        sink.write(&env)
            .await
            .expect("invalid trace headers should not fail delivery");
    }

    #[tokio::test]
    async fn non_2xx_response_is_an_error() {
        let server = MockServer::start().await;
        Mock::given(method_matcher("POST"))
            .and(path("/hook"))
            .respond_with(ResponseTemplate::new(500).set_body_string("boom"))
            .mount(&server)
            .await;

        let sink = build_sink(
            format!("{}/hook", server.uri()),
            Method::POST,
            HeaderMap::new(),
            BodyFormat::Payload,
        );

        let err = sink
            .write(&Envelope::new("src", json!({})))
            .await
            .expect_err("expected non-2xx to surface as an error");
        let msg = format!("{err:#}");
        assert!(msg.contains("500"), "{msg}");
        assert!(msg.contains("boom"), "{msg}");
    }

    #[tokio::test]
    async fn send_errors_do_not_repeat_url_from_reqwest_error() {
        let url = closing_local_url("/token-in-url");
        let sink = ApiSink::new(
            "api-sink",
            url.clone(),
            Method::POST,
            HeaderMap::new(),
            BodyFormat::Payload,
            Some(Duration::from_millis(500)),
        )
        .unwrap();

        let err = sink
            .write(&Envelope::new("src", json!({})))
            .await
            .expect_err("expected connection failure");
        let msg = format!("{err:#}");
        assert_eq!(msg.matches(&url).count(), 1, "{msg}");
    }

    // -----------------------------------------------------------------
    // Factory / config parsing
    // -----------------------------------------------------------------

    #[tokio::test]
    async fn factory_defaults_method_to_post() {
        let server = MockServer::start().await;
        Mock::given(method_matcher("POST"))
            .and(path("/hook"))
            .respond_with(ResponseTemplate::new(200))
            .expect(1)
            .mount(&server)
            .await;

        let sink = api_sink_factory(
            "api",
            json!({ "url": format!("{}/hook", server.uri()) }),
            ErrorPolicy::Drop,
            None,
        )
        .unwrap();

        let (tx, rx) = tokio::sync::mpsc::channel(1);
        let cancel = tokio_util::sync::CancellationToken::new();
        let handle = tokio::spawn(async move { sink.run(rx, cancel).await });

        tx.send(Envelope::new("src", json!({"hello": "world"})))
            .await
            .unwrap();
        drop(tx);
        handle.await.unwrap();
    }

    #[test]
    fn factory_rejects_invalid_method() {
        let err = api_sink_factory(
            "api",
            json!({ "url": "https://example.test/", "method": "FOO BAR" }),
            ErrorPolicy::Drop,
            None,
        )
        .err()
        .expect("expected invalid-method error");
        let msg = format!("{err:#}");
        assert!(msg.contains("unsupported HTTP method"), "{msg}");
    }

    #[test]
    fn factory_rejects_invalid_header_name() {
        let err = api_sink_factory(
            "api",
            json!({
                "url": "https://example.test/",
                "headers": { "bad header": "value" }
            }),
            ErrorPolicy::Drop,
            None,
        )
        .err()
        .expect("expected invalid-header error");
        let msg = format!("{err:#}");
        assert!(msg.contains("invalid header name"), "{msg}");
    }

    #[test]
    fn factory_reports_missing_url_with_uniform_prefix() {
        let err = api_sink_factory("api", json!({}), ErrorPolicy::Drop, None)
            .err()
            .expect("expected missing-url error");
        let msg = format!("{err:#}");
        assert!(
            msg.contains("invalid config for component type 'api'"),
            "{msg}"
        );
    }
}