stackable_telemetry/tracing/settings/
otlp_log.rs

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
//! OTLP Log Subscriber Settings.

use tracing::level_filters::LevelFilter;

use super::{Settings, SettingsBuilder, SettingsToggle};

/// Configure specific settings for the OpenTelemetry log subscriber.
#[derive(Debug, Default, PartialEq)]
pub enum OtlpLogSettings {
    /// OpenTelemetry log subscriber disabled.
    #[default]
    Disabled,

    /// OpenTelemetry log subscriber enabled.
    Enabled {
        /// Common subscriber settings that apply to the OpenTelemetry log subscriber.
        common_settings: Settings,
    },
}

impl SettingsToggle for OtlpLogSettings {
    fn is_enabled(&self) -> bool {
        match self {
            OtlpLogSettings::Disabled => false,
            OtlpLogSettings::Enabled { .. } => true,
        }
    }
}

/// For building [`OtlpLogSettings`].
///
/// <div class="warning">
///
/// Do not use directly, instead use the [`Settings::builder`] associated function.
///
/// </div>
pub struct OtlpLogSettingsBuilder {
    pub(crate) common_settings: Settings,
}

impl OtlpLogSettingsBuilder {
    /// Consumes `self` and builds [`OtlpLogSettings`].
    pub fn build(self) -> OtlpLogSettings {
        OtlpLogSettings::Enabled {
            common_settings: self.common_settings,
        }
    }
}

/// This implementation is used to turn the common settings builder into the OTLP log specific
/// settings builder via the [`SettingsBuilder::otlp_log_settings_builder`] function.
impl From<SettingsBuilder> for OtlpLogSettingsBuilder {
    fn from(value: SettingsBuilder) -> Self {
        Self {
            common_settings: value.build(),
        }
    }
}

impl From<Settings> for OtlpLogSettings {
    fn from(common_settings: Settings) -> Self {
        Self::Enabled { common_settings }
    }
}

impl<T> From<Option<T>> for OtlpLogSettings
where
    T: Into<OtlpLogSettings>,
{
    fn from(settings: Option<T>) -> Self {
        match settings {
            Some(settings) => settings.into(),
            None => OtlpLogSettings::default(),
        }
    }
}

impl From<(&'static str, LevelFilter)> for OtlpLogSettings {
    fn from(value: (&'static str, LevelFilter)) -> Self {
        Self::Enabled {
            common_settings: Settings {
                environment_variable: value.0,
                default_level: value.1,
            },
        }
    }
}

impl From<(&'static str, LevelFilter, bool)> for OtlpLogSettings {
    fn from(value: (&'static str, LevelFilter, bool)) -> Self {
        match value.2 {
            true => Self::Enabled {
                common_settings: Settings {
                    environment_variable: value.0,
                    default_level: value.1,
                },
            },
            false => Self::Disabled,
        }
    }
}

#[cfg(test)]
mod test {
    use tracing::level_filters::LevelFilter;

    use super::*;

    #[test]
    fn builds_settings() {
        let expected = OtlpLogSettings::Enabled {
            common_settings: Settings {
                environment_variable: "hello",
                default_level: LevelFilter::DEBUG,
            },
        };
        let result = Settings::builder()
            .with_environment_variable("hello")
            .with_default_level(LevelFilter::DEBUG)
            .otlp_log_settings_builder()
            .build();

        assert_eq!(expected, result);
    }
}