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
use anyhow::{Result, bail};
use async_trait::async_trait;
use axum::Router;
use axum::body::Bytes;
use axum::extract::State;
use axum::http::{HeaderMap, StatusCode};
use axum::response::{IntoResponse, Response};
use axum::routing::post;
use serde::Deserialize;
use serde_json::Value;
use std::net::SocketAddr;
use tokio::net::TcpListener;
use tokio::sync::mpsc::Sender;
use tokio_util::sync::CancellationToken;

use crate::config::{parse_config, redact_secret};
use crate::envelope::Envelope;
use crate::observability::trace_context::{TRACEPARENT, TRACESTATE};
use crate::observability::{NodeCtx, SourceCtx};
use crate::retry::RetryPolicy;
use crate::sources::Source;

/// Accepts HTTP webhook requests and emits each valid JSON request body as
/// an envelope payload. The request is acknowledged only after the envelope
/// has been sent to the pipeline channel, so normal mpsc backpressure applies
/// to incoming HTTP traffic.
pub struct HttpWebhookSource {
    id: String,
    bind: SocketAddr,
    path: String,
    source_ctx: SourceCtx,
}

impl HttpWebhookSource {
    pub fn new(id: impl Into<String>, bind: SocketAddr, path: impl Into<String>) -> Self {
        let id = id.into();
        Self {
            source_ctx: SourceCtx::new(&id),
            id,
            bind,
            path: path.into(),
        }
    }
}

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

    fn set_node_ctx(&mut self, ctx: NodeCtx) {
        self.source_ctx = SourceCtx::from_node_ctx(ctx);
    }

    async fn run(self: Box<Self>, tx: Sender<Envelope>, cancel: CancellationToken) {
        let state = WebhookState {
            source_id: self.id.clone(),
            source_ctx: self.source_ctx.clone(),
            tx,
            cancel: cancel.clone(),
        };
        let app = Router::new()
            .route(&self.path, post(handle_webhook))
            .fallback(not_found)
            .with_state(state);

        let listener = match TcpListener::bind(self.bind).await {
            Ok(listener) => listener,
            Err(e) => {
                log::error!(
                    "[{}] failed to bind {}: {e}",
                    redact_secret(&self.id),
                    redact_secret(&self.bind.to_string())
                );
                return;
            }
        };

        let local_addr = listener.local_addr().unwrap_or(self.bind);
        log::info!(
            "[{}] listening for POST {} on {}",
            redact_secret(&self.id),
            redact_secret(&self.path),
            local_addr
        );

        if let Err(e) = axum::serve(listener, app)
            .with_graceful_shutdown(cancel.cancelled_owned())
            .await
        {
            log::error!("[{}] webhook server failed: {e}", redact_secret(&self.id));
        }
    }
}

#[derive(Clone)]
struct WebhookState {
    source_id: String,
    source_ctx: SourceCtx,
    tx: Sender<Envelope>,
    cancel: CancellationToken,
}

async fn handle_webhook(
    State(state): State<WebhookState>,
    headers: HeaderMap,
    body: Bytes,
) -> Response {
    let payload = match serde_json::from_slice::<Value>(&body) {
        Ok(payload) => payload,
        Err(e) => {
            return (
                StatusCode::BAD_REQUEST,
                format!("invalid JSON request body: {e}"),
            )
                .into_response();
        }
    };

    let mut env = Envelope::new(&state.source_id, payload);
    capture_headers(&headers, &mut env);

    match state.source_ctx.send(&state.tx, env, &state.cancel).await {
        Ok(()) => (StatusCode::ACCEPTED, "accepted").into_response(),
        Err(_) => (
            StatusCode::SERVICE_UNAVAILABLE,
            "pipeline is not accepting webhook events",
        )
            .into_response(),
    }
}

async fn not_found() -> impl IntoResponse {
    (StatusCode::NOT_FOUND, "webhook path not found")
}

fn capture_headers(headers: &HeaderMap, env: &mut Envelope) {
    for (name, value) in headers {
        let Ok(value) = value.to_str() else {
            continue;
        };
        let name_str = name.as_str();
        if matches!(name_str, TRACEPARENT | TRACESTATE) {
            // Trace-context headers are owned by the observability layer.
            // Store them under the bare key so SourceCtx::send can refresh
            // the span context without leaving a stale `http.header.*` copy.
            env.meta
                .headers
                .insert(name_str.to_string(), value.to_string());
        } else {
            env.meta
                .headers
                .insert(format!("http.header.{}", name_str), value.to_string());
        }
    }
}

#[derive(Debug, Deserialize)]
struct HttpWebhookSourceConfig {
    bind: String,
    path: String,
}

/// Registry factory for [`HttpWebhookSource`]. Registered by
/// `courier::registry::register_builtin` under kind `"http_webhook"`.
///
/// `retry` is rejected at config time: webhook is push-based, so there is
/// nothing to "retry" — the upstream owns the retry decision.
pub fn http_webhook_source_factory(
    id: &str,
    config: Value,
    retry: Option<RetryPolicy>,
) -> Result<Box<dyn Source>> {
    if retry.is_some() {
        bail!(
            "invalid config for component type 'http_webhook': retry has no effect on push-based sources"
        );
    }
    let config: HttpWebhookSourceConfig = parse_config("http_webhook", config)?;
    let bind = config.bind.parse::<SocketAddr>().map_err(|e| {
        anyhow::anyhow!(
            "invalid http_webhook bind '{}': {e}",
            redact_secret(&config.bind)
        )
    })?;
    if !config.path.starts_with('/') {
        bail!(
            "invalid http_webhook path '{}': path must start with '/'",
            redact_secret(&config.path)
        );
    }

    Ok(Box::new(HttpWebhookSource::new(id, bind, config.path)))
}

#[cfg(test)]
mod tests {
    use std::net::{SocketAddr, TcpListener as StdTcpListener};
    use std::time::Duration;

    use reqwest::Client;
    use serde_json::json;
    use tokio::sync::mpsc;

    use super::*;

    #[tokio::test]
    async fn emits_envelope_for_valid_webhook_request() {
        let bind = unused_local_addr();
        let source = HttpWebhookSource::new("webhook", bind, "/events");
        let (tx, mut rx) = mpsc::channel(8);
        let cancel = CancellationToken::new();

        let c = cancel.clone();
        let handle = tokio::spawn(async move { Box::new(source).run(tx, c).await });
        tokio::time::sleep(Duration::from_millis(50)).await;

        let response = Client::new()
            .post(format!("http://{bind}/events"))
            .header("x-event-id", "evt-1")
            .header(
                "traceparent",
                "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
            )
            .json(&json!({ "event": "created" }))
            .send()
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::ACCEPTED);

        let env = tokio::time::timeout(Duration::from_secs(2), rx.recv())
            .await
            .expect("webhook timed out")
            .expect("source closed before emitting");

        assert_eq!(env.meta.source_id, "webhook");
        assert_eq!(env.payload, json!({ "event": "created" }));
        assert_eq!(
            env.meta.headers.get("http.header.x-event-id"),
            Some(&"evt-1".to_string())
        );
        assert!(env.meta.headers.contains_key(TRACEPARENT));
        // Trace-context headers are not duplicated under http.header.* so
        // that SourceCtx::send can refresh the bare key without leaving a
        // stale copy behind.
        assert!(
            !env.meta.headers.contains_key("http.header.traceparent"),
            "traceparent should not be duplicated under http.header.*"
        );

        cancel.cancel();
        let _ = tokio::time::timeout(Duration::from_secs(1), handle).await;
    }

    #[tokio::test]
    async fn rejects_wrong_method_and_invalid_json() {
        let bind = unused_local_addr();
        let source = HttpWebhookSource::new("webhook", bind, "/events");
        let (tx, _rx) = mpsc::channel(8);
        let cancel = CancellationToken::new();

        let c = cancel.clone();
        let handle = tokio::spawn(async move { Box::new(source).run(tx, c).await });
        tokio::time::sleep(Duration::from_millis(50)).await;

        let client = Client::new();
        let wrong_method = client
            .get(format!("http://{bind}/events"))
            .send()
            .await
            .unwrap();
        assert_eq!(wrong_method.status(), StatusCode::METHOD_NOT_ALLOWED);

        let invalid_json = client
            .post(format!("http://{bind}/events"))
            .body("not json")
            .send()
            .await
            .unwrap();
        assert_eq!(invalid_json.status(), StatusCode::BAD_REQUEST);

        cancel.cancel();
        let _ = tokio::time::timeout(Duration::from_secs(1), handle).await;
    }

    #[tokio::test]
    async fn response_waits_for_channel_capacity_before_returning_accepted() {
        let bind = unused_local_addr();
        let source = HttpWebhookSource::new("webhook", bind, "/events");
        let (tx, mut rx) = mpsc::channel(1);
        let cancel = CancellationToken::new();

        let c = cancel.clone();
        let source_handle = tokio::spawn(async move { Box::new(source).run(tx, c).await });
        tokio::time::sleep(Duration::from_millis(50)).await;

        let client = Client::new();
        let first = client
            .post(format!("http://{bind}/events"))
            .json(&json!({ "n": 1 }))
            .send()
            .await
            .unwrap();
        assert_eq!(first.status(), StatusCode::ACCEPTED);

        let second_client = client.clone();
        let second = tokio::spawn(async move {
            second_client
                .post(format!("http://{bind}/events"))
                .json(&json!({ "n": 2 }))
                .send()
                .await
                .unwrap()
        });

        tokio::time::sleep(Duration::from_millis(100)).await;
        assert!(
            !second.is_finished(),
            "second request returned before downstream channel had capacity"
        );

        let first_env = rx.recv().await.expect("expected first envelope");
        assert_eq!(first_env.payload, json!({ "n": 1 }));

        let second_response = tokio::time::timeout(Duration::from_secs(2), second)
            .await
            .expect("second request stayed blocked after capacity was freed")
            .expect("second request task failed");
        assert_eq!(second_response.status(), StatusCode::ACCEPTED);

        let second_env = rx.recv().await.expect("expected second envelope");
        assert_eq!(second_env.payload, json!({ "n": 2 }));

        cancel.cancel();
        let _ = tokio::time::timeout(Duration::from_secs(1), source_handle).await;
    }

    #[tokio::test]
    async fn blocked_request_returns_unavailable_when_cancelled() {
        let bind = unused_local_addr();
        let source = HttpWebhookSource::new("webhook", bind, "/events");
        let (tx, _rx) = mpsc::channel(1);
        let cancel = CancellationToken::new();

        let c = cancel.clone();
        let source_handle = tokio::spawn(async move { Box::new(source).run(tx, c).await });
        tokio::time::sleep(Duration::from_millis(50)).await;

        let client = Client::new();
        let first = client
            .post(format!("http://{bind}/events"))
            .json(&json!({ "n": 1 }))
            .send()
            .await
            .unwrap();
        assert_eq!(first.status(), StatusCode::ACCEPTED);

        let second_client = client.clone();
        let second = tokio::spawn(async move {
            second_client
                .post(format!("http://{bind}/events"))
                .json(&json!({ "n": 2 }))
                .send()
                .await
                .unwrap()
        });

        tokio::time::sleep(Duration::from_millis(100)).await;
        assert!(
            !second.is_finished(),
            "second request returned before cancellation"
        );

        cancel.cancel();
        let second_response = tokio::time::timeout(Duration::from_secs(2), second)
            .await
            .expect("second request stayed blocked after cancellation")
            .expect("second request task failed");
        assert_eq!(second_response.status(), StatusCode::SERVICE_UNAVAILABLE);

        let _ = tokio::time::timeout(Duration::from_secs(1), source_handle).await;
    }

    #[test]
    fn factory_rejects_invalid_path() {
        let err = match http_webhook_source_factory(
            "webhook",
            json!({
                "bind": "127.0.0.1:8080",
                "path": "events",
            }),
            None,
        ) {
            Ok(_) => panic!("expected invalid path error"),
            Err(err) => err,
        };

        assert!(err.to_string().contains("path must start with '/'"));
    }

    #[test]
    fn factory_builds_with_required_fields() {
        let source = http_webhook_source_factory(
            "webhook",
            json!({
                "bind": "127.0.0.1:8080",
                "path": "/events",
            }),
            None,
        )
        .unwrap();

        assert_eq!(source.id(), "webhook");
    }

    #[test]
    fn factory_rejects_retry_policy() {
        use crate::retry::{ExhaustedPolicy, RetryPolicy};

        let err = http_webhook_source_factory(
            "webhook",
            json!({ "bind": "127.0.0.1:8080", "path": "/events" }),
            Some(RetryPolicy {
                max_attempts: 3,
                initial_delay_ms: 100,
                backoff_multiplier: 2.0,
                max_delay_ms: 1000,
                on_exhausted: ExhaustedPolicy::Propagate,
            }),
        )
        .err()
        .expect("expected retry rejection");
        let msg = format!("{err:#}");
        assert!(
            msg.contains("retry has no effect on push-based sources"),
            "{msg}"
        );
    }

    fn unused_local_addr() -> SocketAddr {
        let listener = StdTcpListener::bind("127.0.0.1:0").unwrap();
        listener.local_addr().unwrap()
    }
}