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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! Reusable logic around [OpenTelemetry ("OTEL")][otel] support
//!
//! [otel]: https://opentelemetry.io

use std::{path::PathBuf, str::FromStr};

use anyhow::bail;
use serde::{Deserialize, Serialize};
use url::Url;

use crate::{logging::Level, wit::WitMap};

/// Configuration values for OpenTelemetry
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct OtelConfig {
    /// Determine whether observability should be enabled.
    #[serde(default)]
    pub enable_observability: bool,
    /// Determine whether traces should be enabled.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enable_traces: Option<bool>,
    /// Determine whether metrics should be enabled.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enable_metrics: Option<bool>,
    /// Determine whether logs should be enabled.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enable_logs: Option<bool>,
    /// Overrides the OpenTelemetry endpoint for all signals.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub observability_endpoint: Option<String>,
    /// Overrides the OpenTelemetry endpoint for traces.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub traces_endpoint: Option<String>,
    /// Overrides the OpenTelemetry endpoint for metrics.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metrics_endpoint: Option<String>,
    /// Overrides the OpenTelemetry endpoint for logs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logs_endpoint: Option<String>,
    /// Determines whether http or grpc will be used for exporting the telemetry.
    #[serde(default)]
    pub protocol: OtelProtocol,
    /// Additional CAs to include in the OpenTelemetry client configuration
    #[serde(default)]
    pub additional_ca_paths: Vec<PathBuf>,
    /// The level of tracing to enable.
    #[serde(default)]
    pub trace_level: Level,
}

impl OtelConfig {
    pub fn logs_endpoint(&self) -> String {
        self.resolve_endpoint(OtelSignal::Logs, self.logs_endpoint.clone())
    }

    pub fn metrics_endpoint(&self) -> String {
        self.resolve_endpoint(OtelSignal::Metrics, self.metrics_endpoint.clone())
    }

    pub fn traces_endpoint(&self) -> String {
        self.resolve_endpoint(OtelSignal::Traces, self.traces_endpoint.clone())
    }

    pub fn logs_enabled(&self) -> bool {
        self.enable_logs.unwrap_or(self.enable_observability)
    }

    pub fn metrics_enabled(&self) -> bool {
        self.enable_metrics.unwrap_or(self.enable_observability)
    }

    pub fn traces_enabled(&self) -> bool {
        self.enable_traces.unwrap_or(self.enable_observability)
    }

    // We have 3 potential outcomes depending on the provided configuration:
    // 1. We are given a signal-specific endpoint to use, which we'll use as-is.
    // 2. We are given an endpoint that each of the signal paths should added to
    // 3. We are given nothing, and we should simply default to an empty string,
    //    which lets the opentelemetry-otlp library handle defaults appropriately.
    fn resolve_endpoint(
        &self,
        signal: OtelSignal,
        signal_endpoint_override: Option<String>,
    ) -> String {
        // If we have a signal specific endpoint override, use it as provided.
        if let Some(endpoint) = signal_endpoint_override {
            return endpoint;
        }

        if let Some(endpoint) = self.observability_endpoint.clone() {
            return match self.protocol {
                OtelProtocol::Grpc => self.resolve_grpc_endpoint(endpoint),
                OtelProtocol::Http => self.resolve_http_endpoint(signal, endpoint),
            };
        }

        // If we have no match, fall back to empty string to let the opentelemetry-otlp
        // library handling turn into the signal-specific default endpoint.
        String::new()
    }

    // opentelemetry-otlp expects the gRPC endpoint to not have path components
    // configured, so we're just clearing them out and returning the base url.
    fn resolve_grpc_endpoint(&self, endpoint: String) -> String {
        match Url::parse(&endpoint) {
            Ok(mut url) => {
                if let Ok(mut path) = url.path_segments_mut() {
                    path.clear();
                }
                url.as_str().trim_end_matches('/').to_string()
            }
            Err(_) => endpoint,
        }
    }

    // opentelemetry-otlp expects the http endpoint to be fully configured
    // including the path, so we check whether there's a path already configured
    // and use the url as configured, or append the signal-specific path to the
    // provided endpoint.
    fn resolve_http_endpoint(&self, signal: OtelSignal, endpoint: String) -> String {
        match Url::parse(&endpoint) {
            Ok(url) => {
                if url.path() == "/" {
                    format!("{}{}", url.as_str().trim_end_matches('/'), signal)
                } else {
                    endpoint
                }
            }
            Err(_) => endpoint,
        }
    }
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
// TODO(joonas): In a future release we should enable this renaming once we
// are comfortable with the fact there are no providers being used that have
// the case sensitive handling still in place.
// #[serde(rename_all = "lowercase")]
pub enum OtelProtocol {
    #[serde(alias = "grpc", alias = "Grpc")]
    Grpc,
    #[serde(alias = "http", alias = "Http")]
    Http,
}

// Represents https://opentelemetry.io/docs/concepts/signals/
enum OtelSignal {
    Traces,
    Metrics,
    Logs,
}

impl std::fmt::Display for OtelSignal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "/v1/{}",
            match self {
                OtelSignal::Traces => "traces",
                OtelSignal::Metrics => "metrics",
                OtelSignal::Logs => "logs",
            }
        )
    }
}

impl Default for OtelProtocol {
    fn default() -> Self {
        Self::Http
    }
}

impl FromStr for OtelProtocol {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "http" => Ok(Self::Http),
            "grpc" => Ok(Self::Grpc),
            protocol => {
                bail!("unsupported protocol: {protocol:?}, did you mean 'http' or 'grpc'?")
            }
        }
    }
}

/// Environment settings for initializing a capability provider
pub type TraceContext = WitMap<String>;

#[cfg(test)]
mod tests {
    use super::{OtelConfig, OtelProtocol};

    #[test]
    fn test_grpc_resolves_to_empty_string_without_overrides() {
        let config = OtelConfig {
            protocol: OtelProtocol::Grpc,
            ..Default::default()
        };

        let expected = String::from("");

        assert_eq!(expected, config.traces_endpoint());
        assert_eq!(expected, config.metrics_endpoint());
        assert_eq!(expected, config.logs_endpoint());
    }

    #[test]
    fn test_grpc_resolves_to_base_url_without_path_components() {
        let config = OtelConfig {
            protocol: OtelProtocol::Grpc,
            observability_endpoint: Some(String::from(
                "https://example.com:4318/path/does/not/exist",
            )),
            ..Default::default()
        };

        let expected = String::from("https://example.com:4318");

        assert_eq!(expected, config.traces_endpoint());
        assert_eq!(expected, config.metrics_endpoint());
        assert_eq!(expected, config.logs_endpoint());
    }

    #[test]
    fn test_grpc_resolves_to_signal_specific_overrides_as_provided() {
        let config = OtelConfig {
            protocol: OtelProtocol::Grpc,
            traces_endpoint: Some(String::from("https://example.com:4318/path/does/not/exist")),
            ..Default::default()
        };

        let expected_traces = String::from("https://example.com:4318/path/does/not/exist");
        let expected_others = String::from("");

        assert_eq!(expected_traces, config.traces_endpoint());
        assert_eq!(expected_others, config.metrics_endpoint());
        assert_eq!(expected_others, config.logs_endpoint());
    }

    #[test]
    fn test_http_resolves_to_empty_string_without_overrides() {
        let config = OtelConfig {
            protocol: OtelProtocol::Http,
            ..Default::default()
        };

        let expected = String::from("");

        assert_eq!(expected, config.traces_endpoint());
        assert_eq!(expected, config.metrics_endpoint());
        assert_eq!(expected, config.logs_endpoint());
    }

    #[test]
    fn test_http_configuration_for_specific_signal_should_not_affect_other_signals() {
        let config = OtelConfig {
            protocol: OtelProtocol::Http,
            traces_endpoint: Some(String::from(
                "https://example.com:4318/v1/traces/or/something",
            )),
            ..Default::default()
        };

        let expected_traces = String::from("https://example.com:4318/v1/traces/or/something");
        let expected_others = String::from("");

        assert_eq!(expected_traces, config.traces_endpoint());
        assert_eq!(expected_others, config.metrics_endpoint());
        assert_eq!(expected_others, config.logs_endpoint());
    }

    #[test]
    fn test_http_should_be_configurable_across_all_signals_via_observability_endpoint() {
        let config = OtelConfig {
            protocol: OtelProtocol::Http,
            observability_endpoint: Some(String::from("https://example.com:4318")),
            ..Default::default()
        };

        let expected_traces = String::from("https://example.com:4318/v1/traces");
        let expected_metrics = String::from("https://example.com:4318/v1/metrics");
        let expected_logs = String::from("https://example.com:4318/v1/logs");

        assert_eq!(expected_traces, config.traces_endpoint());
        assert_eq!(expected_metrics, config.metrics_endpoint());
        assert_eq!(expected_logs, config.logs_endpoint());
    }
}