Skip to main content

agent_data_plane_config/
lib.rs

1//! ADP-native configuration model: the typed target of configuration translation.
2//!
3//! This crate owns the domain-shaped model types that translation produces and that ADP runtime
4//! code consumes: `SalukiConfiguration { control, shared, domains }`, `ControlConfiguration`,
5//! `SharedConfiguration`, `DomainConfiguration` and its per-domain structs.
6//!
7//! It does not embed component config structs (those stay in `saluki-components`, built from this
8//! model). It depends on neither the raw configuration map nor the Datadog source model, so a
9//! consumer can depend on it without inheriting either.
10//!
11//! Every field is plain, source-agnostic data. There are no source key names in identifiers and no
12//! source serde (these structs are serialized for the `/config/runtime` view but never
13//! deserialized from a source language; that is the source adapter's job).
14//!
15//! A field whose meaning depends on whether its value was set explicitly, rather than on the value
16//! alone, is a [`ConfigValue<T>`] instead of a plain `T`.
17
18use std::fmt;
19use std::time::Duration;
20
21use serde::Serialize;
22
23pub mod control;
24pub mod defaults;
25pub mod domains;
26pub mod provenance;
27pub mod shared;
28
29pub use control::{ControlConfiguration, Logging};
30pub use domains::DomainConfiguration;
31pub use provenance::{ConfigValue, Provenance};
32pub use shared::SharedConfiguration;
33
34/// The complete ADP-native runtime configuration after translation.
35///
36/// Two writers fill it: the Datadog witness `drive` (schema fields) and `seed` (Saluki-only
37/// fields). They write disjoint fields. It is read by the orchestration layer (`control`) and by
38/// components at topology assembly (`shared` and `domains`).
39#[derive(Clone, Debug, Default, PartialEq, Serialize)]
40pub struct SalukiConfiguration {
41    /// Read first: decides which pipelines/topology to build. Orchestration layer only.
42    pub control: ControlConfiguration,
43    /// Cross-cutting values consumed by more than one domain, each with a single home.
44    pub shared: SharedConfiguration,
45    /// Per-domain resolved config, grouped by ownership domain.
46    pub domains: DomainConfiguration,
47}
48
49impl SalukiConfiguration {
50    /// Returns the topology shutdown timeout.
51    ///
52    /// Uses `control.stop_timeout` when it is set. Otherwise, it sums `control.aggregator_stop_timeout` and
53    /// `shared.endpoints.forwarder.stop_timeout`, returning `Duration::MAX` if the sum overflows.
54    pub fn stop_timeout(&self) -> Duration {
55        match self.control.stop_timeout {
56            Some(timeout) => timeout,
57            None => self
58                .control
59                .aggregator_stop_timeout
60                .saturating_add(self.shared.endpoints.forwarder.stop_timeout),
61        }
62    }
63}
64
65/// An error produced while translating a `DatadogConfiguration` or `SalukiOnly` value into a
66/// `SalukiConfiguration` value.
67#[derive(Debug)]
68pub struct Error {
69    context: String,
70    error: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
71}
72
73/// A boxed source error that can cross thread boundaries.
74pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
75
76impl Error {
77    /// Create an error that wraps an underlying error.
78    ///
79    /// Displays `{context}: {error}`.
80    pub fn new<S, E>(context: S, error: E) -> Self
81    where
82        S: Into<String>,
83        E: Into<BoxError>,
84    {
85        Self {
86            context: context.into(),
87            error: Some(error.into()),
88        }
89    }
90
91    /// Create an error without an underlying source error.
92    pub fn new_without_source<S>(context: S) -> Self
93    where
94        S: Into<String>,
95    {
96        Self {
97            context: context.into(),
98            error: None,
99        }
100    }
101}
102
103impl fmt::Display for Error {
104    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105        write!(f, "{}", self.context)?;
106        if let Some(error) = &self.error {
107            write!(f, ": {error}")?;
108        }
109        Ok(())
110    }
111}
112
113impl std::error::Error for Error {
114    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
115        self.error.as_deref().map(|s| s as &(dyn std::error::Error + 'static))
116    }
117}
118
119#[cfg(test)]
120mod tests {
121    use super::*;
122
123    #[test]
124    fn stop_timeout_uses_the_configured_value() {
125        let mut config = SalukiConfiguration::default();
126        config.control.stop_timeout = Some(Duration::from_secs(11));
127        config.control.aggregator_stop_timeout = Duration::from_secs(3);
128        config.shared.endpoints.forwarder.stop_timeout = Duration::from_secs(7);
129
130        assert_eq!(config.stop_timeout(), Duration::from_secs(11));
131    }
132
133    #[test]
134    fn stop_timeout_sums_component_timeouts() {
135        let mut config = SalukiConfiguration::default();
136        config.control.aggregator_stop_timeout = Duration::from_secs(3);
137        config.shared.endpoints.forwarder.stop_timeout = Duration::from_secs(7);
138
139        assert_eq!(config.stop_timeout(), Duration::from_secs(10));
140    }
141
142    #[test]
143    fn stop_timeout_saturates_when_sum_overflows() {
144        let mut config = SalukiConfiguration::default();
145        config.control.aggregator_stop_timeout = Duration::MAX;
146        config.shared.endpoints.forwarder.stop_timeout = Duration::from_secs(1);
147
148        assert_eq!(config.stop_timeout(), Duration::MAX);
149    }
150}