agent_data_plane_config/domains/apm.rs
1//! APM domain: the experimental trace receiver proxy.
2//!
3//! With the proxy enabled, ADP takes over the trace-agent's receiver: it listens where the trace-agent normally listens
4//! and forwards requests for endpoints it does not handle itself to the trace-agent, which the Datadog Agent moves to
5//! [`proxy_destination`](Domain::proxy_destination). Trace *processing* (obfuscation, sampling, stats) is shared with
6//! the OTLP path and lives in the [`traces`](super::traces) domain.
7//!
8//! The listen settings are the trace-agent's own `apm_config.*` keys, so ADP binds exactly the address tracers are
9//! already configured to reach. The global `bind_host` also applies, and lives in the shared configuration.
10//!
11//! With [`native_trace_intake`](Domain::native_trace_intake) also enabled, the relay serves the trace intake endpoints
12//! itself instead of proxying them, applying the receiver limits held here as the trace-agent does.
13//!
14//! The domain also holds the trace-agent receiver settings that ADP doesn't apply itself but reports to tracers, such
15//! as the connection limit and the resource limits, as the trace-agent reads them.
16
17use std::num::NonZeroUsize;
18use std::time::Duration;
19
20use serde::Serialize;
21
22use crate::defaults::{DEFAULT_APM_NATIVE_TRACE_INTAKE, DEFAULT_APM_PROXY_DESTINATION};
23use crate::ConfigValue;
24
25/// The trace-agent's receiver request timeout, applied when `apm_config.receiver_timeout` is `0`.
26const DEFAULT_RECEIVER_TIMEOUT: Duration = Duration::from_secs(5);
27
28/// Resolved APM configuration.
29#[derive(Clone, Debug, PartialEq, Serialize)]
30pub struct Domain {
31 /// TCP port the trace receiver listens on (`apm_config.receiver_port`).
32 ///
33 /// `0` disables the TCP listener.
34 pub receiver_port: u16,
35
36 /// Unix domain socket path the trace receiver listens on (`apm_config.receiver_socket`).
37 ///
38 /// `None` disables the Unix domain socket listener. Defaults to `/var/run/datadog/apm.socket` on Linux and AIX,
39 /// matching the trace-agent, and to disabled elsewhere.
40 pub receiver_socket: Option<String>,
41
42 /// Whether the TCP listener accepts traffic from other hosts (`apm_config.apm_non_local_traffic`).
43 ///
44 /// When `true`, the listener binds every interface, overriding `bind_host`, as the trace-agent does.
45 ///
46 /// The trace-agent also binds every interface when it runs in a container and neither `bind_host` nor
47 /// `apm_config.apm_non_local_traffic` was set explicitly. That fallback isn't part of the configuration, so the
48 /// provenance is kept to resolve the relay's listen host.
49 pub non_local_traffic: ConfigValue<bool>,
50
51 /// Where the relocated trace-agent listens, as a URL (`data_plane.experimental.apm.proxy_destination`).
52 ///
53 /// Either `http://host:port` or `unix:///path/to/socket`. Every request is forwarded
54 /// here. Defaults to `http://127.0.0.1:8127`. (not in Datadog Agent config schema)
55 ///
56 /// Over a Unix domain socket, the trace-agent attributes a request that carries no container headers to the peer
57 /// of the connection, which is ADP. That covers requests from TCP clients that send no container headers, and from
58 /// Unix domain socket clients whose container ADP couldn't resolve. An `http://` destination leaves their origin
59 /// unknown instead.
60 pub proxy_destination: String,
61
62 /// Whether the relay serves the trace intake endpoints itself (`data_plane.experimental.apm.native_trace_intake`).
63 ///
64 /// When `false`, the default, the relay proxies them to the trace-agent. (not in Datadog Agent config schema)
65 pub native_trace_intake: bool,
66
67 /// Maximum number of connections the trace-agent's receiver accepts (`apm_config.connection_limit`).
68 ///
69 /// `0` when the key is unset: the trace-agent reads the key only when it is set, so it ignores the schema
70 /// default of 2000. A negative value is read as `0`, with a warning: the trace-agent applies no limit for either.
71 pub connection_limit: u32,
72
73 /// Timeout, in seconds, for a request to the trace-agent's receiver (`apm_config.receiver_timeout`).
74 ///
75 /// `0`, the default, leaves the trace-agent's own default of 5 seconds; see [`Domain::effective_receiver_timeout`].
76 /// A negative value is read as `0`, with a warning: the trace-agent uses its default for either.
77 pub receiver_timeout: u32,
78
79 /// Maximum size, in bytes, of a request body the trace-agent's receiver accepts (`apm_config.max_payload_size`).
80 ///
81 /// Defaults to 25 MiB. A negative value is a translation error.
82 pub max_payload_size: u64,
83
84 /// Number of trace payloads the receiver decodes concurrently (`apm_config.decoders`).
85 ///
86 /// `0`, the default, sizes the pool from the available parallelism; see [`Domain::effective_decoders`]. A negative
87 /// value is read as `0`, with a warning, where the trace-agent would panic.
88 pub decoders: u32,
89
90 /// Time, in milliseconds, a trace payload waits for a free decoder before the receiver rejects it
91 /// (`apm_config.decoder_timeout`).
92 ///
93 /// Defaults to 1000. A negative value is read as `0`, with a warning: the trace-agent rejects the payload
94 /// immediately for either.
95 pub decoder_timeout: u32,
96
97 /// Memory, in bytes, the trace-agent aims to stay under (`apm_config.max_memory`).
98 ///
99 /// The provenance is kept because the trace-agent disables the limit in a container when the key is unset; see
100 /// [`Domain::effective_max_memory`].
101 pub max_memory: ConfigValue<f64>,
102
103 /// CPU, in percent of a core, the trace-agent aims to stay under (`apm_config.max_cpu_percent`).
104 ///
105 /// The provenance is kept because the trace-agent disables the limit in a container when the key is unset; see
106 /// [`Domain::effective_max_cpu`].
107 pub max_cpu_percent: ConfigValue<f64>,
108
109 /// Whether the trace-agent proxies tracer telemetry on `/telemetry/proxy/` (`apm_config.telemetry.enabled`).
110 pub telemetry_enabled: bool,
111}
112
113impl Default for Domain {
114 fn default() -> Self {
115 Self {
116 // Saluki-only settings retain these defaults when unset.
117 proxy_destination: DEFAULT_APM_PROXY_DESTINATION.to_string(),
118 native_trace_intake: DEFAULT_APM_NATIVE_TRACE_INTAKE,
119 // Witnessed settings are overwritten during translation.
120 receiver_port: 0,
121 receiver_socket: None,
122 non_local_traffic: ConfigValue::defaulted(false),
123 connection_limit: 0,
124 receiver_timeout: 0,
125 max_payload_size: 0,
126 decoders: 0,
127 decoder_timeout: 0,
128 max_memory: ConfigValue::defaulted(0.0),
129 max_cpu_percent: ConfigValue::defaulted(0.0),
130 telemetry_enabled: false,
131 }
132 }
133}
134
135impl Domain {
136 /// Resolves the TCP listen host and whether to bind every interface.
137 ///
138 /// Explicit host or traffic settings apply as configured. Otherwise, containers bind every interface and
139 /// non-containerized processes use the default loopback host.
140 pub fn listen_host(&self, bind_host: &ConfigValue<Option<String>>, containerized: bool) -> (Option<String>, bool) {
141 if bind_host.is_explicit() || self.non_local_traffic.is_explicit() {
142 let host = if bind_host.is_explicit() {
143 bind_host.value.clone()
144 } else {
145 None
146 };
147 (host, self.non_local_traffic.value)
148 } else {
149 (None, containerized)
150 }
151 }
152
153 /// Resolves the receiver's request timeout, where `0` means the trace-agent's default of 5 seconds.
154 pub fn effective_receiver_timeout(&self) -> Duration {
155 match self.receiver_timeout {
156 0 => DEFAULT_RECEIVER_TIMEOUT,
157 secs => Duration::from_secs(u64::from(secs)),
158 }
159 }
160
161 /// Resolves the number of decoders, given the available parallelism.
162 ///
163 /// As in the trace-agent, `0` means half the available parallelism (its `GOMAXPROCS`), and at least one.
164 pub fn effective_decoders(&self, parallelism: NonZeroUsize) -> NonZeroUsize {
165 NonZeroUsize::new(self.decoders as usize)
166 .or_else(|| NonZeroUsize::new(parallelism.get() / 2))
167 .unwrap_or(NonZeroUsize::MIN)
168 }
169
170 /// Resolves the time a payload waits for a free decoder before the receiver rejects it.
171 pub fn effective_decoder_timeout(&self) -> Duration {
172 Duration::from_millis(u64::from(self.decoder_timeout))
173 }
174
175 /// Resolves the memory, in bytes, the trace-agent aims to stay under, where `0` disables the limit.
176 ///
177 /// As in the trace-agent, a container leaves resource limits to its runtime: the limit is disabled there unless
178 /// `apm_config.max_memory` was set explicitly.
179 pub fn effective_max_memory(&self, containerized: bool) -> f64 {
180 if containerized && !self.max_memory.is_explicit() {
181 0.0
182 } else {
183 self.max_memory.value
184 }
185 }
186
187 /// Resolves the CPU the trace-agent aims to stay under, as a fraction of a core, where `0` disables the limit.
188 ///
189 /// As in the trace-agent, a container leaves resource limits to its runtime: the limit is disabled there unless
190 /// `apm_config.max_cpu_percent` was set explicitly.
191 pub fn effective_max_cpu(&self, containerized: bool) -> f64 {
192 if containerized && !self.max_cpu_percent.is_explicit() {
193 0.0
194 } else {
195 self.max_cpu_percent.value / 100.0
196 }
197 }
198}
199
200#[cfg(test)]
201mod tests {
202 use super::*;
203
204 #[test]
205 fn listen_host_follows_explicit_settings() {
206 let host = Some("10.0.0.1".to_string());
207
208 // An explicit `bind_host` applies, in a container or not.
209 for containerized in [false, true] {
210 assert_eq!(
211 Domain {
212 non_local_traffic: ConfigValue::defaulted(false),
213 ..Default::default()
214 }
215 .listen_host(&ConfigValue::explicit(host.clone()), containerized),
216 (host.clone(), false)
217 );
218 }
219
220 // `apm_non_local_traffic` overrides it, even at its default value once `bind_host` is explicit, as in
221 // Kubernetes where it defaults to `true`.
222 assert_eq!(
223 Domain {
224 non_local_traffic: ConfigValue::defaulted(true),
225 ..Default::default()
226 }
227 .listen_host(&ConfigValue::explicit(host.clone()), false),
228 (host.clone(), true)
229 );
230
231 // An explicit `apm_non_local_traffic: false` keeps a container on the default host.
232 assert_eq!(
233 Domain {
234 non_local_traffic: ConfigValue::explicit(false),
235 ..Default::default()
236 }
237 .listen_host(&ConfigValue::defaulted(Some("localhost".to_string())), true),
238 (None, false)
239 );
240 }
241
242 #[test]
243 fn resource_limits_are_disabled_in_a_container_unless_set() {
244 let defaulted = Domain {
245 max_memory: ConfigValue::defaulted(5e8),
246 max_cpu_percent: ConfigValue::defaulted(50.0),
247 ..Default::default()
248 };
249 assert_eq!(defaulted.effective_max_memory(false), 5e8);
250 assert_eq!(defaulted.effective_max_cpu(false), 0.5);
251 assert_eq!(defaulted.effective_max_memory(true), 0.0);
252 assert_eq!(defaulted.effective_max_cpu(true), 0.0);
253
254 let explicit = Domain {
255 max_memory: ConfigValue::explicit(1e9),
256 max_cpu_percent: ConfigValue::explicit(200.0),
257 ..Default::default()
258 };
259 for containerized in [false, true] {
260 assert_eq!(explicit.effective_max_memory(containerized), 1e9);
261 assert_eq!(explicit.effective_max_cpu(containerized), 2.0);
262 }
263 }
264
265 #[test]
266 fn receiver_timeout_zero_means_five_seconds() {
267 let mut apm = Domain::default();
268 assert_eq!(apm.effective_receiver_timeout(), DEFAULT_RECEIVER_TIMEOUT);
269 apm.receiver_timeout = 7;
270 assert_eq!(apm.effective_receiver_timeout(), Duration::from_secs(7));
271 }
272
273 #[test]
274 fn decoders_zero_means_half_the_parallelism_and_at_least_one() {
275 let n = |v: usize| NonZeroUsize::new(v).unwrap();
276 let mut apm = Domain::default();
277 assert_eq!(apm.effective_decoders(n(8)), n(4));
278 assert_eq!(apm.effective_decoders(n(3)), n(1));
279 assert_eq!(apm.effective_decoders(n(1)), n(1));
280
281 apm.decoders = 6;
282 assert_eq!(apm.effective_decoders(n(1)), n(6));
283 assert_eq!(apm.effective_decoders(n(64)), n(6));
284 }
285
286 #[test]
287 fn decoder_timeout_is_in_milliseconds() {
288 let apm = Domain {
289 decoder_timeout: 1000,
290 ..Default::default()
291 };
292 assert_eq!(apm.effective_decoder_timeout(), Duration::from_secs(1));
293 }
294
295 #[test]
296 fn listen_host_binds_every_interface_in_a_container_when_unset() {
297 let defaulted_host = ConfigValue::defaulted(Some("localhost".to_string()));
298
299 assert_eq!(Domain::default().listen_host(&defaulted_host, true), (None, true));
300 assert_eq!(Domain::default().listen_host(&defaulted_host, false), (None, false));
301 }
302}