Skip to main content

camel_component_api/
component_context.rs

1use std::sync::Arc;
2
3use camel_api::{AsyncHealthCheck, MetricsCollector, PlatformService};
4use camel_language_api::Language;
5
6use crate::Component;
7
8/// Runtime context passed to components during endpoint creation.
9pub trait ComponentContext: Send + Sync {
10    /// Resolve a component by scheme.
11    fn resolve_component(&self, scheme: &str) -> Option<Arc<dyn Component>>;
12
13    /// Resolve a language by name.
14    fn resolve_language(&self, name: &str) -> Option<Arc<dyn Language>>;
15
16    /// Access the active metrics collector.
17    fn metrics(&self) -> Arc<dyn MetricsCollector>;
18
19    /// Snapshot of the `[observability.metrics].components` lever —
20    /// gates only the uniform component-operations family served through
21    /// `RuntimeObservability::component_metrics()`; error-family
22    /// emission is never lever-gated. Default false (opt-in);
23    /// `CamelContext` overrides this with its `MetricsLeversConfig`
24    /// snapshot.
25    fn component_metrics_enabled(&self) -> bool {
26        false
27    }
28
29    /// Access the active health-check registry.
30    ///
31    /// Used by component code paths that need to pin a route Unhealthy
32    /// (category (g) per ADR-0012). Default: NoOp — tests/examples inherit
33    /// the no-op. Concrete runtimes (CamelContext) override to return the
34    /// real registry.
35    fn health(&self) -> Arc<dyn crate::HealthCheckRegistry> {
36        Arc::new(crate::NoOpHealthCheckRegistry)
37    }
38
39    /// Access the active platform service.
40    fn platform_service(&self) -> Arc<dyn PlatformService>;
41
42    fn register_route_health_check(&self, route_id: &str, check: Arc<dyn AsyncHealthCheck>);
43
44    fn unregister_route_health_check(&self, route_id: &str);
45
46    fn route_id(&self) -> Option<&str> {
47        None
48    }
49
50    fn register_current_route_health_check(&self, check: Arc<dyn AsyncHealthCheck>) {
51        if let Some(id) = self.route_id() {
52            self.register_route_health_check(id, check);
53        }
54    }
55}
56
57/// Default no-op component context for tests/examples.
58pub struct NoOpComponentContext;
59
60impl ComponentContext for NoOpComponentContext {
61    fn resolve_component(&self, _scheme: &str) -> Option<Arc<dyn Component>> {
62        None
63    }
64
65    fn resolve_language(&self, _name: &str) -> Option<Arc<dyn Language>> {
66        None
67    }
68
69    fn metrics(&self) -> Arc<dyn MetricsCollector> {
70        Arc::new(camel_api::NoOpMetrics)
71    }
72
73    fn platform_service(&self) -> Arc<dyn PlatformService> {
74        Arc::new(camel_api::NoopPlatformService::default())
75    }
76
77    fn register_route_health_check(&self, _route_id: &str, _check: Arc<dyn AsyncHealthCheck>) {}
78
79    fn unregister_route_health_check(&self, _route_id: &str) {}
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn component_context_health_default_is_noop() {
88        let ctx = NoOpComponentContext;
89        let h = ctx.health();
90        // Must not panic.
91        h.force_unhealthy_for_route("any", "any", "any");
92    }
93}