-
Notifications
You must be signed in to change notification settings - Fork 502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement FromStr
and Display
for Protocol
#2758
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary? Why would a user have this awkward construct: .with_protocol("grpc".parse::<Protocol>().unwrap()) when they could simply do this: .with_protocol(Protocol::Grpc) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Completely agree if hard coded then users should use the enum variant. I am working on a downstream SDK which uses Other downstream packages to set up an exporter, like It feels most correct to have the |
||
type Err = Error; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
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::<Protocol>().unwrap(), Protocol::Grpc); | ||
assert_eq!( | ||
"http/protobuf".parse::<Protocol>().unwrap(), | ||
Protocol::HttpBinary | ||
); | ||
assert_eq!("http/json".parse::<Protocol>().unwrap(), Protocol::HttpJson); | ||
assert!("invalid".parse::<Protocol>().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"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the motivation behind adding a
Display
implementation forProtocol
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After seeing the
Compression
implementation within this library had bothFromStr
andDisplay
, I felt it was most correct to addDisplay
to match theFromStr
here too.If you have concerns, I am happy to remove it - the
FromStr
is the functionality I need at present (as per above comment).