Skip to main content

vector/config/
validation.rs

1use std::{collections::HashMap, path::PathBuf};
2
3use futures_util::{FutureExt, StreamExt, TryFutureExt, TryStreamExt, stream};
4use heim::{disk::Partition, units::information::byte};
5use indexmap::IndexMap;
6use vector_lib::{buffers::config::DiskUsage, internal_event::DEFAULT_OUTPUT};
7
8use super::{
9    ComponentKey, Config, OutputId, Resource, TransformContext, builder::ConfigBuilder,
10    transform::get_transform_output_ids,
11};
12
13/// Minimum value (exclusive) for EWMA alpha options.
14/// The alpha value must be strictly greater than this value.
15const EWMA_ALPHA_MIN: f64 = 0.0;
16
17/// Maximum value (exclusive) for EWMA alpha options.
18/// The alpha value must be strictly less than this value.
19const EWMA_ALPHA_MAX: f64 = 1.0;
20
21/// Minimum value (exclusive) for EWMA half-life options.
22/// The half-life value must be strictly greater than this value.
23const EWMA_HALF_LIFE_SECONDS_MIN: f64 = 0.0;
24
25/// Validates an optional EWMA alpha value and returns an error message if invalid.
26/// Returns `None` if the value is `None` or valid, otherwise returns an error message.
27fn validate_ewma_alpha(alpha: Option<f64>, field_name: &str) -> Option<String> {
28    if let Some(alpha) = alpha
29        && !(alpha > EWMA_ALPHA_MIN && alpha < EWMA_ALPHA_MAX)
30    {
31        Some(format!(
32            "Global `{field_name}` must be between 0 and 1 exclusive (0 < alpha < 1), got {alpha}"
33        ))
34    } else {
35        None
36    }
37}
38
39/// Validates an optional EWMA half-life value and returns an error message if invalid.
40/// Returns `None` if the value is `None` or valid, otherwise returns an error message.
41#[expect(
42    clippy::neg_cmp_op_on_partial_ord,
43    reason = "!(x > 0) rejects NaN and non-positive values; (x <= 0) would incorrectly accept NaN"
44)]
45fn validate_ewma_half_life_seconds(
46    half_life_seconds: Option<f64>,
47    field_name: &str,
48) -> Option<String> {
49    if let Some(half_life_seconds) = half_life_seconds
50        && !(half_life_seconds > EWMA_HALF_LIFE_SECONDS_MIN)
51    {
52        Some(format!(
53            "Global `{field_name}` must be greater than 0, got {half_life_seconds}"
54        ))
55    } else {
56        None
57    }
58}
59
60/// Check that provide + topology config aren't present in the same builder, which is an error.
61pub fn check_provider(config: &ConfigBuilder) -> Result<(), Vec<String>> {
62    if config.provider.is_some()
63        && (!config.sources.is_empty() || !config.transforms.is_empty() || !config.sinks.is_empty())
64    {
65        Err(vec![
66            "No sources/transforms/sinks are allowed if provider config is present.".to_owned(),
67        ])
68    } else {
69        Ok(())
70    }
71}
72
73pub fn check_names<'a, I: Iterator<Item = &'a ComponentKey>>(names: I) -> Result<(), Vec<String>> {
74    let errors: Vec<_> = names
75        .filter(|component_key| component_key.id().contains('.'))
76        .map(|component_key| {
77            format!(
78                "Component name \"{}\" should not contain a \".\"",
79                component_key.id()
80            )
81        })
82        .collect();
83
84    if errors.is_empty() {
85        Ok(())
86    } else {
87        Err(errors)
88    }
89}
90
91pub fn check_shape(config: &ConfigBuilder) -> Result<(), Vec<String>> {
92    let mut errors = vec![];
93
94    if !config.allow_empty {
95        if config.sources.is_empty() {
96            errors.push("No sources defined in the config.".to_owned());
97        }
98
99        if config.sinks.is_empty() {
100            errors.push("No sinks defined in the config.".to_owned());
101        }
102    }
103
104    // Helper for below
105    fn tagged<'a>(
106        tag: &'static str,
107        iter: impl Iterator<Item = &'a ComponentKey>,
108    ) -> impl Iterator<Item = (&'static str, &'a ComponentKey)> {
109        iter.map(move |x| (tag, x))
110    }
111
112    // Check for non-unique names across sources, sinks, and transforms
113    let mut used_keys = HashMap::<&ComponentKey, Vec<&'static str>>::new();
114    for (ctype, id) in tagged("source", config.sources.keys())
115        .chain(tagged("transform", config.transforms.keys()))
116        .chain(tagged("sink", config.sinks.keys()))
117    {
118        let uses = used_keys.entry(id).or_default();
119        uses.push(ctype);
120    }
121
122    for (id, uses) in used_keys.into_iter().filter(|(_id, uses)| uses.len() > 1) {
123        errors.push(format!(
124            "More than one component with name \"{}\" ({}).",
125            id,
126            uses.join(", ")
127        ));
128    }
129
130    // Warnings and errors
131    let sink_inputs = config
132        .sinks
133        .iter()
134        .map(|(key, sink)| ("sink", key.clone(), sink.inputs.clone()));
135    let transform_inputs = config
136        .transforms
137        .iter()
138        .map(|(key, transform)| ("transform", key.clone(), transform.inputs.clone()));
139    for (output_type, key, inputs) in sink_inputs.chain(transform_inputs) {
140        if inputs.is_empty() {
141            errors.push(format!(
142                "{} \"{}\" has no inputs",
143                capitalize(output_type),
144                key
145            ));
146        }
147
148        let mut frequencies = HashMap::new();
149        for input in inputs {
150            let entry = frequencies.entry(input).or_insert(0usize);
151            *entry += 1;
152        }
153
154        for (dup, count) in frequencies.into_iter().filter(|(_name, count)| *count > 1) {
155            errors.push(format!(
156                "{} \"{}\" has input \"{}\" duplicated {} times",
157                capitalize(output_type),
158                key,
159                dup,
160                count,
161            ));
162        }
163    }
164
165    if errors.is_empty() {
166        Ok(())
167    } else {
168        Err(errors)
169    }
170}
171
172pub fn check_resources(config: &ConfigBuilder) -> Result<(), Vec<String>> {
173    let source_resources = config
174        .sources
175        .iter()
176        .map(|(id, config)| (id, config.inner.resources()));
177    let sink_resources = config
178        .sinks
179        .iter()
180        .map(|(id, config)| (id, config.resources(id)));
181
182    let conflicting_components = Resource::conflicts(source_resources.chain(sink_resources));
183
184    if conflicting_components.is_empty() {
185        Ok(())
186    } else {
187        Err(conflicting_components
188            .into_iter()
189            .map(|(resource, components)| {
190                format!("Resource `{resource}` is claimed by multiple components: {components:?}")
191            })
192            .collect())
193    }
194}
195
196/// Validates that `*_ewma_alpha` values are within the valid range (0 < alpha < 1).
197pub fn check_values(config: &ConfigBuilder) -> Result<(), Vec<String>> {
198    let mut errors = Vec::new();
199
200    if let Some(error) = validate_ewma_half_life_seconds(
201        config.global.buffer_utilization_ewma_half_life_seconds,
202        "buffer_utilization_ewma_half_life_seconds",
203    ) {
204        errors.push(error);
205    }
206    if let Some(error) = validate_ewma_alpha(config.global.latency_ewma_alpha, "latency_ewma_alpha")
207    {
208        errors.push(error);
209    }
210
211    if errors.is_empty() {
212        Ok(())
213    } else {
214        Err(errors)
215    }
216}
217
218/// To avoid collisions between `output` metric tags, check that a component
219/// does not have a named output with the name [`DEFAULT_OUTPUT`]
220pub fn check_outputs(config: &ConfigBuilder) -> Result<(), Vec<String>> {
221    let mut errors = Vec::new();
222    for (key, source) in config.sources.iter() {
223        let outputs = source.inner.outputs(config.schema.log_namespace());
224        if outputs
225            .iter()
226            .map(|output| output.port.as_deref().unwrap_or(""))
227            .any(|name| name == DEFAULT_OUTPUT)
228        {
229            errors.push(format!(
230                "Source {key} cannot have a named output with reserved name: `{DEFAULT_OUTPUT}`"
231            ));
232        }
233    }
234
235    for (key, transform) in config.transforms.iter() {
236        // Structural validation: reserved names, duplicate routes, invalid sample rates.
237        // Uses a default context so transforms that require environment resources (VRL
238        // compilation, condition building) must guard on context.key being None and skip
239        // those checks — they run later in validate_transforms() with a real context.
240        if let Err(errs) = transform.inner.validate(&TransformContext::default()) {
241            errors.extend(errs.into_iter().map(|msg| format!("Transform {key} {msg}")));
242        }
243
244        if get_transform_output_ids(
245            transform.inner.as_ref(),
246            key.clone(),
247            config.schema.log_namespace(),
248        )
249        .any(|output| matches!(output.port, Some(output) if output == DEFAULT_OUTPUT))
250        {
251            errors.push(format!(
252                "Transform {key} cannot have a named output with reserved name: `{DEFAULT_OUTPUT}`"
253            ));
254        }
255    }
256
257    if errors.is_empty() {
258        Ok(())
259    } else {
260        Err(errors)
261    }
262}
263
264pub async fn check_buffer_preconditions(config: &Config) -> Result<(), Vec<String>> {
265    // We need to assert that Vector's data directory is located on a mountpoint that has enough
266    // capacity to allow all sinks with disk buffers configured to be able to use up to their
267    // maximum configured size without overrunning the total capacity.
268    //
269    // More subtly, we need to make sure we properly map a given buffer's data directory to the
270    // appropriate mountpoint, as it is technically possible that individual buffers could be on
271    // separate mountpoints.
272    //
273    // Notably, this does *not* cover other data usage by Vector on the same mountpoint because we
274    // don't always know the upper bound of that usage i.e. file checkpoint state.
275
276    // Grab all configured disk buffers, and if none are present, simply return early.
277    let global_data_dir = config.global.data_dir.clone();
278    let configured_disk_buffers = config
279        .sinks()
280        .flat_map(|(id, sink)| {
281            sink.buffer
282                .stages()
283                .iter()
284                .filter_map(|stage| stage.disk_usage(global_data_dir.clone(), id))
285        })
286        .collect::<Vec<_>>();
287
288    if configured_disk_buffers.is_empty() {
289        return Ok(());
290    }
291
292    // Now query all the mountpoints on the system, and get their total capacity. We also have to
293    // sort the mountpoints from longest to shortest so we can find the longest prefix match for
294    // each buffer data directory by simply iterating from beginning to end.
295    let mountpoints = heim::disk::partitions()
296        .and_then(|stream| stream.try_collect::<Vec<_>>().and_then(process_partitions))
297        .or_else(|_| {
298            heim::disk::partitions_physical()
299                .and_then(|stream| stream.try_collect::<Vec<_>>().and_then(process_partitions))
300        })
301        .await;
302
303    let mountpoints = match mountpoints {
304        Ok(mut mountpoints) => {
305            mountpoints.sort_by(|m1, _, m2, _| m2.cmp(m1));
306            mountpoints
307        }
308        Err(e) => {
309            warn!(
310                cause = %e,
311                message = "Failed to query disk partitions. Cannot ensure that buffer size limits are within physical storage capacity limits.",
312            );
313            return Ok(());
314        }
315    };
316
317    // Now build a mapping of buffer IDs/usage configuration to the mountpoint they reside on.
318    let mountpoint_buffer_mapping = configured_disk_buffers.into_iter().fold(
319        HashMap::new(),
320        |mut mappings: HashMap<PathBuf, Vec<DiskUsage>>, usage| {
321            let canonicalized_data_dir = usage
322                .data_dir()
323                .canonicalize()
324                .unwrap_or_else(|_| usage.data_dir().to_path_buf());
325            let mountpoint = mountpoints
326                .keys()
327                .find(|mountpoint| canonicalized_data_dir.starts_with(mountpoint));
328
329            match mountpoint {
330                None => warn!(
331                    buffer_id = usage.id().id(),
332                    data_dir = usage.data_dir().to_string_lossy().as_ref(),
333                    canonicalized_data_dir = canonicalized_data_dir.to_string_lossy().as_ref(),
334                    message = "Found no matching mountpoint for buffer data directory.",
335                ),
336                Some(mountpoint) => {
337                    mappings.entry(mountpoint.clone()).or_default().push(usage);
338                }
339            }
340
341            mappings
342        },
343    );
344
345    // Finally, we have a mapping of disk buffers, based on their underlying mountpoint. Go through
346    // and check to make sure the sum total of `max_size` for all buffers associated with each
347    // mountpoint does not exceed that mountpoint's total capacity.
348    //
349    // We specifically do not do any sort of warning on free space because that has to be the
350    // responsibility of the operator to ensure there's enough total space for all buffers present.
351    let mut errors = Vec::new();
352
353    for (mountpoint, buffers) in mountpoint_buffer_mapping {
354        let buffer_max_size_total: u64 = buffers.iter().map(|usage| usage.max_size()).sum();
355        let mountpoint_total_capacity = mountpoints
356            .get(&mountpoint)
357            .copied()
358            .expect("mountpoint must exist");
359
360        if buffer_max_size_total > mountpoint_total_capacity {
361            let component_ids = buffers
362                .iter()
363                .map(|usage| usage.id().id())
364                .collect::<Vec<_>>();
365            errors.push(format!(
366                "Mountpoint '{}' has total capacity of {} bytes, but configured buffers using mountpoint have total maximum size of {} bytes. \
367Reduce the `max_size` of the buffers to fit within the total capacity of the mountpoint. (components associated with mountpoint: {})",
368                mountpoint.to_string_lossy(), mountpoint_total_capacity, buffer_max_size_total, component_ids.join(", "),
369            ));
370        }
371    }
372
373    if errors.is_empty() {
374        Ok(())
375    } else {
376        Err(errors)
377    }
378}
379
380async fn process_partitions(partitions: Vec<Partition>) -> heim::Result<IndexMap<PathBuf, u64>> {
381    stream::iter(partitions)
382        .map(Ok)
383        .and_then(|partition| {
384            let mountpoint_path = partition.mount_point().to_path_buf();
385            heim::disk::usage(mountpoint_path.clone())
386                .map(|usage| usage.map(|usage| (mountpoint_path, usage.total().get::<byte>())))
387        })
388        .try_collect::<IndexMap<_, _>>()
389        .await
390}
391
392pub fn warnings(config: &Config) -> Vec<String> {
393    let mut warnings = vec![];
394
395    let table_sources = config
396        .enrichment_tables
397        .iter()
398        .filter_map(|(key, table)| table.as_source(key))
399        .collect::<Vec<_>>();
400    let source_ids = config
401        .sources
402        .iter()
403        .chain(table_sources.iter().map(|(k, s)| (k, s)))
404        .flat_map(|(key, source)| {
405            source
406                .inner
407                .outputs(config.schema.log_namespace())
408                .iter()
409                .map(|output| {
410                    if let Some(port) = &output.port {
411                        ("source", OutputId::from((key, port.clone())))
412                    } else {
413                        ("source", OutputId::from(key))
414                    }
415                })
416                .collect::<Vec<_>>()
417        });
418    let transform_ids = config.transforms.iter().flat_map(|(key, transform)| {
419        get_transform_output_ids(
420            transform.inner.as_ref(),
421            key.clone(),
422            config.schema.log_namespace(),
423        )
424        .map(|output| ("transform", output))
425        .collect::<Vec<_>>()
426    });
427
428    let table_sinks = config
429        .enrichment_tables
430        .iter()
431        .filter_map(|(key, table)| table.as_sink(key))
432        .collect::<Vec<_>>();
433    for (input_type, id) in transform_ids.chain(source_ids) {
434        if !config
435            .transforms
436            .iter()
437            .any(|(_, transform)| transform.inputs.contains(&id))
438            && !config
439                .sinks
440                .iter()
441                .any(|(_, sink)| sink.inputs.contains(&id))
442            && !table_sinks
443                .iter()
444                .any(|(_, sink)| sink.inputs.contains(&id))
445        {
446            warnings.push(format!(
447                "{} \"{}\" has no consumers",
448                capitalize(input_type),
449                id
450            ));
451        }
452    }
453
454    warnings
455}
456
457fn capitalize(s: &str) -> String {
458    let mut s = s.to_owned();
459    if let Some(r) = s.get_mut(0..1) {
460        r.make_ascii_uppercase();
461    }
462    s
463}