Skip to content

Commit 8663602

Browse files
authored
feat: Migrate LogSchema::metadata key to new lookup code (vectordotdev#18058)
* Migrate LogSchema::metadata_key to new lookup code * add generic maybe_insert function * updates after rebasing on latest master * fix tests
1 parent db9e47f commit 8663602

File tree

3 files changed

+56
-33
lines changed

3 files changed

+56
-33
lines changed

lib/vector-core/src/config/log_schema.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub struct LogSchema {
7373
/// Generally, this field will be set by Vector to hold event-specific metadata, such as
7474
/// annotations by the `remap` transform when an error or abort is encountered.
7575
#[serde(default = "LogSchema::default_metadata_key")]
76-
metadata_key: String,
76+
metadata_key: OptionalValuePath,
7777
}
7878

7979
impl Default for LogSchema {
@@ -105,8 +105,8 @@ impl LogSchema {
105105
OptionalValuePath::new(SOURCE_TYPE)
106106
}
107107

108-
fn default_metadata_key() -> String {
109-
String::from(METADATA)
108+
fn default_metadata_key() -> OptionalValuePath {
109+
OptionalValuePath::new(METADATA)
110110
}
111111

112112
pub fn message_key(&self) -> Option<&OwnedValuePath> {
@@ -134,8 +134,8 @@ impl LogSchema {
134134
self.source_type_key.path.as_ref()
135135
}
136136

137-
pub fn metadata_key(&self) -> &str {
138-
&self.metadata_key
137+
pub fn metadata_key(&self) -> Option<&OwnedValuePath> {
138+
self.metadata_key.path.as_ref()
139139
}
140140

141141
pub fn set_message_key(&mut self, path: Option<OwnedValuePath>) {
@@ -154,8 +154,8 @@ impl LogSchema {
154154
self.source_type_key = OptionalValuePath { path };
155155
}
156156

157-
pub fn set_metadata_key(&mut self, v: String) {
158-
self.metadata_key = v;
157+
pub fn set_metadata_key(&mut self, path: Option<OwnedValuePath>) {
158+
self.metadata_key = OptionalValuePath { path };
159159
}
160160

161161
/// Merge two `LogSchema` instances together.
@@ -202,7 +202,7 @@ impl LogSchema {
202202
{
203203
errors.push("conflicting values for 'log_schema.metadata_key' found".to_owned());
204204
} else {
205-
self.set_metadata_key(other.metadata_key().to_string());
205+
self.set_metadata_key(other.metadata_key().cloned());
206206
}
207207
}
208208

lib/vector-core/src/event/trace.rs

+17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use vector_common::{
77
internal_event::TaggedEventsSent, json_size::JsonSize, request_metadata::GetEventCountTags,
88
EventDataEq,
99
};
10+
use vrl::path::{PathPrefix, ValuePath};
1011

1112
use super::{
1213
BatchNotifier, EstimatedJsonEncodedSizeOf, EventFinalizer, EventFinalizers, EventMetadata,
@@ -84,13 +85,29 @@ impl TraceEvent {
8485
self.0.contains(key.as_ref())
8586
}
8687

88+
// TODO This should eventually use TargetPath for the `key` parameter.
89+
// https://github.com/vectordotdev/vector/issues/18059
8790
pub fn insert(
8891
&mut self,
8992
key: impl AsRef<str>,
9093
value: impl Into<Value> + Debug,
9194
) -> Option<Value> {
9295
self.0.insert(key.as_ref(), value.into())
9396
}
97+
98+
// TODO Audit code and use this if possible.
99+
// https://github.com/vectordotdev/vector/issues/18059
100+
pub fn maybe_insert<'a, F: FnOnce() -> Value>(
101+
&mut self,
102+
prefix: PathPrefix,
103+
path: Option<impl ValuePath<'a>>,
104+
value_callback: F,
105+
) -> Option<Value> {
106+
if let Some(path) = path {
107+
return self.0.insert((prefix, path), value_callback());
108+
}
109+
None
110+
}
94111
}
95112

96113
impl From<LogEvent> for TraceEvent {

src/transforms/remap.rs

+31-25
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use std::{
77
};
88

99
use codecs::MetricTagValues;
10-
use lookup::lookup_v2::{parse_value_path, ValuePath};
11-
use lookup::{metadata_path, owned_value_path, path, PathPrefix};
10+
use lookup::{metadata_path, owned_value_path, PathPrefix};
1211
use snafu::{ResultExt, Snafu};
1312
use vector_common::TimeZone;
1413
use vector_config::configurable_component;
@@ -20,6 +19,8 @@ use vrl::compiler::runtime::{Runtime, Terminate};
2019
use vrl::compiler::state::ExternalEnv;
2120
use vrl::compiler::{CompileConfig, ExpressionError, Function, Program, TypeState, VrlRuntime};
2221
use vrl::diagnostic::{DiagnosticMessage, Formatter, Note};
22+
use vrl::path;
23+
use vrl::path::ValuePath;
2324
use vrl::value::{Kind, Value};
2425

2526
use crate::config::OutputId;
@@ -288,7 +289,7 @@ impl TransformConfig for RemapConfig {
288289
let dropped_definition = Definition::combine_log_namespaces(
289290
input_definition.log_namespaces(),
290291
input_definition.clone().with_event_field(
291-
&parse_value_path(log_schema().metadata_key()).expect("valid metadata key"),
292+
log_schema().metadata_key().expect("valid metadata key"),
292293
Kind::object(BTreeMap::from([
293294
("reason".into(), Kind::bytes()),
294295
("message".into(), Kind::bytes()),
@@ -451,13 +452,12 @@ where
451452
match event {
452453
Event::Log(ref mut log) => match log.namespace() {
453454
LogNamespace::Legacy => {
454-
log.insert(
455-
(
456-
PathPrefix::Event,
457-
log_schema().metadata_key().concat(path!("dropped")),
458-
),
459-
self.dropped_data(reason, error),
460-
);
455+
if let Some(metadata_key) = log_schema().metadata_key() {
456+
log.insert(
457+
(PathPrefix::Event, metadata_key.concat(path!("dropped"))),
458+
self.dropped_data(reason, error),
459+
);
460+
}
461461
}
462462
LogNamespace::Vector => {
463463
log.insert(
@@ -467,23 +467,29 @@ where
467467
}
468468
},
469469
Event::Metric(ref mut metric) => {
470-
let m = log_schema().metadata_key();
471-
metric.replace_tag(format!("{}.dropped.reason", m), reason.into());
472-
metric.replace_tag(
473-
format!("{}.dropped.component_id", m),
474-
self.component_key
475-
.as_ref()
476-
.map(ToString::to_string)
477-
.unwrap_or_else(String::new),
478-
);
479-
metric.replace_tag(format!("{}.dropped.component_type", m), "remap".into());
480-
metric.replace_tag(format!("{}.dropped.component_kind", m), "transform".into());
470+
if let Some(metadata_key) = log_schema().metadata_key() {
471+
metric.replace_tag(format!("{}.dropped.reason", metadata_key), reason.into());
472+
metric.replace_tag(
473+
format!("{}.dropped.component_id", metadata_key),
474+
self.component_key
475+
.as_ref()
476+
.map(ToString::to_string)
477+
.unwrap_or_else(String::new),
478+
);
479+
metric.replace_tag(
480+
format!("{}.dropped.component_type", metadata_key),
481+
"remap".into(),
482+
);
483+
metric.replace_tag(
484+
format!("{}.dropped.component_kind", metadata_key),
485+
"transform".into(),
486+
);
487+
}
481488
}
482489
Event::Trace(ref mut trace) => {
483-
trace.insert(
484-
log_schema().metadata_key(),
485-
self.dropped_data(reason, error),
486-
);
490+
trace.maybe_insert(PathPrefix::Event, log_schema().metadata_key(), || {
491+
self.dropped_data(reason, error).into()
492+
});
487493
}
488494
}
489495
}

0 commit comments

Comments
 (0)