@@ -221,6 +221,9 @@ mod metric;
221221#[ cfg( any( feature = "http-proto" , feature = "http-json" , feature = "grpc-tonic" ) ) ]
222222mod span;
223223
224+ use std:: fmt:: Display ;
225+ use std:: str:: FromStr ;
226+
224227pub use crate :: exporter:: Compression ;
225228pub use crate :: exporter:: ExportConfig ;
226229#[ cfg( feature = "trace" ) ]
@@ -257,6 +260,9 @@ pub use crate::exporter::{
257260 OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT ,
258261} ;
259262
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 ;
260266use opentelemetry_sdk:: ExportError ;
261267
262268/// Type to indicate the builder does not have a client set.
@@ -352,6 +358,10 @@ pub enum Error {
352358 #[ cfg( any( not( feature = "gzip-tonic" ) , not( feature = "zstd-tonic" ) ) ) ]
353359 #[ error( "feature '{0}' is required to use the compression algorithm '{1}'" ) ]
354360 FeatureRequiredForCompressionAlgorithm ( & ' static str , Compression ) ,
361+
362+ /// Unsupported protocol.
363+ #[ error( "unsupported protocol '{0}'" ) ]
364+ UnsupportedProtocol ( String ) ,
355365}
356366
357367#[ cfg( feature = "grpc-tonic" ) ]
@@ -396,7 +406,53 @@ pub enum Protocol {
396406 HttpJson ,
397407}
398408
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+
399432#[ derive( Debug , Default ) ]
400433#[ doc( hidden) ]
401434/// Placeholder type when no exporter pipeline has been configured in telemetry pipeline.
402435pub 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