|
| 1 | +//! Translation layer from Datadog config to ADP-native config. |
| 2 | +//! |
| 3 | +//! This module owns the [`Translator`] -- the [`DatadogConfigConsumer`] implementation that writes |
| 4 | +//! witnessed Datadog values directly into the ADP-native structs embedded in a |
| 5 | +//! [`SalukiConfiguration`]. |
| 6 | +//! |
| 7 | +//! 1. The Saluki-schema-only seed (`saluki_only.seed()`) produces the base `SalukiConfiguration` |
| 8 | +//! with defaults plus Saluki-only fields that cannot arrive from the Datadog agent. |
| 9 | +//! 2. The Datadog witness `drive` overlays its config fields via the `consume_<key>` methods. |
| 10 | +//! |
| 11 | +//! [`DatadogConfigConsumer`]: datadog_agent_config::DatadogConfigConsumer |
| 12 | +//! [`SalukiConfiguration`]: agent_data_plane_config::SalukiConfiguration |
| 13 | +
|
| 14 | +mod datadog; |
| 15 | + |
| 16 | +use agent_data_plane_config::{SalukiConfiguration, SalukiOnlyConfiguration}; |
| 17 | +use datadog_agent_config::{drive, DatadogConfiguration, TranslateError}; |
| 18 | + |
| 19 | +/// The witness consumer that accumulates a [`SalukiConfiguration`] from a Datadog source parse. |
| 20 | +/// |
| 21 | +/// The accumulator composes the output type [`SalukiConfiguration`], along with accumulated |
| 22 | +/// translation errors. |
| 23 | +#[allow(dead_code)] |
| 24 | +pub(crate) struct Translator { |
| 25 | + /// The in-progress native model. Seeded from the Saluki-only base, then overlaid by the drive. |
| 26 | + pub(crate) saluki: SalukiConfiguration, |
| 27 | + |
| 28 | + /// Accumulated semantic translation errors. The first is surfaced by `drive`. |
| 29 | + errors: Vec<TranslateError>, |
| 30 | +} |
| 31 | + |
| 32 | +impl Translator { |
| 33 | + /// Creates a translator over a seeded base. |
| 34 | + /// |
| 35 | + /// `base` is the lowest-precedence starting point (typically `saluki_only.seed()`). The Datadog |
| 36 | + /// drive overlays its schema fields on top, so the base only usefully carries disjoint |
| 37 | + /// Saluki-schema-only fields. |
| 38 | + #[allow(dead_code)] |
| 39 | + pub(crate) fn new(base: SalukiConfiguration) -> Self { |
| 40 | + Self { |
| 41 | + saluki: base, |
| 42 | + errors: Vec::new(), |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// Returns the finished native model. |
| 47 | + #[allow(dead_code)] |
| 48 | + pub(crate) fn finish(self) -> SalukiConfiguration { |
| 49 | + self.saluki |
| 50 | + } |
| 51 | + |
| 52 | + /// Records a semantic translation error. The first recorded error is surfaced by `drive`. |
| 53 | + #[allow(dead_code)] |
| 54 | + pub(crate) fn record_error(&mut self, e: TranslateError) { |
| 55 | + self.errors.push(e); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// Translates a Datadog source parse plus the Saluki-only seed into the ADP-native model. |
| 60 | +/// |
| 61 | +/// This is the single reusable translation path used by both startup and each dynamic update: |
| 62 | +/// seed a [`Translator`] from `saluki_only`, drive the Datadog witness over it, and finish. |
| 63 | +/// |
| 64 | +/// # Errors |
| 65 | +/// |
| 66 | +/// Returns the first [`TranslateError`] recorded while consuming a witnessed value (for example, a |
| 67 | +/// value that cannot be parsed into its native destination). Callers decide policy: startup bails; |
| 68 | +/// a dynamic update is rejected and the last-good config retained. |
| 69 | +#[allow(dead_code)] |
| 70 | +pub(crate) fn translate( |
| 71 | + saluki_only: &SalukiOnlyConfiguration, datadog: &DatadogConfiguration, |
| 72 | +) -> Result<SalukiConfiguration, TranslateError> { |
| 73 | + let mut t = Translator::new(saluki_only.seed()); |
| 74 | + drive(datadog, &mut t)?; |
| 75 | + Ok(t.finish()) |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(test)] |
| 79 | +mod tests { |
| 80 | + use saluki_component_config::dogstatsd::SourceConfig; |
| 81 | + |
| 82 | + use super::*; |
| 83 | + |
| 84 | + // Datadog Agent defaults clobber Saluki native defaults by design. Translation is |
| 85 | + // clobber-not-merge: a Datadog-schema field's default overwrites the native default even when |
| 86 | + // the two differ. |
| 87 | + // TODO: we need a mechanism for default alignment #1802 |
| 88 | + #[test] |
| 89 | + fn seed_then_drive_default_succeeds_and_clobbers() { |
| 90 | + let saluki_only = SalukiOnlyConfiguration::default(); |
| 91 | + let datadog = DatadogConfiguration::default(); |
| 92 | + let config = translate(&saluki_only, &datadog).expect("default translation succeeds"); |
| 93 | + // The drive ran: the Datadog default port (8125) flows into the native source. |
| 94 | + assert_eq!(config.components.dogstatsd.source.port, 8125); |
| 95 | + // Clobber, not merge: the Datadog default for `dogstatsd_capture_depth` is 0, and it |
| 96 | + // overwrites the native default of 1024 (the component later raises sub-1024 values). |
| 97 | + assert_eq!(config.components.dogstatsd.source.capture_depth, 0); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn seed_and_drive_write_disjoint_dogstatsd_fields() { |
| 102 | + // Saluki-only seeds `allow_context_heap_allocs` (a Saluki-schema-only field); the Datadog |
| 103 | + // drive overlays `dogstatsd_port` (a Datadog-schema field). Both survive into one struct. |
| 104 | + let mut saluki_only = SalukiOnlyConfiguration::default(); |
| 105 | + saluki_only.dogstatsd.allow_context_heap_allocs = Some(false); |
| 106 | + |
| 107 | + let datadog = DatadogConfiguration { |
| 108 | + dogstatsd_port: 7000, |
| 109 | + ..Default::default() |
| 110 | + }; |
| 111 | + |
| 112 | + let config = translate(&saluki_only, &datadog).expect("translation succeeds"); |
| 113 | + assert!( |
| 114 | + !config.components.dogstatsd.source.allow_context_heap_allocations, |
| 115 | + "seeded Saluki-only field survives" |
| 116 | + ); |
| 117 | + assert_eq!( |
| 118 | + config.components.dogstatsd.source.port, 7000, |
| 119 | + "Datadog drive overlays its field" |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn drive_overlays_dogstatsd_source() { |
| 125 | + let saluki_only = SalukiOnlyConfiguration::default(); |
| 126 | + let datadog = DatadogConfiguration { |
| 127 | + dogstatsd_buffer_size: 4096, |
| 128 | + dogstatsd_non_local_traffic: true, |
| 129 | + dogstatsd_tag_cardinality: "high".to_string(), |
| 130 | + ..Default::default() |
| 131 | + }; |
| 132 | + |
| 133 | + let config = translate(&saluki_only, &datadog).expect("translation succeeds"); |
| 134 | + let source: &SourceConfig = &config.components.dogstatsd.source; |
| 135 | + assert_eq!(source.buffer_size, 4096); |
| 136 | + assert!(source.non_local_traffic); |
| 137 | + assert_eq!( |
| 138 | + source.origin_enrichment.tag_cardinality, |
| 139 | + saluki_context::origin::OriginTagCardinality::High |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn malformed_value_surfaces_as_error() { |
| 145 | + let saluki_only = SalukiOnlyConfiguration::default(); |
| 146 | + let datadog = DatadogConfiguration { |
| 147 | + dogstatsd_tag_cardinality: "bogus".to_string(), |
| 148 | + ..Default::default() |
| 149 | + }; |
| 150 | + assert!(translate(&saluki_only, &datadog).is_err()); |
| 151 | + } |
| 152 | +} |
0 commit comments