Skip to content
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

zstd compression for tonic exporter #1947

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions opentelemetry-otlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ default = ["grpc-tonic", "trace", "metrics", "logs"]
# grpc using tonic
grpc-tonic = ["tonic", "prost", "http", "tokio", "opentelemetry-proto/gen-tonic"]
gzip-tonic = ["tonic/gzip"]
zstd-tonic = ["tonic/zstd"]
tls = ["tonic/tls"]
tls-roots = ["tls", "tonic/tls-roots"]
tls-webpki-roots = ["tls", "tonic/tls-webpki-roots"]
Expand Down
4 changes: 4 additions & 0 deletions opentelemetry-otlp/src/exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,15 @@
pub enum Compression {
/// Compresses data using gzip.
Gzip,
/// Compresses data using zstd.
Zstd,
}

impl Display for Compression {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Compression::Gzip => write!(f, "gzip"),
Compression::Zstd => write!(f, "zstd"),

Check warning on line 109 in opentelemetry-otlp/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/mod.rs#L109

Added line #L109 was not covered by tests
}
}
}
Expand All @@ -114,6 +117,7 @@
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"gzip" => Ok(Compression::Gzip),
"zstd" => Ok(Compression::Zstd),

Check warning on line 120 in opentelemetry-otlp/src/exporter/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-otlp/src/exporter/mod.rs#L120

Added line #L120 was not covered by tests
_ => Err(Error::UnsupportedCompressionAlgorithm(s.to_string())),
}
}
Expand Down
36 changes: 32 additions & 4 deletions opentelemetry-otlp/src/exporter/tonic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ impl TryFrom<Compression> for tonic::codec::CompressionEncoding {
#[cfg(feature = "gzip-tonic")]
Compression::Gzip => Ok(tonic::codec::CompressionEncoding::Gzip),
#[cfg(not(feature = "gzip-tonic"))]
Compression::Gzip => Err(crate::Error::UnsupportedCompressionAlgorithm(
value.to_string(),
Compression::Gzip => Err(crate::Error::FeatureRequiredForCompressionAlgorithm(
"gzip-tonic",
Compression::Gzip,
)),
#[cfg(feature = "zstd-tonic")]
Compression::Zstd => Ok(tonic::codec::CompressionEncoding::Zstd),
#[cfg(not(feature = "zstd-tonic"))]
Compression::Zstd => Err(crate::Error::FeatureRequiredForCompressionAlgorithm(
"zstd-tonic",
Compression::Zstd,
)),
}
}
Expand Down Expand Up @@ -399,7 +407,7 @@ fn parse_headers_from_env(signal_headers_var: &str) -> HeaderMap {
#[cfg(test)]
mod tests {
use crate::exporter::tests::run_env_test;
#[cfg(feature = "gzip-tonic")]
#[cfg(feature = "grpc-tonic")]
use crate::exporter::Compression;
use crate::TonicExporterBuilder;
use crate::{OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_TRACES_HEADERS};
Expand Down Expand Up @@ -438,14 +446,34 @@ mod tests {

#[test]
#[cfg(feature = "gzip-tonic")]
fn test_with_compression() {
fn test_with_gzip_compression() {
// metadata should merge with the current one with priority instead of just replacing it
let mut metadata = MetadataMap::new();
metadata.insert("foo", "bar".parse().unwrap());
let builder = TonicExporterBuilder::default().with_compression(Compression::Gzip);
assert_eq!(builder.tonic_config.compression.unwrap(), Compression::Gzip);
}

#[test]
#[cfg(feature = "zstd-tonic")]
fn test_with_zstd_compression() {
let builder = TonicExporterBuilder::default().with_compression(Compression::Zstd);
assert_eq!(builder.tonic_config.compression.unwrap(), Compression::Zstd);
}

#[test]
#[cfg(feature = "grpc-tonic")]
fn test_convert_compression() {
#[cfg(feature = "gzip-tonic")]
assert!(tonic::codec::CompressionEncoding::try_from(Compression::Gzip).is_ok());
#[cfg(not(feature = "gzip-tonic"))]
assert!(tonic::codec::CompressionEncoding::try_from(Compression::Gzip).is_err());
#[cfg(feature = "zstd-tonic")]
assert!(tonic::codec::CompressionEncoding::try_from(Compression::Zstd).is_ok());
#[cfg(not(feature = "zstd-tonic"))]
assert!(tonic::codec::CompressionEncoding::try_from(Compression::Zstd).is_err());
}

#[test]
fn test_parse_headers_from_env() {
run_env_test(
Expand Down
6 changes: 6 additions & 0 deletions opentelemetry-otlp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
//! For users uses `tonic` as grpc layer:
//! * `grpc-tonic`: Use `tonic` as grpc layer. This is enabled by default.
//! * `gzip-tonic`: Use gzip compression for `tonic` grpc layer.
//! * `zstd-tonic`: Use zstd compression for `tonic` grpc layer.
//! * `tls-tonic`: Enable TLS.
//! * `tls-roots`: Adds system trust roots to rustls-based gRPC clients using the rustls-native-certs crate
//! * `tls-webkpi-roots`: Embeds Mozilla's trust roots to rustls-based gRPC clients using the webkpi-roots crate
Expand Down Expand Up @@ -372,6 +373,11 @@ pub enum Error {
/// Unsupported compression algorithm.
#[error("unsupported compression algorithm '{0}'")]
UnsupportedCompressionAlgorithm(String),

/// Feature required to use the specified compression algorithm.
#[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),
}

#[cfg(feature = "grpc-tonic")]
Expand Down
Loading