Skip to content

Commit

Permalink
fix(remap transform): log namespace should be used when splitting eve…
Browse files Browse the repository at this point in the history
…nts from arrays (vectordotdev#18372)

fix remap array return values when using the Vector namespace
  • Loading branch information
fuchsnj authored Aug 24, 2023
1 parent eac3cd0 commit 15a63b4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/vector-core/src/event/vrl_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub struct TargetIter<T> {
iter: std::vec::IntoIter<Value>,
metadata: EventMetadata,
_marker: PhantomData<T>,
log_namespace: LogNamespace,
}

fn create_log_event(value: Value, metadata: EventMetadata) -> LogEvent {
Expand All @@ -63,9 +64,12 @@ impl Iterator for TargetIter<LogEvent> {

fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|v| {
match v {
value @ Value::Object(_) => LogEvent::from_parts(value, self.metadata.clone()),
value => create_log_event(value, self.metadata.clone()),
match self.log_namespace {
LogNamespace::Legacy => match v {
value @ Value::Object(_) => LogEvent::from_parts(value, self.metadata.clone()),
value => create_log_event(value, self.metadata.clone()),
},
LogNamespace::Vector => LogEvent::from_parts(v, self.metadata.clone()),
}
.into()
})
Expand Down Expand Up @@ -142,6 +146,7 @@ impl VrlTarget {
iter: values.into_iter(),
metadata,
_marker: PhantomData,
log_namespace,
}),

v => match log_namespace {
Expand All @@ -161,6 +166,7 @@ impl VrlTarget {
iter: values.into_iter(),
metadata,
_marker: PhantomData,
log_namespace,
}),

v => TargetEvents::One(create_log_event(v, metadata).into()),
Expand Down
51 changes: 51 additions & 0 deletions src/transforms/remap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1890,4 +1890,55 @@ mod tests {
outputs1[0].schema_definitions(true),
);
}

#[test]
fn check_remap_array_vector_namespace() {
let event = {
let mut event = LogEvent::from("input");
// mark the event as a "Vector" namespaced log
event
.metadata_mut()
.value_mut()
.insert("vector", BTreeMap::new());
Event::from(event)
};

let conf = RemapConfig {
source: Some(
r#". = [null]
"#
.to_string(),
),
file: None,
drop_on_error: true,
drop_on_abort: false,
..Default::default()
};
let mut tform = remap(conf.clone()).unwrap();
let result = transform_one(&mut tform, event).unwrap();

// Legacy namespace nests this under "message", Vector should set it as the root
assert_eq!(result.as_log().get("."), Some(&Value::Null));

let enrichment_tables = enrichment::TableRegistry::default();
let outputs1 = conf.outputs(
enrichment_tables,
&[(
"in".into(),
schema::Definition::new_with_default_metadata(
Kind::any_object(),
[LogNamespace::Vector],
),
)],
LogNamespace::Vector,
);

let wanted =
schema::Definition::new_with_default_metadata(Kind::null(), [LogNamespace::Vector]);

assert_eq!(
HashMap::from([(OutputId::from("in"), wanted)]),
outputs1[0].schema_definitions(true),
);
}
}

0 comments on commit 15a63b4

Please sign in to comment.