Skip to content

Commit

Permalink
chore(config): log component graph when there's an error (#21669)
Browse files Browse the repository at this point in the history
* chore(config): add message to help debug problematic config

* fmt

* clippy fixes

* update test
  • Loading branch information
pront authored Nov 6, 2024
1 parent fb53a2d commit e2b83f2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
35 changes: 30 additions & 5 deletions lib/vector-core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub const MEMORY_BUFFER_DEFAULT_MAX_EVENTS: NonZeroUsize =
// This enum should be kept alphabetically sorted as the bitmask value is used when
// sorting sources by data type in the GraphQL API.
#[bitmask(u8)]
#[bitmask_config(flags_iter)]
pub enum DataType {
Log,
Metric,
Expand All @@ -38,11 +39,11 @@ pub enum DataType {

impl fmt::Display for DataType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut t = Vec::new();
self.contains(DataType::Log).then(|| t.push("Log"));
self.contains(DataType::Metric).then(|| t.push("Metric"));
self.contains(DataType::Trace).then(|| t.push("Trace"));
f.write_str(&t.join(","))
f.debug_list()
.entries(
Self::flags().filter_map(|&(name, value)| self.contains(value).then_some(name)),
)
.finish()
}
}

Expand Down Expand Up @@ -191,6 +192,24 @@ impl SourceOutput {
}
}

fn fmt_helper(
f: &mut fmt::Formatter<'_>,
maybe_port: Option<&String>,
data_type: DataType,
) -> fmt::Result {
match maybe_port {
Some(port) => write!(f, "port: \"{port}\",",),
None => write!(f, "port: None,"),
}?;
write!(f, " types: {data_type}")
}

impl fmt::Display for SourceOutput {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_helper(f, self.port.as_ref(), self.ty)
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct TransformOutput {
pub port: Option<String>,
Expand All @@ -203,6 +222,12 @@ pub struct TransformOutput {
pub log_schema_definitions: HashMap<OutputId, schema::Definition>,
}

impl fmt::Display for TransformOutput {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt_helper(f, self.port.as_ref(), self.ty)
}
}

impl TransformOutput {
/// Create a `TransformOutput` of the given data type that contains multiple [`schema::Definition`]s.
/// Designed for use in transforms.
Expand Down
46 changes: 40 additions & 6 deletions src/config/graph.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use indexmap::{set::IndexSet, IndexMap};
use std::collections::{HashMap, HashSet, VecDeque};

use super::{
schema, ComponentKey, DataType, OutputId, SinkOuter, SourceOuter, SourceOutput, TransformOuter,
TransformOutput,
};
use indexmap::{set::IndexSet, IndexMap};
use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt;

#[derive(Debug, Clone)]
pub enum Node {
Expand All @@ -20,6 +20,33 @@ pub enum Node {
},
}

impl fmt::Display for Node {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Node::Source { outputs } => {
write!(f, "component_kind: source\n outputs:")?;
for output in outputs {
write!(f, "\n {}", output)?;
}
Ok(())
}
Node::Transform { in_ty, outputs } => {
write!(
f,
"component_kind: source\n input_types: {in_ty}\n outputs:"
)?;
for output in outputs {
write!(f, "\n {}", output)?;
}
Ok(())
}
Node::Sink { ty } => {
write!(f, "component_kind: sink\n types: {ty}")
}
}
}
}

#[derive(Debug, Clone)]
struct Edge {
from: OutputId,
Expand Down Expand Up @@ -139,9 +166,16 @@ impl Graph {
Some(Node::Sink { .. }) => "sink",
_ => panic!("only transforms and sinks have inputs"),
};
info!(
"Available components:\n{}",
self.nodes
.iter()
.map(|(key, node)| format!("\"{}\":\n {}", key, node))
.collect::<Vec<_>>()
.join("\n")
);
Err(format!(
"Input \"{}\" for {} \"{}\" doesn't match any components.",
from, output_type, to
"Input \"{from}\" for {output_type} \"{to}\" doesn't match any components.",
))
}
}
Expand Down Expand Up @@ -510,7 +544,7 @@ mod test {

assert_eq!(
Err(vec![
"Data type mismatch between in (Log) and out (Metric)".into()
"Data type mismatch between in ([\"Log\"]) and out ([\"Metric\"])".into()
]),
graph.typecheck()
);
Expand Down

0 comments on commit e2b83f2

Please sign in to comment.