Skip to content

Commit

Permalink
add generic maybe_insert function
Browse files Browse the repository at this point in the history
  • Loading branch information
pront committed Jul 21, 2023
1 parent 2e9a322 commit a1c5461
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
4 changes: 0 additions & 4 deletions lib/vector-core/src/config/log_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ use vrl::path::parse_target_path;
static LOG_SCHEMA: OnceCell<LogSchema> = OnceCell::new();
static LOG_SCHEMA_DEFAULT: Lazy<LogSchema> = Lazy::new(LogSchema::default);

const MESSAGE: &str = "message";
const TIMESTAMP: &str = "timestamp";
const HOST: &str = "host";
const SOURCE_TYPE: &str = "source_type";
const METADATA: &str = "metadata";

/// Loads Log Schema from configurations and sets global schema. Once this is
Expand Down
18 changes: 18 additions & 0 deletions lib/vector-core/src/event/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use vector_common::{
internal_event::TaggedEventsSent, json_size::JsonSize, request_metadata::GetEventCountTags,
EventDataEq,
};
use vrl::path::{PathPrefix, ValuePath};

use super::{
BatchNotifier, EstimatedJsonEncodedSizeOf, EventFinalizer, EventFinalizers, EventMetadata,
Expand Down Expand Up @@ -84,13 +85,30 @@ impl TraceEvent {
self.0.contains(key.as_ref())
}

// TODO This should eventually use TargetPath for the `key` parameter.
// https://github.com/vectordotdev/vector/issues/7070
pub fn insert(
&mut self,
key: impl AsRef<str>,
value: impl Into<Value> + Debug,
) -> Option<Value> {
self.0.insert(key.as_ref(), value.into())
}

// TODO Audit code and use this if possible.
// https://github.com/vectordotdev/vector/issues/7070
#[allow(clippy::needless_pass_by_value)] // TargetPath is always a reference
pub fn maybe_insert<'a, F: FnOnce() -> Value>(
&mut self,
prefix: PathPrefix,
path: Option<impl ValuePath<'a>>,
value_callback: F,
) -> Option<Value> {
if let Some(path) = path {
return self.0.insert((prefix, path), value_callback());
}
None
}
}

impl From<LogEvent> for TraceEvent {
Expand Down
6 changes: 3 additions & 3 deletions src/transforms/remap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,9 @@ where
}
}
Event::Trace(ref mut trace) => {
if let Some(metadata_key) = log_schema().metadata_key() {
trace.insert(metadata_key.to_string(), self.dropped_data(reason, error));
}
trace.maybe_insert(PathPrefix::Event, log_schema().metadata_key(), || {
self.dropped_data(reason, error).into()
});
}
}
}
Expand Down

0 comments on commit a1c5461

Please sign in to comment.