Skip to main content

a2a_protocol_server/otel/telemetry/
compression.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F. <[email protected]> (https://github.com/tomtom215)
3
4//! Compression of OTLP export bodies.
5//!
6//! `OTEL_EXPORTER_OTLP_COMPRESSION` (and the per-signal forms) is read here,
7//! not left to `opentelemetry-otlp`, for the reason `config.rs` gives for the
8//! protocol: `opentelemetry-otlp` *warns and ignores* a value it was not built
9//! to honour, and `Telemetry` has no subscriber yet when it builds the
10//! exporters, so the warning went nowhere and the export went out
11//! uncompressed. Measured 2026-10-26 against `hello-agent`: with
12//! `OTEL_EXPORTER_OTLP_COMPRESSION=gzip` the collector received no
13//! `Content-Encoding` and the same 6.6 KB body. A value this crate cannot act
14//! on is noted, by name, in [`Telemetry::notices`](super::Telemetry::notices),
15//! and the export is uncompressed: the specification has an SDK warn and ignore
16//! it, and it was an error until 2026-11-01 (audit C-08).
17
18use super::config::{Env, Notes, Signal};
19
20/// How the body of an export request is compressed. Without one, it is not.
21///
22/// Only gzip is offered: every OTLP collector and the specification's
23/// SDK-support list accept it, and the encoder is pure Rust. zstd needs a C
24/// library, which this crate's dependency policy does not take on.
25#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26#[non_exhaustive]
27pub enum OtlpCompression {
28    /// gzip, as `Content-Encoding: gzip` over OTLP/HTTP and `grpc-encoding:
29    /// gzip` over OTLP/gRPC.
30    Gzip,
31}
32
33impl OtlpCompression {
34    pub(super) const fn to_upstream(self) -> opentelemetry_otlp::Compression {
35        match self {
36            Self::Gzip => opentelemetry_otlp::Compression::Gzip,
37        }
38    }
39}
40
41/// The compression the environment names for `signal`: its own
42/// `OTEL_EXPORTER_OTLP_{SIGNAL}_COMPRESSION`, else
43/// `OTEL_EXPORTER_OTLP_COMPRESSION`. `none` and unset are both `None`.
44///
45/// Any other value is noted, naming the variable, and the export is not
46/// compressed. `zstd` has its own reason, since the specification lists it and
47/// a reader may expect it to work.
48pub(super) fn from_env(env: &Env, signal: Signal, notes: &Notes) -> Option<OtlpCompression> {
49    let (var, value) = signal.otlp_var(env, "COMPRESSION")?;
50    match value.trim().to_ascii_lowercase().as_str() {
51        "gzip" => Some(OtlpCompression::Gzip),
52        "none" => None,
53        "zstd" => {
54            notes.ignored(
55                &var,
56                &value,
57                "is not supported (zstd needs a C library); `gzip` and `none` are",
58                "the export is not compressed",
59            );
60            None
61        }
62        _ => {
63            notes.ignored(
64                &var,
65                &value,
66                "is not `gzip` or `none`",
67                "the export is not compressed",
68            );
69            None
70        }
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77    use std::collections::HashMap;
78
79    fn env(pairs: &[(&str, &str)]) -> Box<Env> {
80        let map: HashMap<String, String> = pairs
81            .iter()
82            .map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
83            .collect();
84        Box::new(move |k| map.get(k).cloned().filter(|v| !v.trim().is_empty()))
85    }
86
87    fn read(e: &Env, signal: Signal) -> (Option<OtlpCompression>, Vec<String>) {
88        let notes = Notes::default();
89        let got = from_env(e, signal, &notes);
90        (got, notes.take())
91    }
92
93    #[test]
94    fn unset_and_none_are_no_compression_and_say_nothing() {
95        assert_eq!(read(&*env(&[]), Signal::Traces), (None, vec![]));
96        let e = env(&[("OTEL_EXPORTER_OTLP_COMPRESSION", "none")]);
97        assert_eq!(read(&*e, Signal::Traces), (None, vec![]));
98    }
99
100    #[test]
101    fn gzip_is_read_in_any_case_and_with_spaces() {
102        for v in ["gzip", "GZIP", " Gzip "] {
103            let e = env(&[("OTEL_EXPORTER_OTLP_COMPRESSION", v)]);
104            assert_eq!(
105                read(&*e, Signal::Logs),
106                (Some(OtlpCompression::Gzip), vec![]),
107                "{v:?}"
108            );
109        }
110    }
111
112    #[test]
113    fn the_signals_own_variable_wins_over_the_general_one_both_ways() {
114        let e = env(&[
115            ("OTEL_EXPORTER_OTLP_COMPRESSION", "gzip"),
116            ("OTEL_EXPORTER_OTLP_METRICS_COMPRESSION", "none"),
117        ]);
118        assert_eq!(read(&*e, Signal::Metrics).0, None);
119        assert_eq!(read(&*e, Signal::Traces).0, Some(OtlpCompression::Gzip));
120        let e = env(&[("OTEL_EXPORTER_OTLP_LOGS_COMPRESSION", "gzip")]);
121        assert_eq!(read(&*e, Signal::Logs).0, Some(OtlpCompression::Gzip));
122        assert_eq!(read(&*e, Signal::Traces).0, None);
123    }
124
125    #[test]
126    fn zstd_is_ignored_naming_the_variable_and_the_alternative() {
127        let e = env(&[("OTEL_EXPORTER_OTLP_TRACES_COMPRESSION", "zstd")]);
128        let (got, notes) = read(&*e, Signal::Traces);
129        assert_eq!(got, None);
130        let msg = &notes[0];
131        assert!(
132            msg.contains("OTEL_EXPORTER_OTLP_TRACES_COMPRESSION")
133                && msg.contains("zstd")
134                && msg.contains("gzip"),
135            "{msg}"
136        );
137        // The fallback message for an unknown value carries the variable, the
138        // value and "gzip" too; only the reason tells the two arms apart.
139        assert!(msg.contains("is not supported"), "{msg}");
140        assert!(msg.contains("the export is not compressed"), "{msg}");
141    }
142
143    #[test]
144    fn an_unknown_value_is_ignored_naming_the_variable() {
145        let e = env(&[("OTEL_EXPORTER_OTLP_COMPRESSION", "brotli")]);
146        let (got, notes) = read(&*e, Signal::Logs);
147        assert_eq!(got, None);
148        let msg = &notes[0];
149        assert!(
150            msg.contains("OTEL_EXPORTER_OTLP_COMPRESSION") && msg.contains("brotli"),
151            "{msg}"
152        );
153        assert!(msg.contains("is not `gzip` or `none`"), "{msg}");
154    }
155
156    #[test]
157    fn an_empty_value_is_unset() {
158        let e = env(&[("OTEL_EXPORTER_OTLP_COMPRESSION", "  ")]);
159        assert_eq!(read(&*e, Signal::Traces), (None, vec![]));
160    }
161}