stackable_telemetry/tracing/settings/
otlp_log.rs

1//! OTLP Log Subscriber Settings.
2
3use tracing::level_filters::LevelFilter;
4
5use super::{Settings, SettingsBuilder, SettingsToggle};
6
7/// Configure specific settings for the OpenTelemetry log subscriber.
8#[derive(Debug, Default, PartialEq)]
9pub enum OtlpLogSettings {
10    /// OpenTelemetry log subscriber disabled.
11    #[default]
12    Disabled,
13
14    /// OpenTelemetry log subscriber enabled.
15    Enabled {
16        /// Common subscriber settings that apply to the OpenTelemetry log subscriber.
17        common_settings: Settings,
18    },
19}
20
21impl SettingsToggle for OtlpLogSettings {
22    fn is_enabled(&self) -> bool {
23        match self {
24            OtlpLogSettings::Disabled => false,
25            OtlpLogSettings::Enabled { .. } => true,
26        }
27    }
28}
29
30/// For building [`OtlpLogSettings`].
31///
32/// <div class="warning">
33///
34/// Do not use directly, instead use the [`Settings::builder`] associated function.
35///
36/// </div>
37pub struct OtlpLogSettingsBuilder {
38    pub(crate) common_settings: Settings,
39}
40
41impl OtlpLogSettingsBuilder {
42    /// Consumes `self` and builds [`OtlpLogSettings`].
43    pub fn build(self) -> OtlpLogSettings {
44        OtlpLogSettings::Enabled {
45            common_settings: self.common_settings,
46        }
47    }
48}
49
50/// This implementation is used to turn the common settings builder into the OTLP log specific
51/// settings builder via the [`SettingsBuilder::otlp_log_settings_builder`] function.
52impl From<SettingsBuilder> for OtlpLogSettingsBuilder {
53    fn from(value: SettingsBuilder) -> Self {
54        Self {
55            common_settings: value.build(),
56        }
57    }
58}
59
60impl From<Settings> for OtlpLogSettings {
61    fn from(common_settings: Settings) -> Self {
62        Self::Enabled { common_settings }
63    }
64}
65
66impl<T> From<Option<T>> for OtlpLogSettings
67where
68    T: Into<OtlpLogSettings>,
69{
70    fn from(settings: Option<T>) -> Self {
71        match settings {
72            Some(settings) => settings.into(),
73            None => OtlpLogSettings::default(),
74        }
75    }
76}
77
78impl From<(&'static str, LevelFilter)> for OtlpLogSettings {
79    fn from(value: (&'static str, LevelFilter)) -> Self {
80        Self::Enabled {
81            common_settings: Settings {
82                environment_variable: value.0,
83                default_level: value.1,
84            },
85        }
86    }
87}
88
89impl From<(&'static str, LevelFilter, bool)> for OtlpLogSettings {
90    fn from(value: (&'static str, LevelFilter, bool)) -> Self {
91        match value.2 {
92            true => Self::Enabled {
93                common_settings: Settings {
94                    environment_variable: value.0,
95                    default_level: value.1,
96                },
97            },
98            false => Self::Disabled,
99        }
100    }
101}
102
103#[cfg(test)]
104mod test {
105    use tracing::level_filters::LevelFilter;
106
107    use super::*;
108
109    #[test]
110    fn builds_settings() {
111        let expected = OtlpLogSettings::Enabled {
112            common_settings: Settings {
113                environment_variable: "hello",
114                default_level: LevelFilter::DEBUG,
115            },
116        };
117        let result = Settings::builder()
118            .with_environment_variable("hello")
119            .with_default_level(LevelFilter::DEBUG)
120            .otlp_log_settings_builder()
121            .build();
122
123        assert_eq!(expected, result);
124    }
125}