Skip to main content

saluki_components/config/
mrf.rs

1//! Multi-region failover configuration.
2
3/// Multi-region failover configuration shared by signal-specific pipelines.
4#[derive(Debug, Eq, PartialEq)]
5pub struct MrfConfiguration {
6    enabled: bool,
7    api_key: Option<String>,
8    metrics_endpoint: Option<String>,
9}
10
11impl MrfConfiguration {
12    /// Creates a new `MrfConfiguration`.
13    ///
14    /// `api_key` and `metrics_endpoint` are the failover region's API key and metrics intake URL, if configured.
15    pub fn new(enabled: bool, api_key: Option<String>, metrics_endpoint: Option<String>) -> Self {
16        Self {
17            enabled,
18            api_key,
19            metrics_endpoint,
20        }
21    }
22
23    /// Returns whether multi-region failover is enabled for this process.
24    pub const fn is_enabled(&self) -> bool {
25        self.enabled
26    }
27
28    /// Returns the failover-region API key.
29    pub fn api_key(&self) -> Option<&str> {
30        self.api_key.as_deref()
31    }
32
33    /// Returns the failover-region metrics endpoint URL.
34    pub fn metrics_endpoint_url(&self) -> Option<&str> {
35        self.metrics_endpoint.as_deref()
36    }
37
38    /// Returns the endpoint and API key override for the failover-region metrics forwarder.
39    pub fn metrics_endpoint_override(&self) -> Option<(String, String)> {
40        if !self.enabled {
41            return None;
42        }
43
44        Some((self.metrics_endpoint.clone()?, self.api_key.clone()?))
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    const API_KEY: &str = "mrf-api-key";
53    const ENDPOINT: &str = "https://mrf.example.com";
54
55    fn mrf_config(enabled: bool, api_key: Option<&str>, metrics_endpoint: Option<&str>) -> MrfConfiguration {
56        MrfConfiguration::new(enabled, api_key.map(String::from), metrics_endpoint.map(String::from))
57    }
58
59    #[test]
60    fn carries_the_failover_region_settings() {
61        let config = mrf_config(true, Some(API_KEY), Some(ENDPOINT));
62
63        assert!(config.is_enabled());
64        assert_eq!(Some(API_KEY), config.api_key());
65        assert_eq!(Some(ENDPOINT), config.metrics_endpoint_url());
66    }
67
68    #[test]
69    fn metrics_endpoint_override_requires_api_key_and_endpoint() {
70        assert_eq!(None, mrf_config(true, None, Some(ENDPOINT)).metrics_endpoint_override());
71        assert_eq!(None, mrf_config(true, Some(API_KEY), None).metrics_endpoint_override());
72        assert_eq!(
73            Some((ENDPOINT.to_string(), API_KEY.to_string())),
74            mrf_config(true, Some(API_KEY), Some(ENDPOINT)).metrics_endpoint_override()
75        );
76    }
77
78    #[test]
79    fn metrics_endpoint_override_is_none_when_disabled() {
80        // Even with a fully-populated endpoint and API key, `metrics_endpoint_override` short-circuits to `None`
81        // when multi-region failover is disabled (the `if !self.enabled` guard at the top of the method).
82        let config = mrf_config(false, Some(API_KEY), Some(ENDPOINT));
83
84        assert!(!config.is_enabled());
85        assert_eq!(None, config.metrics_endpoint_override());
86
87        // The endpoint itself is still carried; only the override is gated on `enabled`, which confirms the `None`
88        // above comes from the disabled short-circuit rather than a missing endpoint or API key.
89        assert_eq!(Some(ENDPOINT), config.metrics_endpoint_url());
90        assert_eq!(Some(API_KEY), config.api_key());
91    }
92}