@@ -186,13 +186,12 @@ pub trait WithExportConfig {
186
186
///
187
187
/// Note that protocols that are not supported by exporters will be ignore. The exporter
188
188
/// will use default protocol in this case.
189
+ ///
190
+ /// ## Note
191
+ /// All exporters in this crate are only support one protocol thus choosing the protocol is an no-op at the moment
189
192
fn with_protocol ( self , protocol : Protocol ) -> Self ;
190
193
/// Set the timeout to the collector.
191
194
fn with_timeout ( self , timeout : Duration ) -> Self ;
192
- /// Set the trace provider configuration from the given environment variables.
193
- ///
194
- /// If the value in environment variables is illegal, will fall back to use default value.
195
- fn with_env ( self ) -> Self ;
196
195
/// Set export config. This will override all previous configuration.
197
196
fn with_export_config ( self , export_config : ExportConfig ) -> Self ;
198
197
}
@@ -213,184 +212,10 @@ impl<B: HasExportConfig> WithExportConfig for B {
213
212
self
214
213
}
215
214
216
- fn with_env ( mut self ) -> Self {
217
- let protocol = match std:: env:: var ( OTEL_EXPORTER_OTLP_PROTOCOL )
218
- . unwrap_or_else ( |_| OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT . to_string ( ) )
219
- . as_str ( )
220
- {
221
- OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF => Protocol :: HttpBinary ,
222
- OTEL_EXPORTER_OTLP_PROTOCOL_GRPC => Protocol :: Grpc ,
223
- _ => default_protocol ( ) ,
224
- } ;
225
-
226
- self . export_config ( ) . protocol = protocol;
227
-
228
- let endpoint = match std:: env:: var ( OTEL_EXPORTER_OTLP_ENDPOINT ) {
229
- Ok ( val) => val,
230
- Err ( _) => default_endpoint ( protocol) ,
231
- } ;
232
- self . export_config ( ) . endpoint = endpoint;
233
-
234
- let timeout = match std:: env:: var ( OTEL_EXPORTER_OTLP_TIMEOUT ) {
235
- Ok ( val) => u64:: from_str ( & val) . unwrap_or ( OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT ) ,
236
- Err ( _) => OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT ,
237
- } ;
238
- self . export_config ( ) . timeout = Duration :: from_secs ( timeout) ;
239
- self
240
- }
241
-
242
215
fn with_export_config ( mut self , exporter_config : ExportConfig ) -> Self {
243
216
self . export_config ( ) . endpoint = exporter_config. endpoint ;
244
217
self . export_config ( ) . protocol = exporter_config. protocol ;
245
218
self . export_config ( ) . timeout = exporter_config. timeout ;
246
219
self
247
220
}
248
221
}
249
-
250
- #[ cfg( test) ]
251
- #[ cfg( feature = "grpc-tonic" ) ]
252
- mod tests {
253
- // If an env test fails then the mutex will be poisoned and the following error will be displayed.
254
- const LOCK_POISONED_MESSAGE : & str = "one of the other pipeline builder from env tests failed" ;
255
-
256
- use crate :: exporter:: {
257
- default_endpoint, default_protocol, WithExportConfig , OTEL_EXPORTER_OTLP_ENDPOINT ,
258
- OTEL_EXPORTER_OTLP_GRPC_ENDPOINT_DEFAULT , OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT ,
259
- OTEL_EXPORTER_OTLP_PROTOCOL_GRPC , OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF ,
260
- OTEL_EXPORTER_OTLP_TIMEOUT , OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT ,
261
- } ;
262
- use crate :: { new_exporter, Compression , Protocol , OTEL_EXPORTER_OTLP_PROTOCOL } ;
263
- use std:: str:: FromStr ;
264
- use std:: sync:: Mutex ;
265
-
266
- // Make sure env tests are not running concurrently
267
- static ENV_LOCK : Mutex < usize > = Mutex :: new ( 0 ) ;
268
-
269
- #[ test]
270
- fn test_pipeline_builder_from_env_default_vars ( ) {
271
- let _env_lock = ENV_LOCK . lock ( ) . expect ( LOCK_POISONED_MESSAGE ) ;
272
- let exporter_builder = new_exporter ( ) . tonic ( ) . with_env ( ) ;
273
- assert_eq ! (
274
- exporter_builder. exporter_config. protocol,
275
- default_protocol( )
276
- ) ;
277
- assert_eq ! (
278
- exporter_builder. exporter_config. endpoint,
279
- default_endpoint( default_protocol( ) )
280
- ) ;
281
- assert_eq ! (
282
- exporter_builder. exporter_config. timeout,
283
- std:: time:: Duration :: from_secs( OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT )
284
- ) ;
285
- }
286
-
287
- #[ test]
288
- fn test_pipeline_builder_from_env_endpoint ( ) {
289
- let _env_lock = ENV_LOCK . lock ( ) . expect ( LOCK_POISONED_MESSAGE ) ;
290
- std:: env:: set_var ( OTEL_EXPORTER_OTLP_ENDPOINT , "http://example.com" ) ;
291
- let exporter_builder = new_exporter ( ) . tonic ( ) . with_env ( ) ;
292
- assert_eq ! (
293
- exporter_builder. exporter_config. endpoint,
294
- "http://example.com"
295
- ) ;
296
- std:: env:: remove_var ( OTEL_EXPORTER_OTLP_ENDPOINT ) ;
297
- assert ! ( std:: env:: var( OTEL_EXPORTER_OTLP_ENDPOINT ) . is_err( ) ) ;
298
- }
299
-
300
- #[ test]
301
- fn test_pipeline_builder_from_env_protocol_http_protobuf ( ) {
302
- let _env_lock = ENV_LOCK . lock ( ) . expect ( LOCK_POISONED_MESSAGE ) ;
303
- std:: env:: set_var (
304
- OTEL_EXPORTER_OTLP_PROTOCOL ,
305
- OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF ,
306
- ) ;
307
- let exporter_builder = new_exporter ( ) . tonic ( ) . with_env ( ) ;
308
- assert_eq ! (
309
- exporter_builder. exporter_config. protocol,
310
- Protocol :: HttpBinary
311
- ) ;
312
- assert_eq ! (
313
- exporter_builder. exporter_config. endpoint,
314
- OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT
315
- ) ;
316
-
317
- std:: env:: remove_var ( OTEL_EXPORTER_OTLP_PROTOCOL ) ;
318
- assert ! ( std:: env:: var( OTEL_EXPORTER_OTLP_PROTOCOL ) . is_err( ) ) ;
319
- }
320
-
321
- #[ test]
322
- fn test_pipeline_builder_from_env_protocol_grpc ( ) {
323
- let _env_lock = ENV_LOCK . lock ( ) . expect ( LOCK_POISONED_MESSAGE ) ;
324
- std:: env:: set_var (
325
- OTEL_EXPORTER_OTLP_PROTOCOL ,
326
- OTEL_EXPORTER_OTLP_PROTOCOL_GRPC ,
327
- ) ;
328
- let exporter_builder = new_exporter ( ) . tonic ( ) . with_env ( ) ;
329
- assert_eq ! ( exporter_builder. exporter_config. protocol, Protocol :: Grpc ) ;
330
- assert_eq ! (
331
- exporter_builder. exporter_config. endpoint,
332
- OTEL_EXPORTER_OTLP_GRPC_ENDPOINT_DEFAULT
333
- ) ;
334
-
335
- std:: env:: remove_var ( OTEL_EXPORTER_OTLP_PROTOCOL ) ;
336
- assert ! ( std:: env:: var( OTEL_EXPORTER_OTLP_PROTOCOL ) . is_err( ) ) ;
337
- }
338
-
339
- #[ test]
340
- fn test_pipeline_builder_from_env_bad_protocol ( ) {
341
- let _env_lock = ENV_LOCK . lock ( ) . expect ( LOCK_POISONED_MESSAGE ) ;
342
- std:: env:: set_var ( OTEL_EXPORTER_OTLP_PROTOCOL , "bad_protocol" ) ;
343
- let exporter_builder = new_exporter ( ) . tonic ( ) . with_env ( ) ;
344
- assert_eq ! (
345
- exporter_builder. exporter_config. protocol,
346
- default_protocol( )
347
- ) ;
348
- assert_eq ! (
349
- exporter_builder. exporter_config. endpoint,
350
- default_endpoint( default_protocol( ) )
351
- ) ;
352
-
353
- std:: env:: remove_var ( OTEL_EXPORTER_OTLP_PROTOCOL ) ;
354
- assert ! ( std:: env:: var( OTEL_EXPORTER_OTLP_PROTOCOL ) . is_err( ) ) ;
355
- }
356
-
357
- #[ test]
358
- fn test_pipeline_builder_from_env_timeout ( ) {
359
- let _env_lock = ENV_LOCK . lock ( ) . expect ( LOCK_POISONED_MESSAGE ) ;
360
- std:: env:: set_var ( OTEL_EXPORTER_OTLP_TIMEOUT , "60" ) ;
361
- let exporter_builder = new_exporter ( ) . tonic ( ) . with_env ( ) ;
362
- assert_eq ! (
363
- exporter_builder. exporter_config. timeout,
364
- std:: time:: Duration :: from_secs( 60 )
365
- ) ;
366
-
367
- std:: env:: remove_var ( OTEL_EXPORTER_OTLP_TIMEOUT ) ;
368
- assert ! ( std:: env:: var( OTEL_EXPORTER_OTLP_TIMEOUT ) . is_err( ) ) ;
369
- }
370
-
371
- #[ test]
372
- fn test_pipeline_builder_from_env_bad_timeout ( ) {
373
- let _env_lock = ENV_LOCK . lock ( ) . expect ( LOCK_POISONED_MESSAGE ) ;
374
- std:: env:: set_var ( OTEL_EXPORTER_OTLP_TIMEOUT , "bad_timeout" ) ;
375
-
376
- let exporter_builder = new_exporter ( ) . tonic ( ) . with_env ( ) ;
377
- assert_eq ! (
378
- exporter_builder. exporter_config. timeout,
379
- std:: time:: Duration :: from_secs( OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT )
380
- ) ;
381
-
382
- std:: env:: remove_var ( OTEL_EXPORTER_OTLP_TIMEOUT ) ;
383
- assert ! ( std:: env:: var( OTEL_EXPORTER_OTLP_TIMEOUT ) . is_err( ) ) ;
384
- }
385
-
386
- #[ test]
387
- fn test_compression_parse ( ) {
388
- assert_eq ! ( Compression :: from_str( "gzip" ) . unwrap( ) , Compression :: Gzip ) ;
389
- Compression :: from_str ( "bad_compression" ) . expect_err ( "bad compression" ) ;
390
- }
391
-
392
- #[ test]
393
- fn test_compression_to_str ( ) {
394
- assert_eq ! ( Compression :: Gzip . to_string( ) , "gzip" ) ;
395
- }
396
- }
0 commit comments