From e6711a244c6da0436cbef1c34594c26abce548f6 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Wed, 5 Mar 2025 14:40:16 +0000 Subject: [PATCH 1/2] feat: implement `FromStr` and `Display` for `Protocol` --- opentelemetry-otlp/src/exporter/mod.rs | 6 +-- opentelemetry-otlp/src/lib.rs | 56 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/opentelemetry-otlp/src/exporter/mod.rs b/opentelemetry-otlp/src/exporter/mod.rs index c905260857..9bd552e156 100644 --- a/opentelemetry-otlp/src/exporter/mod.rs +++ b/opentelemetry-otlp/src/exporter/mod.rs @@ -45,9 +45,9 @@ pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = OTEL_EXPORTER_OTLP_PROTOCO /// Default protocol if no features are enabled. pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = ""; -const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF: &str = "http/protobuf"; -const OTEL_EXPORTER_OTLP_PROTOCOL_GRPC: &str = "grpc"; -const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON: &str = "http/json"; +pub(crate) const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF: &str = "http/protobuf"; +pub(crate) const OTEL_EXPORTER_OTLP_PROTOCOL_GRPC: &str = "grpc"; +pub(crate) const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON: &str = "http/json"; /// Max waiting time for the backend to process each signal batch, defaults to 10 seconds. pub const OTEL_EXPORTER_OTLP_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_TIMEOUT"; diff --git a/opentelemetry-otlp/src/lib.rs b/opentelemetry-otlp/src/lib.rs index 4d80a017f1..ae05b8de74 100644 --- a/opentelemetry-otlp/src/lib.rs +++ b/opentelemetry-otlp/src/lib.rs @@ -221,6 +221,9 @@ mod metric; #[cfg(any(feature = "http-proto", feature = "http-json", feature = "grpc-tonic"))] mod span; +use std::fmt::Display; +use std::str::FromStr; + pub use crate::exporter::Compression; pub use crate::exporter::ExportConfig; #[cfg(feature = "trace")] @@ -257,6 +260,9 @@ pub use crate::exporter::{ OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT, }; +use exporter::OTEL_EXPORTER_OTLP_PROTOCOL_GRPC; +use exporter::OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON; +use exporter::OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF; use opentelemetry_sdk::ExportError; /// Type to indicate the builder does not have a client set. @@ -352,6 +358,10 @@ pub enum Error { #[cfg(any(not(feature = "gzip-tonic"), not(feature = "zstd-tonic")))] #[error("feature '{0}' is required to use the compression algorithm '{1}'")] FeatureRequiredForCompressionAlgorithm(&'static str, Compression), + + /// Unsupported protocol. + #[error("unsupported protocol '{0}'")] + UnsupportedProtocol(String), } #[cfg(feature = "grpc-tonic")] @@ -396,7 +406,53 @@ pub enum Protocol { HttpJson, } +impl Display for Protocol { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Protocol::Grpc => write!(f, "{}", OTEL_EXPORTER_OTLP_PROTOCOL_GRPC), + Protocol::HttpBinary => write!(f, "{}", OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF), + Protocol::HttpJson => write!(f, "{}", OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON), + } + } +} + +impl FromStr for Protocol { + type Err = Error; + + fn from_str(s: &str) -> Result { + match s { + OTEL_EXPORTER_OTLP_PROTOCOL_GRPC => Ok(Protocol::Grpc), + OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF => Ok(Protocol::HttpBinary), + OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON => Ok(Protocol::HttpJson), + _ => Err(Error::UnsupportedProtocol(s.to_string())), + } + } +} + #[derive(Debug, Default)] #[doc(hidden)] /// Placeholder type when no exporter pipeline has been configured in telemetry pipeline. pub struct NoExporterConfig(()); + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_protocol() { + assert_eq!("grpc".parse::().unwrap(), Protocol::Grpc); + assert_eq!( + "http/protobuf".parse::().unwrap(), + Protocol::HttpBinary + ); + assert_eq!("http/json".parse::().unwrap(), Protocol::HttpJson); + assert!("invalid".parse::().is_err()); + } + + #[test] + fn test_display_protocol() { + assert_eq!(Protocol::Grpc.to_string(), "grpc"); + assert_eq!(Protocol::HttpBinary.to_string(), "http/protobuf"); + assert_eq!(Protocol::HttpJson.to_string(), "http/json"); + } +} From e23f7c92e440662dc41bfddb522b5677b4b856be Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Thu, 6 Mar 2025 09:15:37 +0000 Subject: [PATCH 2/2] add CHANGELOG entry --- opentelemetry-otlp/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/opentelemetry-otlp/CHANGELOG.md b/opentelemetry-otlp/CHANGELOG.md index 311a8a4bfc..9af4d78d30 100644 --- a/opentelemetry-otlp/CHANGELOG.md +++ b/opentelemetry-otlp/CHANGELOG.md @@ -4,6 +4,7 @@ - The `OTEL_EXPORTER_OTLP_TIMEOUT`, `OTEL_EXPORTER_OTLP_TRACES_TIMEOUT`, `OTEL_EXPORTER_OTLP_METRICS_TIMEOUT` and `OTEL_EXPORTER_OTLP_LOGS_TIMEOUT` are changed from seconds to miliseconds. - Fixed `.with_headers()` in `HttpExporterBuilder` to correctly support multiple key/value pairs. [#2699](https://github.com/open-telemetry/opentelemetry-rust/pull/2699) +- Implement `FromStr` and `Display` for `opentelemetry_otlp::Protocol`. [#2758](https://github.com/open-telemetry/opentelemetry-rust/pull/2758) ## 0.28.0