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
#[cfg(feature = "grpc-sys")]
use crate::exporter::grpcio::GrpcioExporterBuilder;
#[cfg(feature = "http-proto")]
use crate::exporter::http::HttpExporterBuilder;
#[cfg(feature = "grpc-tonic")]
use crate::exporter::tonic::TonicExporterBuilder;
use crate::Protocol;
use std::str::FromStr;
use std::time::Duration;
pub const OTEL_EXPORTER_OTLP_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_ENDPOINT";
pub const OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT: &str = "https://localhost:4317";
pub const OTEL_EXPORTER_OTLP_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_TIMEOUT";
pub const OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT: u64 = 10;
#[cfg(feature = "grpc-sys")]
pub(crate) mod grpcio;
#[cfg(feature = "http-proto")]
pub(crate) mod http;
#[cfg(feature = "grpc-tonic")]
pub(crate) mod tonic;
#[derive(Debug)]
pub struct ExportConfig {
pub endpoint: String,
pub protocol: Protocol,
pub timeout: Duration,
}
impl Default for ExportConfig {
fn default() -> Self {
ExportConfig {
endpoint: OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT.to_string(),
protocol: Protocol::Grpc,
timeout: Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
}
}
}
pub trait HasExportConfig {
fn export_config(&mut self) -> &mut ExportConfig;
}
#[cfg(feature = "grpc-tonic")]
impl HasExportConfig for TonicExporterBuilder {
fn export_config(&mut self) -> &mut ExportConfig {
&mut self.exporter_config
}
}
#[cfg(feature = "grpc-sys")]
impl HasExportConfig for GrpcioExporterBuilder {
fn export_config(&mut self) -> &mut ExportConfig {
&mut self.exporter_config
}
}
#[cfg(feature = "http-proto")]
impl HasExportConfig for HttpExporterBuilder {
fn export_config(&mut self) -> &mut ExportConfig {
&mut self.exporter_config
}
}
pub trait WithExportConfig {
fn with_endpoint<T: Into<String>>(self, endpoint: T) -> Self;
fn with_protocol(self, protocol: Protocol) -> Self;
fn with_timeout(self, timeout: Duration) -> Self;
fn with_env(self) -> Self;
fn with_export_config(self, export_config: ExportConfig) -> Self;
}
impl<B: HasExportConfig> WithExportConfig for B {
fn with_endpoint<T: Into<String>>(mut self, endpoint: T) -> Self {
self.export_config().endpoint = endpoint.into();
self
}
fn with_protocol(mut self, protocol: Protocol) -> Self {
self.export_config().protocol = protocol;
self
}
fn with_timeout(mut self, timeout: Duration) -> Self {
self.export_config().timeout = timeout;
self
}
fn with_env(mut self) -> Self {
let endpoint = match std::env::var(OTEL_EXPORTER_OTLP_ENDPOINT) {
Ok(val) => val,
Err(_) => OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT.to_string(),
};
self.export_config().endpoint = endpoint;
let timeout = match std::env::var(OTEL_EXPORTER_OTLP_TIMEOUT) {
Ok(val) => u64::from_str(&val).unwrap_or(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
Err(_) => OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT,
};
self.export_config().timeout = Duration::from_secs(timeout);
self
}
fn with_export_config(mut self, exporter_config: ExportConfig) -> Self {
self.export_config().endpoint = exporter_config.endpoint;
self.export_config().protocol = exporter_config.protocol;
self.export_config().timeout = exporter_config.timeout;
self
}
}
#[cfg(test)]
#[cfg(feature = "grpc-tonic")]
mod tests {
use crate::exporter::{
WithExportConfig, OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_TIMEOUT,
OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT,
};
use crate::new_exporter;
#[test]
fn test_pipeline_builder_from_env_default_vars() {
let expected_endpoint = "https://otlp_endpoint:4317";
std::env::set_var(OTEL_EXPORTER_OTLP_ENDPOINT, expected_endpoint);
std::env::set_var(OTEL_EXPORTER_OTLP_TIMEOUT, "bad_timeout");
let mut exporter_builder = new_exporter().tonic().with_env();
assert_eq!(exporter_builder.exporter_config.endpoint, expected_endpoint);
exporter_builder = new_exporter().tonic().with_env();
assert_eq!(
exporter_builder.exporter_config.timeout,
std::time::Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT)
);
std::env::set_var(OTEL_EXPORTER_OTLP_TIMEOUT, "60");
exporter_builder = new_exporter().tonic().with_env();
assert_eq!(
exporter_builder.exporter_config.timeout,
std::time::Duration::from_secs(60)
);
std::env::remove_var(OTEL_EXPORTER_OTLP_ENDPOINT);
std::env::remove_var(OTEL_EXPORTER_OTLP_TIMEOUT);
assert!(std::env::var(OTEL_EXPORTER_OTLP_ENDPOINT).is_err());
assert!(std::env::var(OTEL_EXPORTER_OTLP_TIMEOUT).is_err());
}
}