1+ use crate :: utils:: from_env:: { FromEnv , FromEnvErr , FromEnvVar } ;
12use opentelemetry:: { trace:: TracerProvider , KeyValue } ;
23use opentelemetry_sdk:: trace:: SdkTracerProvider ;
34use opentelemetry_sdk:: Resource ;
@@ -10,12 +11,7 @@ use tracing::level_filters::LevelFilter;
1011use tracing_subscriber:: Layer ;
1112use url:: Url ;
1213
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" ;
1915const OTEL_LEVEL : & str = "OTEL_LEVEL" ;
2016const OTEL_TIMEOUT : & str = "OTEL_TIMEOUT" ;
2117const OTEL_ENVIRONMENT : & str = "OTEL_ENVIRONMENT_NAME" ;
@@ -65,18 +61,6 @@ impl Drop for OtelGuard {
6561 }
6662}
6763
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-
8064/// Otlp parse error.
8165#[ derive( Debug , Clone , PartialEq , Eq ) ]
8266pub struct OtlpParseError ( String ) ;
@@ -95,45 +79,12 @@ impl core::fmt::Display for OtlpParseError {
9579
9680impl core:: error:: Error for OtlpParseError { }
9781
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-
12982/// Otel configuration. This struct is intended to be loaded from the env vars
13083///
13184/// 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`].
13788/// - OTEL_LEVEL - optional. Specifies the minimum [`tracing::Level`] to
13889/// export. Defaults to [`tracing::Level::DEBUG`].
13990/// - OTEL_TIMEOUT - optional. Specifies the timeout for the exporter in
@@ -146,8 +97,7 @@ pub struct OtelConfig {
14697 /// The endpoint to send traces to, should be some valid HTTP endpoint for
14798 /// OTLP.
14899 pub endpoint : Url ,
149- /// Defaults to JSON.
150- pub protocol : OtlpProtocols ,
100+
151101 /// Defaults to DEBUG.
152102 pub level : tracing:: Level ,
153103 /// Defaults to 1 second. Specified in Milliseconds.
@@ -164,8 +114,6 @@ impl FromEnv for OtelConfig {
164114 // load endpoint from env. ignore empty values (shortcut return None), parse, and print the error if any using inspect_err
165115 let endpoint = Url :: from_env_var ( OTEL_ENDPOINT ) . inspect_err ( |e| eprintln ! ( "{e}" ) ) ?;
166116
167- let protocol = OtlpProtocols :: from_env_var ( OTEL_PROTOCOL ) . unwrap_or_default ( ) ;
168-
169117 let level = tracing:: Level :: from_env_var ( OTEL_LEVEL ) . unwrap_or ( tracing:: Level :: DEBUG ) ;
170118
171119 let timeout = Duration :: from_env_var ( OTEL_TIMEOUT ) . unwrap_or ( Duration :: from_millis ( 1000 ) ) ;
@@ -174,7 +122,6 @@ impl FromEnv for OtelConfig {
174122
175123 Ok ( Self {
176124 endpoint,
177- protocol,
178125 level,
179126 timeout,
180127 environment,
@@ -186,8 +133,6 @@ impl OtelConfig {
186133 /// Load from env vars.
187134 ///
188135 /// 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
191136 /// return [`None`].
192137 /// - `OTEL_PROTOCOL` - optional. Specifies the OTLP protocol to use, should
193138 /// be one of "grpc", "binary" or "json". Defaults to json.
@@ -242,7 +187,6 @@ mod test {
242187
243188 fn clear_env ( ) {
244189 std:: env:: remove_var ( OTEL_ENDPOINT ) ;
245- std:: env:: remove_var ( OTEL_PROTOCOL ) ;
246190 std:: env:: remove_var ( OTEL_LEVEL ) ;
247191 std:: env:: remove_var ( OTEL_TIMEOUT ) ;
248192 std:: env:: remove_var ( OTEL_ENVIRONMENT ) ;
@@ -265,25 +209,12 @@ mod test {
265209
266210 let cfg = OtelConfig :: load ( ) . unwrap ( ) ;
267211 assert_eq ! ( cfg. endpoint, URL . parse( ) . unwrap( ) ) ;
268- assert_eq ! ( cfg. protocol, OtlpProtocols :: Json ) ;
269212 assert_eq ! ( cfg. level, tracing:: Level :: DEBUG ) ;
270213 assert_eq ! ( cfg. timeout, std:: time:: Duration :: from_millis( 1000 ) ) ;
271214 assert_eq ! ( cfg. environment, "unknown" ) ;
272215 } )
273216 }
274217
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-
287218 #[ test]
288219 #[ serial_test:: serial]
289220 fn test_env_read_level ( ) {
0 commit comments