stackable_telemetry/tracing/settings/
otlp_log.rs1use tracing::level_filters::LevelFilter;
4
5use super::{Settings, SettingsBuilder, SettingsToggle};
6
7#[derive(Debug, Default, PartialEq, Eq)]
9pub enum OtlpLogSettings {
10 #[default]
12 Disabled,
13
14 Enabled {
16 common_settings: Settings,
18 },
19}
20
21impl SettingsToggle for OtlpLogSettings {
22 fn is_enabled(&self) -> bool {
23 match self {
24 Self::Disabled => false,
25 Self::Enabled { .. } => true,
26 }
27 }
28}
29
30pub struct OtlpLogSettingsBuilder {
38 pub(crate) common_settings: Settings,
39}
40
41impl OtlpLogSettingsBuilder {
42 pub fn build(self) -> OtlpLogSettings {
44 OtlpLogSettings::Enabled {
45 common_settings: self.common_settings,
46 }
47 }
48}
49
50impl 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<Self>,
69{
70 fn from(settings: Option<T>) -> Self {
71 match settings {
72 Some(settings) => settings.into(),
73 None => Self::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 if value.2 {
92 Self::Enabled {
93 common_settings: Settings {
94 environment_variable: value.0,
95 default_level: value.1,
96 },
97 }
98 } else {
99 Self::Disabled
100 }
101 }
102}
103
104#[cfg(test)]
105mod test {
106 use tracing::level_filters::LevelFilter;
107
108 use super::*;
109
110 #[test]
111 fn builds_settings() {
112 let expected = OtlpLogSettings::Enabled {
113 common_settings: Settings {
114 environment_variable: "hello",
115 default_level: LevelFilter::DEBUG,
116 },
117 };
118 let result = Settings::builder()
119 .with_environment_variable("hello")
120 .with_default_level(LevelFilter::DEBUG)
121 .otlp_log_settings_builder()
122 .build();
123
124 assert_eq!(expected, result);
125 }
126}