a2a_protocol_server/otel/telemetry/
compression.rs1use super::config::{Env, Notes, Signal};
19
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26#[non_exhaustive]
27pub enum OtlpCompression {
28 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
41pub(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, ¬es);
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 = ¬es[0];
131 assert!(
132 msg.contains("OTEL_EXPORTER_OTLP_TRACES_COMPRESSION")
133 && msg.contains("zstd")
134 && msg.contains("gzip"),
135 "{msg}"
136 );
137 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 = ¬es[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}