Skip to content

Commit eb38eef

Browse files
authored
feat(OTLP): add zstd and gzip compression support to HTTP OTLP exporter (#3092)
1 parent 290f4bc commit eb38eef

File tree

9 files changed

+643
-55
lines changed

9 files changed

+643
-55
lines changed

opentelemetry-otlp/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## vNext
44

5+
- Add HTTP compression support with `gzip-http` and `zstd-http` feature flags
6+
57
## 0.30.0
68

79
Released 2025-May-23

opentelemetry-otlp/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,18 @@ serde = { workspace = true, features = ["derive"], optional = true }
4343
thiserror = { workspace = true }
4444
serde_json = { workspace = true, optional = true }
4545

46+
# compression dependencies
47+
flate2 = { version = "1.1.2", optional = true }
48+
zstd = { version = "0.13", optional = true }
49+
4650
[dev-dependencies]
4751
tokio-stream = { workspace = true, features = ["net"] }
4852
opentelemetry_sdk = { workspace = true, features = ["trace", "testing"] }
4953
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
5054
futures-util = { workspace = true }
5155
temp-env = { workspace = true }
5256
tonic = { workspace = true, features = ["router", "server"] }
57+
async-trait = { workspace = true }
5358

5459
[features]
5560
# telemetry pillars and functions
@@ -67,6 +72,10 @@ default = ["http-proto", "reqwest-blocking-client", "trace", "metrics", "logs",
6772
grpc-tonic = ["tonic", "prost", "http", "tokio", "opentelemetry-proto/gen-tonic"]
6873
gzip-tonic = ["tonic/gzip"]
6974
zstd-tonic = ["tonic/zstd"]
75+
76+
# http compression
77+
gzip-http = ["flate2"]
78+
zstd-http = ["zstd"]
7079
tls = ["tonic/tls-ring"]
7180
tls-roots = ["tls", "tonic/tls-native-roots"]
7281
tls-webpki-roots = ["tls", "tonic/tls-webpki-roots"]

opentelemetry-otlp/src/exporter/http/logs.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::OtlpHttpClient;
2+
use http::header::CONTENT_ENCODING;
23
use http::{header::CONTENT_TYPE, Method};
34
use opentelemetry::otel_debug;
45
use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult};
@@ -14,14 +15,20 @@ impl LogExporter for OtlpHttpClient {
1415
.clone()
1516
.ok_or(OTelSdkError::AlreadyShutdown)?;
1617

17-
let (body, content_type) = self
18+
let (body, content_type, content_encoding) = self
1819
.build_logs_export_body(batch)
1920
.map_err(OTelSdkError::InternalFailure)?;
2021

21-
let mut request = http::Request::builder()
22+
let mut request_builder = http::Request::builder()
2223
.method(Method::POST)
2324
.uri(&self.collector_endpoint)
24-
.header(CONTENT_TYPE, content_type)
25+
.header(CONTENT_TYPE, content_type);
26+
27+
if let Some(encoding) = content_encoding {
28+
request_builder = request_builder.header(CONTENT_ENCODING, encoding);
29+
}
30+
31+
let mut request = request_builder
2532
.body(body.into())
2633
.map_err(|e| OTelSdkError::InternalFailure(e.to_string()))?;
2734

opentelemetry-otlp/src/exporter/http/metrics.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@ impl MetricsClient for OtlpHttpClient {
1919
_ => Err(OTelSdkError::AlreadyShutdown),
2020
})?;
2121

22-
let (body, content_type) = self.build_metrics_export_body(metrics).ok_or_else(|| {
23-
OTelSdkError::InternalFailure("Failed to serialize metrics".to_string())
24-
})?;
25-
let mut request = http::Request::builder()
22+
let (body, content_type, content_encoding) =
23+
self.build_metrics_export_body(metrics).ok_or_else(|| {
24+
OTelSdkError::InternalFailure("Failed to serialize metrics".to_string())
25+
})?;
26+
27+
let mut request_builder = http::Request::builder()
2628
.method(Method::POST)
2729
.uri(&self.collector_endpoint)
28-
.header(CONTENT_TYPE, content_type)
30+
.header(CONTENT_TYPE, content_type);
31+
32+
if let Some(encoding) = content_encoding {
33+
request_builder = request_builder.header("Content-Encoding", encoding);
34+
}
35+
36+
let mut request = request_builder
2937
.body(body.into())
3038
.map_err(|e| OTelSdkError::InternalFailure(format!("{e:?}")))?;
3139

0 commit comments

Comments
 (0)