@@ -221,6 +221,9 @@ mod metric;
221
221
#[ cfg( any( feature = "http-proto" , feature = "http-json" , feature = "grpc-tonic" ) ) ]
222
222
mod span;
223
223
224
+ use std:: fmt:: Display ;
225
+ use std:: str:: FromStr ;
226
+
224
227
pub use crate :: exporter:: Compression ;
225
228
pub use crate :: exporter:: ExportConfig ;
226
229
#[ cfg( feature = "trace" ) ]
@@ -257,6 +260,9 @@ pub use crate::exporter::{
257
260
OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT ,
258
261
} ;
259
262
263
+ use exporter:: OTEL_EXPORTER_OTLP_PROTOCOL_GRPC ;
264
+ use exporter:: OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON ;
265
+ use exporter:: OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF ;
260
266
use opentelemetry_sdk:: ExportError ;
261
267
262
268
/// Type to indicate the builder does not have a client set.
@@ -352,6 +358,10 @@ pub enum Error {
352
358
#[ cfg( any( not( feature = "gzip-tonic" ) , not( feature = "zstd-tonic" ) ) ) ]
353
359
#[ error( "feature '{0}' is required to use the compression algorithm '{1}'" ) ]
354
360
FeatureRequiredForCompressionAlgorithm ( & ' static str , Compression ) ,
361
+
362
+ /// Unsupported protocol.
363
+ #[ error( "unsupported protocol '{0}'" ) ]
364
+ UnsupportedProtocol ( String ) ,
355
365
}
356
366
357
367
#[ cfg( feature = "grpc-tonic" ) ]
@@ -396,7 +406,53 @@ pub enum Protocol {
396
406
HttpJson ,
397
407
}
398
408
409
+ impl Display for Protocol {
410
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
411
+ match self {
412
+ Protocol :: Grpc => write ! ( f, "{}" , OTEL_EXPORTER_OTLP_PROTOCOL_GRPC ) ,
413
+ Protocol :: HttpBinary => write ! ( f, "{}" , OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF ) ,
414
+ Protocol :: HttpJson => write ! ( f, "{}" , OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON ) ,
415
+ }
416
+ }
417
+ }
418
+
419
+ impl FromStr for Protocol {
420
+ type Err = Error ;
421
+
422
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
423
+ match s {
424
+ OTEL_EXPORTER_OTLP_PROTOCOL_GRPC => Ok ( Protocol :: Grpc ) ,
425
+ OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF => Ok ( Protocol :: HttpBinary ) ,
426
+ OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON => Ok ( Protocol :: HttpJson ) ,
427
+ _ => Err ( Error :: UnsupportedProtocol ( s. to_string ( ) ) ) ,
428
+ }
429
+ }
430
+ }
431
+
399
432
#[ derive( Debug , Default ) ]
400
433
#[ doc( hidden) ]
401
434
/// Placeholder type when no exporter pipeline has been configured in telemetry pipeline.
402
435
pub struct NoExporterConfig ( ( ) ) ;
436
+
437
+ #[ cfg( test) ]
438
+ mod tests {
439
+ use super :: * ;
440
+
441
+ #[ test]
442
+ fn test_parse_protocol ( ) {
443
+ assert_eq ! ( "grpc" . parse:: <Protocol >( ) . unwrap( ) , Protocol :: Grpc ) ;
444
+ assert_eq ! (
445
+ "http/protobuf" . parse:: <Protocol >( ) . unwrap( ) ,
446
+ Protocol :: HttpBinary
447
+ ) ;
448
+ assert_eq ! ( "http/json" . parse:: <Protocol >( ) . unwrap( ) , Protocol :: HttpJson ) ;
449
+ assert ! ( "invalid" . parse:: <Protocol >( ) . is_err( ) ) ;
450
+ }
451
+
452
+ #[ test]
453
+ fn test_display_protocol ( ) {
454
+ assert_eq ! ( Protocol :: Grpc . to_string( ) , "grpc" ) ;
455
+ assert_eq ! ( Protocol :: HttpBinary . to_string( ) , "http/protobuf" ) ;
456
+ assert_eq ! ( Protocol :: HttpJson . to_string( ) , "http/json" ) ;
457
+ }
458
+ }
0 commit comments