1
+ use crate :: utils:: from_env:: { FromEnv , FromEnvErr , FromEnvVar } ;
1
2
use opentelemetry:: { trace:: TracerProvider , KeyValue } ;
2
3
use opentelemetry_sdk:: trace:: SdkTracerProvider ;
3
4
use opentelemetry_sdk:: Resource ;
@@ -10,12 +11,7 @@ use tracing::level_filters::LevelFilter;
10
11
use tracing_subscriber:: Layer ;
11
12
use url:: Url ;
12
13
13
- use crate :: utils:: from_env:: { FromEnv , FromEnvErr , FromEnvVar } ;
14
-
15
- use super :: from_env:: parse_env_if_present;
16
-
17
- const OTEL_ENDPOINT : & str = "OTEL_ENDPOINT" ;
18
- const OTEL_PROTOCOL : & str = "OTEL_PROTOCOL" ;
14
+ const OTEL_ENDPOINT : & str = "OTEL_EXPORTER_OTLP_ENDPOINT" ;
19
15
const OTEL_LEVEL : & str = "OTEL_LEVEL" ;
20
16
const OTEL_TIMEOUT : & str = "OTEL_TIMEOUT" ;
21
17
const OTEL_ENVIRONMENT : & str = "OTEL_ENVIRONMENT_NAME" ;
@@ -65,18 +61,6 @@ impl Drop for OtelGuard {
65
61
}
66
62
}
67
63
68
- /// OTLP protocol choices
69
- #[ derive( Debug , Clone , Copy , Default , PartialEq , Eq ) ]
70
- pub enum OtlpProtocols {
71
- /// GRPC.
72
- Grpc ,
73
- /// Binary.
74
- Binary ,
75
- /// JSON.
76
- #[ default]
77
- Json ,
78
- }
79
-
80
64
/// Otlp parse error.
81
65
#[ derive( Debug , Clone , PartialEq , Eq ) ]
82
66
pub struct OtlpParseError ( String ) ;
@@ -95,45 +79,12 @@ impl core::fmt::Display for OtlpParseError {
95
79
96
80
impl core:: error:: Error for OtlpParseError { }
97
81
98
- impl FromEnvVar for OtlpProtocols {
99
- type Error = OtlpParseError ;
100
-
101
- fn from_env_var ( env_var : & str ) -> Result < Self , FromEnvErr < Self :: Error > > {
102
- parse_env_if_present ( env_var)
103
- }
104
- }
105
-
106
- impl std:: str:: FromStr for OtlpProtocols {
107
- type Err = OtlpParseError ;
108
-
109
- fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
110
- match s {
111
- s if s. eq_ignore_ascii_case ( "grpc" ) => Ok ( Self :: Grpc ) ,
112
- s if s. eq_ignore_ascii_case ( "binary" ) => Ok ( Self :: Binary ) ,
113
- s if s. eq_ignore_ascii_case ( "json" ) => Ok ( Self :: Json ) ,
114
- _ => Err ( OtlpParseError ( format ! ( "Invalid protocol: {}" , s) ) ) ,
115
- }
116
- }
117
- }
118
-
119
- impl From < OtlpProtocols > for opentelemetry_otlp:: Protocol {
120
- fn from ( protocol : OtlpProtocols ) -> Self {
121
- match protocol {
122
- OtlpProtocols :: Grpc => Self :: Grpc ,
123
- OtlpProtocols :: Binary => Self :: HttpBinary ,
124
- OtlpProtocols :: Json => Self :: HttpJson ,
125
- }
126
- }
127
- }
128
-
129
82
/// Otel configuration. This struct is intended to be loaded from the env vars
130
83
///
131
84
/// The env vars it checks are:
132
- /// - OTEL_ENDPOINT - optional. The endpoint to send traces to, should be some
133
- /// valid URL. If not specified, then [`OtelConfig::load`] will return
134
- /// [`None`].
135
- /// - OTEL_PROTOCOL - optional. Specifies the OTLP protocol to use, should be
136
- /// one of "grpc", "binary" or "json". Defaults to json.
85
+ /// - `OTEL_EXPORTER_OTLP_ENDPOINT` - optional. The endpoint to send traces to,
86
+ /// should be some valid URL. If not specified, then [`OtelConfig::load`]
87
+ /// will return [`None`].
137
88
/// - OTEL_LEVEL - optional. Specifies the minimum [`tracing::Level`] to
138
89
/// export. Defaults to [`tracing::Level::DEBUG`].
139
90
/// - OTEL_TIMEOUT - optional. Specifies the timeout for the exporter in
@@ -146,8 +97,7 @@ pub struct OtelConfig {
146
97
/// The endpoint to send traces to, should be some valid HTTP endpoint for
147
98
/// OTLP.
148
99
pub endpoint : Url ,
149
- /// Defaults to JSON.
150
- pub protocol : OtlpProtocols ,
100
+
151
101
/// Defaults to DEBUG.
152
102
pub level : tracing:: Level ,
153
103
/// Defaults to 1 second. Specified in Milliseconds.
@@ -164,8 +114,6 @@ impl FromEnv for OtelConfig {
164
114
// load endpoint from env. ignore empty values (shortcut return None), parse, and print the error if any using inspect_err
165
115
let endpoint = Url :: from_env_var ( OTEL_ENDPOINT ) . inspect_err ( |e| eprintln ! ( "{e}" ) ) ?;
166
116
167
- let protocol = OtlpProtocols :: from_env_var ( OTEL_PROTOCOL ) . unwrap_or_default ( ) ;
168
-
169
117
let level = tracing:: Level :: from_env_var ( OTEL_LEVEL ) . unwrap_or ( tracing:: Level :: DEBUG ) ;
170
118
171
119
let timeout = Duration :: from_env_var ( OTEL_TIMEOUT ) . unwrap_or ( Duration :: from_millis ( 1000 ) ) ;
@@ -174,7 +122,6 @@ impl FromEnv for OtelConfig {
174
122
175
123
Ok ( Self {
176
124
endpoint,
177
- protocol,
178
125
level,
179
126
timeout,
180
127
environment,
@@ -186,8 +133,6 @@ impl OtelConfig {
186
133
/// Load from env vars.
187
134
///
188
135
/// The env vars it checks are:
189
- /// - `OTEL_ENDPOINT` - optional. The endpoint to send traces to, should be
190
- /// some valid URL. If not specified, then [`OtelConfig::load`] will
191
136
/// return [`None`].
192
137
/// - `OTEL_PROTOCOL` - optional. Specifies the OTLP protocol to use, should
193
138
/// be one of "grpc", "binary" or "json". Defaults to json.
@@ -242,7 +187,6 @@ mod test {
242
187
243
188
fn clear_env ( ) {
244
189
std:: env:: remove_var ( OTEL_ENDPOINT ) ;
245
- std:: env:: remove_var ( OTEL_PROTOCOL ) ;
246
190
std:: env:: remove_var ( OTEL_LEVEL ) ;
247
191
std:: env:: remove_var ( OTEL_TIMEOUT ) ;
248
192
std:: env:: remove_var ( OTEL_ENVIRONMENT ) ;
@@ -265,25 +209,12 @@ mod test {
265
209
266
210
let cfg = OtelConfig :: load ( ) . unwrap ( ) ;
267
211
assert_eq ! ( cfg. endpoint, URL . parse( ) . unwrap( ) ) ;
268
- assert_eq ! ( cfg. protocol, OtlpProtocols :: Json ) ;
269
212
assert_eq ! ( cfg. level, tracing:: Level :: DEBUG ) ;
270
213
assert_eq ! ( cfg. timeout, std:: time:: Duration :: from_millis( 1000 ) ) ;
271
214
assert_eq ! ( cfg. environment, "unknown" ) ;
272
215
} )
273
216
}
274
217
275
- #[ test]
276
- #[ serial_test:: serial]
277
- fn test_env_read_protocol ( ) {
278
- run_clear_env ( || {
279
- std:: env:: set_var ( OTEL_ENDPOINT , URL ) ;
280
- std:: env:: set_var ( OTEL_PROTOCOL , "grpc" ) ;
281
-
282
- let cfg = OtelConfig :: load ( ) . unwrap ( ) ;
283
- assert_eq ! ( cfg. protocol, OtlpProtocols :: Grpc ) ;
284
- } )
285
- }
286
-
287
218
#[ test]
288
219
#[ serial_test:: serial]
289
220
fn test_env_read_level ( ) {
0 commit comments