camel-core 0.9.0

Core engine for rust-camel
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
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;

use camel_api::{
    CamelError, Exchange, ExchangePatch, FunctionDefinition, FunctionId, FunctionInvocationError,
    FunctionInvoker, PatchBody,
};
use tower::Service;
use tracing::Instrument;

#[derive(Clone)]
pub struct FunctionStep {
    definition: FunctionDefinition,
    invoker: Arc<dyn FunctionInvoker>,
}

impl FunctionStep {
    pub fn new(invoker: Arc<dyn FunctionInvoker>, definition: FunctionDefinition) -> Self {
        Self {
            definition,
            invoker,
        }
    }
}

impl Service<Exchange> for FunctionStep {
    type Response = Exchange;
    type Error = CamelError;
    type Future = Pin<Box<dyn Future<Output = Result<Exchange, CamelError>> + Send>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, mut ex: Exchange) -> Self::Future {
        let invoker = Arc::clone(&self.invoker);
        let id = self.definition.id.clone();
        let runtime = self.definition.runtime.clone();
        let timeout_ms = self.definition.timeout_ms;
        let span = tracing::info_span!(
            target: "camel_function",
            "function",
            function_id = %id.0,
            runtime = %runtime,
            timeout_ms = timeout_ms,
            status = tracing::field::Empty,
            duration_ms = tracing::field::Empty,
            error_kind = tracing::field::Empty,
        );
        Box::pin(async move {
            let start = std::time::Instant::now();
            let outcome: Result<ExchangePatch, CamelError> = async {
                let result = tokio::time::timeout(
                    Duration::from_millis(timeout_ms),
                    invoker.invoke(&id, &ex),
                )
                .await
                .map_err(|_| {
                    CamelError::ProcessorError(format!(
                        "function:timeout: {} timed out after {}ms",
                        id.0, timeout_ms
                    ))
                })?;
                let patch = result.map_err(|e| map_invocation_error(e, &id))?;
                Ok(patch)
            }
            .instrument(span.clone())
            .await;
            let elapsed = start.elapsed().as_millis() as u64;
            span.record("duration_ms", elapsed);
            match &outcome {
                Ok(_) => {
                    span.record("status", "ok");
                }
                Err(CamelError::ProcessorError(msg)) => {
                    let kind = if msg.starts_with("function:timeout:") {
                        "timeout"
                    } else if msg.starts_with("function:user_error:") {
                        "user_error"
                    } else if msg.starts_with("function:runner_unavailable:") {
                        "runner_unavailable"
                    } else if msg.starts_with("function:not_registered:") {
                        "not_registered"
                    } else if msg.starts_with("function:transport:") {
                        "transport"
                    } else if msg.starts_with("function:invalid_patch:") {
                        "invalid_patch"
                    } else {
                        "unknown"
                    };
                    span.record("status", kind);
                    span.record("error_kind", kind);
                }
                Err(_) => {
                    span.record("status", "unknown");
                    span.record("error_kind", "unknown");
                }
            }
            let patch = outcome?;
            apply_patch(&mut ex, patch);
            Ok(ex)
        })
    }
}

fn map_invocation_error(err: FunctionInvocationError, id: &FunctionId) -> CamelError {
    match err {
        FunctionInvocationError::UserError { message, stack, .. } => {
            let detail = match stack {
                Some(s) if !s.is_empty() => {
                    format!("function:user_error: {}: {}\n{}", id.0, message, s)
                }
                _ => format!("function:user_error: {}: {}", id.0, message),
            };
            CamelError::ProcessorError(detail)
        }
        FunctionInvocationError::Timeout { timeout_ms, .. } => CamelError::ProcessorError(format!(
            "function:timeout: {} timed out after {}ms",
            id.0, timeout_ms
        )),
        FunctionInvocationError::NotRegistered { .. } => {
            CamelError::ProcessorError(format!("function:not_registered: {}", id.0))
        }
        FunctionInvocationError::RunnerUnavailable { reason } => {
            CamelError::ProcessorError(format!("function:runner_unavailable: {}: {}", id.0, reason))
        }
        FunctionInvocationError::Transport(msg) => {
            CamelError::ProcessorError(format!("function:transport: {}: {}", id.0, msg))
        }
        FunctionInvocationError::InvalidPatch(msg) => {
            CamelError::ProcessorError(format!("function:invalid_patch: {}: {}", id.0, msg))
        }
    }
}

fn apply_patch(ex: &mut Exchange, patch: ExchangePatch) {
    if let Some(body) = patch.body {
        ex.input.body = match body {
            PatchBody::Text(s) => s.into(),
            PatchBody::Json(v) => v.into(),
            PatchBody::Empty => camel_api::Body::Empty,
        };
    }
    for (k, v) in patch.headers_set {
        ex.input.headers.insert(k, v);
    }
    for k in patch.headers_removed {
        ex.input.headers.remove(&k);
    }
    for (k, v) in patch.properties_set {
        ex.properties.insert(k, v);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use camel_api::function::PrepareToken;
    use camel_api::{FunctionDiff, FunctionInvokerSync};
    use std::sync::Mutex;

    struct MockInvoker {
        responses: Mutex<Vec<Result<ExchangePatch, FunctionInvocationError>>>,
    }

    impl MockInvoker {
        fn new(responses: Vec<Result<ExchangePatch, FunctionInvocationError>>) -> Self {
            Self {
                responses: Mutex::new(responses),
            }
        }
    }

    impl FunctionInvokerSync for MockInvoker {
        fn stage_pending(
            &self,
            _def: FunctionDefinition,
            _route_id: Option<&str>,
            _generation: u64,
        ) {
        }
        fn discard_staging(&self, _generation: u64) {}
        fn begin_reload(&self) -> u64 {
            0
        }
        fn function_refs_for_route(&self, _route_id: &str) -> Vec<(FunctionId, Option<String>)> {
            vec![]
        }
        fn staged_refs_for_route(
            &self,
            _route_id: &str,
            _generation: u64,
        ) -> Vec<(FunctionId, Option<String>)> {
            vec![]
        }
        fn staged_defs_for_route(
            &self,
            _route_id: &str,
            _generation: u64,
        ) -> Vec<(FunctionDefinition, Option<String>)> {
            vec![]
        }
    }

    #[async_trait]
    impl FunctionInvoker for MockInvoker {
        async fn register(
            &self,
            _def: FunctionDefinition,
            _route_id: Option<&str>,
        ) -> Result<(), FunctionInvocationError> {
            Ok(())
        }
        async fn unregister(
            &self,
            _id: &FunctionId,
            _route_id: Option<&str>,
        ) -> Result<(), FunctionInvocationError> {
            Ok(())
        }
        async fn invoke(
            &self,
            _id: &FunctionId,
            _exchange: &Exchange,
        ) -> Result<ExchangePatch, FunctionInvocationError> {
            let mut resp = self.responses.lock().unwrap();
            resp.remove(0)
        }
        async fn prepare_reload(
            &self,
            _diff: FunctionDiff,
            _generation: u64,
        ) -> Result<PrepareToken, FunctionInvocationError> {
            Ok(PrepareToken::default())
        }
        async fn finalize_reload(
            &self,
            _diff: &FunctionDiff,
            _generation: u64,
        ) -> Result<(), FunctionInvocationError> {
            Ok(())
        }
        async fn rollback_reload(
            &self,
            _token: PrepareToken,
            _generation: u64,
        ) -> Result<(), FunctionInvocationError> {
            Ok(())
        }
        async fn commit_reload(
            &self,
            _diff: FunctionDiff,
            _generation: u64,
        ) -> Result<(), FunctionInvocationError> {
            Ok(())
        }
        async fn commit_staged(&self) -> Result<(), FunctionInvocationError> {
            Ok(())
        }
    }

    fn test_definition() -> FunctionDefinition {
        FunctionDefinition {
            id: FunctionId::compute("deno", "test", 5000),
            runtime: "deno".into(),
            source: "test".into(),
            timeout_ms: 5000,
            route_id: None,
            step_index: None,
        }
    }

    #[tokio::test]
    async fn function_step_applies_patch_body_text() {
        let invoker = Arc::new(MockInvoker::new(vec![Ok(ExchangePatch {
            body: Some(PatchBody::Text("patched".into())),
            ..Default::default()
        })]));
        let mut step = FunctionStep::new(invoker, test_definition());
        let ex = Exchange::default();
        let result = step.call(ex).await.unwrap();
        assert_eq!(result.input.body.as_text(), Some("patched"));
    }

    #[tokio::test]
    async fn function_step_applies_patch_headers() {
        let invoker = Arc::new(MockInvoker::new(vec![Ok(ExchangePatch {
            headers_set: vec![("x-key".into(), serde_json::json!("val"))],
            headers_removed: vec!["x-old".into()],
            ..Default::default()
        })]));
        let mut step = FunctionStep::new(invoker, test_definition());
        let mut ex = Exchange::default();
        ex.input
            .headers
            .insert("x-old".into(), serde_json::json!("gone"));
        let result = step.call(ex).await.unwrap();
        assert_eq!(
            result.input.headers.get("x-key").unwrap().as_str(),
            Some("val")
        );
        assert!(!result.input.headers.contains_key("x-old"));
    }

    #[tokio::test]
    async fn function_step_applies_patch_properties() {
        let invoker = Arc::new(MockInvoker::new(vec![Ok(ExchangePatch {
            properties_set: vec![("prop".into(), serde_json::json!(42))],
            ..Default::default()
        })]));
        let mut step = FunctionStep::new(invoker, test_definition());
        let ex = Exchange::default();
        let result = step.call(ex).await.unwrap();
        assert_eq!(result.properties.get("prop").unwrap().as_i64(), Some(42));
    }

    #[tokio::test]
    async fn function_step_maps_timeout_error() {
        let invoker = Arc::new(MockInvoker::new(vec![Err(
            FunctionInvocationError::Timeout {
                function_id: FunctionId("x".into()),
                timeout_ms: 5000,
            },
        )]));
        let mut step = FunctionStep::new(invoker, test_definition());
        let ex = Exchange::default();
        let err = step.call(ex).await.unwrap_err();
        let msg = match &err {
            CamelError::ProcessorError(m) => m,
            _ => panic!("wrong error type"),
        };
        assert!(msg.contains("function:timeout:"));
    }

    #[tokio::test]
    async fn function_step_maps_user_error() {
        let invoker = Arc::new(MockInvoker::new(vec![Err(
            FunctionInvocationError::UserError {
                function_id: FunctionId("x".into()),
                message: "boom".into(),
                stack: None,
            },
        )]));
        let mut step = FunctionStep::new(invoker, test_definition());
        let ex = Exchange::default();
        let err = step.call(ex).await.unwrap_err();
        let msg = match &err {
            CamelError::ProcessorError(m) => m,
            _ => panic!("wrong error type"),
        };
        assert!(msg.contains("function:user_error:"));
        assert!(msg.contains("boom"));
    }

    #[tokio::test]
    async fn function_step_client_side_timeout_fires() {
        struct SlowInvoker;
        impl FunctionInvokerSync for SlowInvoker {
            fn stage_pending(
                &self,
                _def: FunctionDefinition,
                _route_id: Option<&str>,
                _generation: u64,
            ) {
            }
            fn discard_staging(&self, _generation: u64) {}
            fn begin_reload(&self) -> u64 {
                0
            }
            fn function_refs_for_route(
                &self,
                _route_id: &str,
            ) -> Vec<(FunctionId, Option<String>)> {
                vec![]
            }
            fn staged_refs_for_route(
                &self,
                _route_id: &str,
                _generation: u64,
            ) -> Vec<(FunctionId, Option<String>)> {
                vec![]
            }
            fn staged_defs_for_route(
                &self,
                _route_id: &str,
                _generation: u64,
            ) -> Vec<(FunctionDefinition, Option<String>)> {
                vec![]
            }
        }
        #[async_trait]
        impl FunctionInvoker for SlowInvoker {
            async fn register(
                &self,
                _def: FunctionDefinition,
                _route_id: Option<&str>,
            ) -> Result<(), FunctionInvocationError> {
                Ok(())
            }
            async fn unregister(
                &self,
                _id: &FunctionId,
                _route_id: Option<&str>,
            ) -> Result<(), FunctionInvocationError> {
                Ok(())
            }
            async fn invoke(
                &self,
                _id: &FunctionId,
                _exchange: &Exchange,
            ) -> Result<ExchangePatch, FunctionInvocationError> {
                tokio::time::sleep(Duration::from_secs(10)).await;
                Ok(ExchangePatch::default())
            }
            async fn prepare_reload(
                &self,
                _diff: FunctionDiff,
                _generation: u64,
            ) -> Result<PrepareToken, FunctionInvocationError> {
                Ok(PrepareToken::default())
            }
            async fn finalize_reload(
                &self,
                _diff: &FunctionDiff,
                _generation: u64,
            ) -> Result<(), FunctionInvocationError> {
                Ok(())
            }
            async fn rollback_reload(
                &self,
                _token: PrepareToken,
                _generation: u64,
            ) -> Result<(), FunctionInvocationError> {
                Ok(())
            }
            async fn commit_reload(
                &self,
                _diff: FunctionDiff,
                _generation: u64,
            ) -> Result<(), FunctionInvocationError> {
                Ok(())
            }
            async fn commit_staged(&self) -> Result<(), FunctionInvocationError> {
                Ok(())
            }
        }
        let def = FunctionDefinition {
            id: FunctionId::compute("deno", "slow", 50),
            runtime: "deno".into(),
            source: "slow".into(),
            timeout_ms: 50,
            route_id: None,
            step_index: None,
        };
        let mut step = FunctionStep::new(Arc::new(SlowInvoker), def);
        let ex = Exchange::default();
        let err = step.call(ex).await.unwrap_err();
        let msg = match &err {
            CamelError::ProcessorError(m) => m,
            _ => panic!("wrong error type"),
        };
        assert!(msg.contains("function:timeout:"));
    }

    #[tokio::test]
    async fn function_step_emits_tracing_span() {
        use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
        use tracing_subscriber::prelude::*;

        let invoker = Arc::new(MockInvoker::new(vec![Ok(ExchangePatch::default())]));
        let def = FunctionDefinition {
            id: FunctionId::compute("deno", "span_test", 5000),
            runtime: "deno".into(),
            source: "span_test".into(),
            timeout_ms: 5000,
            route_id: None,
            step_index: None,
        };
        let mut step = FunctionStep::new(invoker, def);
        let ex = Exchange::default();

        let span_seen = Arc::new(AtomicBool::new(false));
        let span_seen_clone = span_seen.clone();

        let layer = tracing_subscriber::fmt::layer()
            .with_writer(std::io::sink)
            .with_filter(tracing_subscriber::filter::filter_fn(move |meta| {
                if meta.target() == "camel_function" && meta.name() == "function" {
                    span_seen_clone.store(true, AtomicOrdering::SeqCst);
                }
                true
            }));

        let _guard = tracing_subscriber::registry().with(layer).set_default();
        let result = step.call(ex).await;
        assert!(result.is_ok());
        assert!(
            span_seen.load(AtomicOrdering::SeqCst),
            "expected function span with target 'camel_function' and name 'function'"
        );
    }

    #[tokio::test]
    async fn function_step_user_error_with_stack() {
        let invoker = Arc::new(MockInvoker::new(vec![Err(
            FunctionInvocationError::UserError {
                function_id: FunctionId("x".into()),
                message: "custom error".into(),
                stack: Some("at line 1\nat line 2".into()),
            },
        )]));
        let mut step = FunctionStep::new(invoker, test_definition());
        let ex = Exchange::default();
        let err = step.call(ex).await.unwrap_err();
        let msg = match &err {
            CamelError::ProcessorError(m) => m.clone(),
            _ => panic!("wrong error type"),
        };
        assert!(msg.contains("function:user_error:"));
        assert!(msg.contains("custom error"));
        assert!(msg.contains("at line 1"));
    }
}