Skip to content

Commit e6711a2

Browse files
committed
feat: implement FromStr and Display for Protocol
1 parent baf4bfd commit e6711a2

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

opentelemetry-otlp/src/exporter/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = OTEL_EXPORTER_OTLP_PROTOCO
4545
/// Default protocol if no features are enabled.
4646
pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = "";
4747

48-
const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF: &str = "http/protobuf";
49-
const OTEL_EXPORTER_OTLP_PROTOCOL_GRPC: &str = "grpc";
50-
const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON: &str = "http/json";
48+
pub(crate) const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF: &str = "http/protobuf";
49+
pub(crate) const OTEL_EXPORTER_OTLP_PROTOCOL_GRPC: &str = "grpc";
50+
pub(crate) const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON: &str = "http/json";
5151

5252
/// Max waiting time for the backend to process each signal batch, defaults to 10 seconds.
5353
pub const OTEL_EXPORTER_OTLP_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_TIMEOUT";

opentelemetry-otlp/src/lib.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ mod metric;
221221
#[cfg(any(feature = "http-proto", feature = "http-json", feature = "grpc-tonic"))]
222222
mod span;
223223

224+
use std::fmt::Display;
225+
use std::str::FromStr;
226+
224227
pub use crate::exporter::Compression;
225228
pub 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;
260266
use 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.
402435
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

Comments
 (0)