Skip to content

Commit 5692dc8

Browse files
committed
chore: string paths should not be used outside test code
1 parent 8a2f8f6 commit 5692dc8

File tree

21 files changed

+115
-117
lines changed

21 files changed

+115
-117
lines changed

lib/codecs/src/decoding/format/gelf.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use vrl::value::kind::Collection;
1717
use vrl::value::{Kind, Value};
1818

1919
use super::{default_lossy, Deserializer};
20+
use crate::gelf::GELF_TARGET_PATHS;
2021
use crate::{gelf_fields::*, VALID_FIELD_REGEX};
2122

2223
/// On GELF decoding behavior:
@@ -123,11 +124,11 @@ impl GelfDeserializer {
123124
.into());
124125
}
125126

126-
log.insert(VERSION, parsed.version.to_string());
127-
log.insert(HOST, parsed.host.to_string());
127+
log.insert(&GELF_TARGET_PATHS.version, parsed.version.to_string());
128+
log.insert(&GELF_TARGET_PATHS.host, parsed.host.to_string());
128129

129130
if let Some(full_message) = &parsed.full_message {
130-
log.insert(FULL_MESSAGE, full_message.to_string());
131+
log.insert(&GELF_TARGET_PATHS.full_message, full_message.to_string());
131132
}
132133

133134
if let Some(timestamp_key) = log_schema().timestamp_key_target_path() {
@@ -145,19 +146,19 @@ impl GelfDeserializer {
145146
}
146147

147148
if let Some(level) = parsed.level {
148-
log.insert(LEVEL, level);
149+
log.insert(&GELF_TARGET_PATHS.level, level);
149150
}
150151
if let Some(facility) = &parsed.facility {
151-
log.insert(FACILITY, facility.to_string());
152+
log.insert(&GELF_TARGET_PATHS.facility, facility.to_string());
152153
}
153154
if let Some(line) = parsed.line {
154155
log.insert(
155-
LINE,
156+
&GELF_TARGET_PATHS.line,
156157
Value::Float(ordered_float::NotNan::new(line).expect("JSON doesn't allow NaNs")),
157158
);
158159
}
159160
if let Some(file) = &parsed.file {
160-
log.insert(FILE, file.to_string());
161+
log.insert(&GELF_TARGET_PATHS.file, file.to_string());
161162
}
162163

163164
if let Some(add) = &parsed.additional_fields {

lib/codecs/src/encoding/format/gelf.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::gelf::GELF_TARGET_PATHS;
12
use crate::{gelf_fields::*, VALID_FIELD_REGEX};
23
use bytes::{BufMut, BytesMut};
34
use lookup::event_path;
@@ -12,7 +13,6 @@ use vector_core::{
1213
event::Value,
1314
schema,
1415
};
15-
use vrl::path::PathPrefix;
1616

1717
/// On GELF encoding behavior:
1818
/// Graylog has a relaxed parsing. They are much more lenient than the spec would
@@ -131,20 +131,18 @@ fn coerce_required_fields(mut log: LogEvent) -> vector_common::Result<LogEvent>
131131
}
132132

133133
// add the VERSION if it does not exist
134-
if !log.contains(VERSION) {
135-
log.insert(VERSION, GELF_VERSION);
134+
if !log.contains(&GELF_TARGET_PATHS.version) {
135+
log.insert(&GELF_TARGET_PATHS.version, GELF_VERSION);
136136
}
137137

138-
if !log.contains(HOST) {
138+
if !log.contains(&GELF_TARGET_PATHS.host) {
139139
err_missing_field(HOST)?;
140140
}
141141

142-
if !log.contains(SHORT_MESSAGE) {
143-
if let Some(message_key) = log_schema().message_key() {
144-
// rename the log_schema().message_key() to SHORT_MESSAGE
145-
let target_path = (PathPrefix::Event, message_key);
146-
if log.contains(target_path) {
147-
log.rename_key(target_path, SHORT_MESSAGE);
142+
if !log.contains(&GELF_TARGET_PATHS.short_message) {
143+
if let Some(message_key) = log_schema().message_key_target_path() {
144+
if log.contains(message_key) {
145+
log.rename_key(message_key, &GELF_TARGET_PATHS.short_message);
148146
} else {
149147
err_missing_field(SHORT_MESSAGE)?;
150148
}

lib/codecs/src/gelf.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
33
use once_cell::sync::Lazy;
44
use regex::Regex;
5+
use vrl::owned_value_path;
6+
use vrl::path::OwnedTargetPath;
57

68
/// GELF Message fields. Definitions from <https://docs.graylog.org/docs/gelf>.
79
pub mod gelf_fields {
8-
910
/// (not a field) The latest version of the GELF specification.
1011
pub const GELF_VERSION: &str = "1.1";
1112

@@ -40,6 +41,30 @@ pub mod gelf_fields {
4041
// < Every field with an underscore (_) prefix will be treated as an additional field. >
4142
}
4243

44+
/// GELF event paths.
45+
pub(crate) struct GelfTargetPaths {
46+
pub version: OwnedTargetPath,
47+
pub host: OwnedTargetPath,
48+
pub full_message: OwnedTargetPath,
49+
pub level: OwnedTargetPath,
50+
pub facility: OwnedTargetPath,
51+
pub line: OwnedTargetPath,
52+
pub file: OwnedTargetPath,
53+
pub short_message: OwnedTargetPath,
54+
}
55+
56+
/// GELF target paths.
57+
pub(crate) static GELF_TARGET_PATHS: Lazy<GelfTargetPaths> = Lazy::new(|| GelfTargetPaths {
58+
version: OwnedTargetPath::event(owned_value_path!(gelf_fields::VERSION)),
59+
host: OwnedTargetPath::event(owned_value_path!(gelf_fields::HOST)),
60+
full_message: OwnedTargetPath::event(owned_value_path!(gelf_fields::FULL_MESSAGE)),
61+
level: OwnedTargetPath::event(owned_value_path!(gelf_fields::LEVEL)),
62+
facility: OwnedTargetPath::event(owned_value_path!(gelf_fields::FACILITY)),
63+
line: OwnedTargetPath::event(owned_value_path!(gelf_fields::LINE)),
64+
file: OwnedTargetPath::event(owned_value_path!(gelf_fields::FILE)),
65+
short_message: OwnedTargetPath::event(owned_value_path!(gelf_fields::SHORT_MESSAGE)),
66+
});
67+
4368
/// Regex for matching valid field names. Must contain only word chars, periods and dashes.
4469
/// Additional field names must also be prefixed with an `_` , however that is intentionally
4570
/// omitted from this regex to be checked separately to create a specific error message.

lib/vector-core/benches/event/log_event.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ use criterion::{
55
};
66
use lookup::event_path;
77
use vector_core::event::LogEvent;
8+
use vrl::stdlib::Log;
9+
10+
fn default_log_event() -> LogEvent {
11+
let mut log_event = LogEvent::default();
12+
log_event.insert(event_path!("one"), 1);
13+
log_event.insert(event_path!("two"), 2);
14+
log_event.insert(event_path!("three"), 3);
15+
log_event
16+
}
817

918
fn rename_key_flat(c: &mut Criterion) {
1019
let mut group: BenchmarkGroup<WallTime> =
@@ -13,13 +22,7 @@ fn rename_key_flat(c: &mut Criterion) {
1322

1423
group.bench_function("rename_flat_key (key is present)", move |b| {
1524
b.iter_batched(
16-
|| {
17-
let mut log_event = LogEvent::default();
18-
log_event.insert("one", 1);
19-
log_event.insert("two", 2);
20-
log_event.insert("three", 3);
21-
log_event
22-
},
25+
default_log_event,
2326
|mut log_event| {
2427
log_event.rename_key(event_path!("one"), event_path!("1"));
2528
},
@@ -29,13 +32,7 @@ fn rename_key_flat(c: &mut Criterion) {
2932

3033
group.bench_function("rename_flat_key (key is NOT present)", move |b| {
3134
b.iter_batched(
32-
|| {
33-
let mut log_event = LogEvent::default();
34-
log_event.insert("one", 1);
35-
log_event.insert("two", 2);
36-
log_event.insert("three", 3);
37-
log_event
38-
},
35+
default_log_event,
3936
|mut log_event| {
4037
log_event.rename_key(event_path!("four"), event_path!("4"));
4138
},

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::config::{log_schema, telemetry};
3333
use crate::{event::MaybeAsLogMut, ByteSizeOf};
3434
use lookup::{metadata_path, path};
3535
use once_cell::sync::Lazy;
36-
use vrl::owned_value_path;
36+
use vrl::{event_path, owned_value_path};
3737

3838
static VECTOR_SOURCE_TYPE_PATH: Lazy<Option<OwnedTargetPath>> = Lazy::new(|| {
3939
Some(OwnedTargetPath::metadata(owned_value_path!(
@@ -162,8 +162,8 @@ impl LogEvent {
162162
let mut log = LogEvent::default();
163163
log.maybe_insert(log_schema().message_key_target_path(), msg.into());
164164

165-
if let Some(timestamp_key) = log_schema().timestamp_key() {
166-
log.insert((PathPrefix::Event, timestamp_key), Utc::now());
165+
if let Some(timestamp_key) = log_schema().timestamp_key_target_path() {
166+
log.insert(timestamp_key, Utc::now());
167167
}
168168

169169
log
@@ -436,12 +436,13 @@ impl LogEvent {
436436
/// Merge all fields specified at `fields` from `incoming` to `current`.
437437
pub fn merge(&mut self, mut incoming: LogEvent, fields: &[impl AsRef<str>]) {
438438
for field in fields {
439-
let Some(incoming_val) = incoming.remove(field.as_ref()) else {
439+
let field_path = event_path!(field.as_ref());
440+
let Some(incoming_val) = incoming.remove(field_path) else {
440441
continue
441442
};
442-
match self.get_mut(field.as_ref()) {
443+
match self.get_mut(field_path) {
443444
None => {
444-
self.insert(field.as_ref(), incoming_val);
445+
self.insert(field_path, incoming_val);
445446
}
446447
Some(current_val) => current_val.merge(incoming_val),
447448
}
@@ -568,8 +569,8 @@ mod test_utils {
568569
fn from(message: Bytes) -> Self {
569570
let mut log = LogEvent::default();
570571
log.maybe_insert(log_schema().message_key_target_path(), message);
571-
if let Some(timestamp_key) = log_schema().timestamp_key() {
572-
log.insert((PathPrefix::Event, timestamp_key), Utc::now());
572+
if let Some(timestamp_key) = log_schema().timestamp_key_target_path() {
573+
log.insert(timestamp_key, Utc::now());
573574
}
574575
log
575576
}
@@ -684,11 +685,11 @@ impl From<&tracing::Event<'_>> for LogEvent {
684685
event.record(&mut maker);
685686

686687
let mut log = maker;
687-
log.insert("timestamp", now);
688+
log.insert(event_path!("timestamp"), now);
688689

689690
let meta = event.metadata();
690691
log.insert(
691-
"metadata.kind",
692+
event_path!("metadata", "kind"),
692693
if meta.is_event() {
693694
Value::Bytes("event".to_string().into())
694695
} else if meta.is_span() {
@@ -697,42 +698,42 @@ impl From<&tracing::Event<'_>> for LogEvent {
697698
Value::Null
698699
},
699700
);
700-
log.insert("metadata.level", meta.level().to_string());
701+
log.insert(event_path!("metadata", "level"), meta.level().to_string());
701702
log.insert(
702-
"metadata.module_path",
703+
event_path!("metadata", "module_path"),
703704
meta.module_path()
704705
.map_or(Value::Null, |mp| Value::Bytes(mp.to_string().into())),
705706
);
706-
log.insert("metadata.target", meta.target().to_string());
707+
log.insert(event_path!("metadata", "target"), meta.target().to_string());
707708

708709
log
709710
}
710711
}
711712

712713
impl tracing::field::Visit for LogEvent {
713714
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
714-
self.insert(field.name(), value.to_string());
715+
self.insert(event_path!(field.name()), value.to_string());
715716
}
716717

717718
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn Debug) {
718-
self.insert(field.name(), format!("{value:?}"));
719+
self.insert(event_path!(field.name()), format!("{value:?}"));
719720
}
720721

721722
fn record_i64(&mut self, field: &tracing::field::Field, value: i64) {
722-
self.insert(field.name(), value);
723+
self.insert(event_path!(field.name()), value);
723724
}
724725

725726
fn record_u64(&mut self, field: &tracing::field::Field, value: u64) {
726-
let field = field.name();
727+
let field_path = event_path!(field.name());
727728
let converted: Result<i64, _> = value.try_into();
728729
match converted {
729-
Ok(value) => self.insert(field, value),
730-
Err(_) => self.insert(field, value.to_string()),
730+
Ok(value) => self.insert(field_path, value),
731+
Err(_) => self.insert(field_path, value.to_string()),
731732
};
732733
}
733734

734735
fn record_bool(&mut self, field: &tracing::field::Field, value: bool) {
735-
self.insert(field.name(), value);
736+
self.insert(event_path!(field.name()), value);
736737
}
737738
}
738739

lib/vector-core/src/event/test/size_of.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,12 @@ fn log_operation_maintains_size() {
115115
match action {
116116
Action::InsertFlat { key, value } => {
117117
let new_value_sz = value.size_of();
118-
let old_value_sz = log_event
119-
.get((PathPrefix::Event, path!(key.as_str())))
120-
.map_or(0, ByteSizeOf::size_of);
118+
let target_path = (PathPrefix::Event, path!(key.as_str()));
119+
let old_value_sz = log_event.get(target_path).map_or(0, ByteSizeOf::size_of);
121120
if !log_event.contains(key.as_str()) {
122121
current_size += key.size_of();
123122
}
124-
log_event.insert((PathPrefix::Event, path!(&key)), value);
123+
log_event.insert(target_path, value);
125124
current_size -= old_value_sz;
126125
current_size += new_value_sz;
127126
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ impl TraceEvent {
8080
self.0.get_mut(key.as_ref())
8181
}
8282

83-
pub fn contains(&self, key: impl AsRef<str>) -> bool {
84-
self.0.contains(key.as_ref())
83+
pub fn contains<'a>(&self, key: impl TargetPath<'a>) -> bool {
84+
self.0.contains(key)
8585
}
8686

8787
pub fn insert<'a>(

src/codecs/encoding/transformer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl Transformer {
191191
}
192192
}
193193
for (k, v) in unix_timestamps {
194-
log.insert(k.as_str(), v);
194+
log.insert(event_path!(k.as_str()), v);
195195
}
196196
} else {
197197
// root is not an object

src/config/unit_test/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use tokio::sync::{
1515
Mutex,
1616
};
1717
use uuid::Uuid;
18+
use vrl::event_path;
1819

1920
pub use self::unit_test_components::{
2021
UnitTestSinkCheck, UnitTestSinkConfig, UnitTestSinkResult, UnitTestSourceConfig,
@@ -572,7 +573,7 @@ fn build_input_event(input: &TestInput) -> Result<Event, String> {
572573
NotNan::new(*f).map_err(|_| "NaN value not supported".to_string())?,
573574
),
574575
};
575-
event.insert(path.as_str(), value);
576+
event.insert(event_path!(path.as_str()), value);
576577
}
577578
Ok(event.into())
578579
} else {

src/sinks/aws_cloudwatch_logs/integration_tests.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,7 @@ async fn cloudwatch_insert_out_of_range_timestamp() {
188188
let line = input_lines.next().unwrap();
189189
let mut event = LogEvent::from(line.clone());
190190
event.insert(
191-
(
192-
lookup::PathPrefix::Event,
193-
log_schema().timestamp_key().unwrap(),
194-
),
191+
log_schema().timestamp_key_target_path().unwrap(),
195192
now + offset,
196193
);
197194
events.push(Event::Log(event));

src/sinks/aws_cloudwatch_logs/request_builder.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,7 @@ mod tests {
154154
let timestamp = Utc::now();
155155
let message = "event message";
156156
let mut event = LogEvent::from(message);
157-
event.insert(
158-
(
159-
lookup::PathPrefix::Event,
160-
log_schema().timestamp_key().unwrap(),
161-
),
162-
timestamp,
163-
);
157+
event.insert(log_schema().timestamp_key_target_path().unwrap(), timestamp);
164158

165159
let request = request_builder.build(event.into()).unwrap();
166160
assert_eq!(request.timestamp, timestamp.timestamp_millis());

src/sinks/azure_monitor_logs.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,11 +488,13 @@ mod tests {
488488
fn insert_timestamp_kv(log: &mut LogEvent) -> (String, String) {
489489
let now = chrono::Utc::now();
490490

491-
let timestamp_key = log_schema().timestamp_key().unwrap();
492491
let timestamp_value = now.to_rfc3339_opts(chrono::SecondsFormat::Millis, true);
493-
log.insert((PathPrefix::Event, timestamp_key), now);
492+
log.insert(log_schema().timestamp_key_target_path().unwrap(), now);
494493

495-
(timestamp_key.to_string(), timestamp_value)
494+
(
495+
log_schema().timestamp_key().unwrap().to_string(),
496+
timestamp_value,
497+
)
496498
}
497499

498500
#[test]

0 commit comments

Comments
 (0)