stackable_telemetry/tracing/settings/
otlp_trace.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 Trace Subscriber Settings.

use tracing::level_filters::LevelFilter;

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

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

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

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

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

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

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

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

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

impl From<(&'static str, LevelFilter)> for OtlpTraceSettings {
    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 OtlpTraceSettings {
    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 = OtlpTraceSettings::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_trace_settings_builder()
            .build();

        assert_eq!(expected, result);
    }
}