Skip to content

Commit e9dd21f

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

File tree

24 files changed

+177
-148
lines changed

24 files changed

+177
-148
lines changed

benches/event.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use bytes::Bytes;
22
use criterion::{criterion_group, BatchSize, Criterion};
33
use vector::event::LogEvent;
4+
use vrl::event_path;
45

56
fn benchmark_event_iterate(c: &mut Criterion) {
67
let mut group = c.benchmark_group("event/iterate");
@@ -9,9 +10,9 @@ fn benchmark_event_iterate(c: &mut Criterion) {
910
b.iter_batched_ref(
1011
|| {
1112
let mut log = LogEvent::default();
12-
log.insert("key1", Bytes::from("value1"));
13-
log.insert("key2", Bytes::from("value2"));
14-
log.insert("key3", Bytes::from("value3"));
13+
log.insert(event_path!("key1"), Bytes::from("value1"));
14+
log.insert(event_path!("key2"), Bytes::from("value2"));
15+
log.insert(event_path!("key3"), Bytes::from("value3"));
1516
log
1617
},
1718
|e| e.all_fields().unwrap().count(),
@@ -23,9 +24,15 @@ fn benchmark_event_iterate(c: &mut Criterion) {
2324
b.iter_batched_ref(
2425
|| {
2526
let mut log = LogEvent::default();
26-
log.insert("key1.nested1.nested2", Bytes::from("value1"));
27-
log.insert("key1.nested1.nested3", Bytes::from("value4"));
28-
log.insert("key3", Bytes::from("value3"));
27+
log.insert(
28+
event_path!("key1", "nested1", "nested2"),
29+
Bytes::from("value1"),
30+
);
31+
log.insert(
32+
event_path!("key1", "nested1", "nested3"),
33+
Bytes::from("value4"),
34+
);
35+
log.insert(event_path!("key3"), Bytes::from("value3"));
2936
log
3037
},
3138
|e| e.all_fields().unwrap().count(),
@@ -37,8 +44,8 @@ fn benchmark_event_iterate(c: &mut Criterion) {
3744
b.iter_batched_ref(
3845
|| {
3946
let mut log = LogEvent::default();
40-
log.insert("key1.nested1[0]", Bytes::from("value1"));
41-
log.insert("key1.nested1[1]", Bytes::from("value2"));
47+
log.insert(event_path!("key1", "nested1", "0"), Bytes::from("value1"));
48+
log.insert(event_path!("key1", "nested1", "1"), Bytes::from("value2"));
4249
log
4350
},
4451
|e| e.all_fields().unwrap().count(),
@@ -53,25 +60,31 @@ fn benchmark_event_create(c: &mut Criterion) {
5360
group.bench_function("single-level", |b| {
5461
b.iter(|| {
5562
let mut log = LogEvent::default();
56-
log.insert("key1", Bytes::from("value1"));
57-
log.insert("key2", Bytes::from("value2"));
58-
log.insert("key3", Bytes::from("value3"));
63+
log.insert(event_path!("key1"), Bytes::from("value1"));
64+
log.insert(event_path!("key2"), Bytes::from("value2"));
65+
log.insert(event_path!("key3"), Bytes::from("value3"));
5966
})
6067
});
6168

6269
group.bench_function("nested-keys", |b| {
6370
b.iter(|| {
6471
let mut log = LogEvent::default();
65-
log.insert("key1.nested1.nested2", Bytes::from("value1"));
66-
log.insert("key1.nested1.nested3", Bytes::from("value4"));
67-
log.insert("key3", Bytes::from("value3"));
72+
log.insert(
73+
event_path!("key1", "nested1", "nested2"),
74+
Bytes::from("value1"),
75+
);
76+
log.insert(
77+
event_path!("key1", "nested1", "nested3"),
78+
Bytes::from("value4"),
79+
);
80+
log.insert(event_path!("key3"), Bytes::from("value3"));
6881
})
6982
});
7083
group.bench_function("array", |b| {
7184
b.iter(|| {
7285
let mut log = LogEvent::default();
73-
log.insert("key1.nested1[0]", Bytes::from("value1"));
74-
log.insert("key1.nested1[1]", Bytes::from("value2"));
86+
log.insert(event_path!("key1", "nested1", "0"), Bytes::from("value1"));
87+
log.insert(event_path!("key1", "nested1", "1"), Bytes::from("value2"));
7588
})
7689
});
7790
}

benches/lua.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use vector::{
99
test_util::collect_ready,
1010
transforms::{self, OutputBuffer, Transform},
1111
};
12+
use vrl::event_path;
1213

1314
fn bench_add_fields(c: &mut Criterion) {
1415
let event = Event::from(LogEvent::default());
@@ -87,7 +88,7 @@ fn bench_field_filter(c: &mut Criterion) {
8788
let events = (0..num_events)
8889
.map(|i| {
8990
let mut event = LogEvent::default();
90-
event.insert("the_field", (i % 10).to_string());
91+
event.insert(event_path!("the_field"), (i % 10).to_string());
9192
Event::from(event)
9293
})
9394
.collect::<Vec<_>>();

benches/remap.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use vector::{
1212
},
1313
};
1414
use vector_common::TimeZone;
15+
use vrl::event_path;
1516
use vrl::prelude::*;
1617

1718
criterion_group!(
@@ -35,9 +36,9 @@ fn benchmark_remap(c: &mut Criterion) {
3536
let result = outputs.take_primary();
3637
let output_1 = result.first().unwrap().as_log();
3738

38-
debug_assert_eq!(output_1.get("foo").unwrap().to_string_lossy(), "bar");
39-
debug_assert_eq!(output_1.get("bar").unwrap().to_string_lossy(), "baz");
40-
debug_assert_eq!(output_1.get("copy").unwrap().to_string_lossy(), "buz");
39+
debug_assert_eq!(output_1.get(event_path!("foo")).unwrap().to_string_lossy(), "bar");
40+
debug_assert_eq!(output_1.get(event_path!("bar")).unwrap().to_string_lossy(), "baz");
41+
debug_assert_eq!(output_1.get(event_path!("copy")).unwrap().to_string_lossy(), "buz");
4142

4243
result
4344
};
@@ -67,7 +68,9 @@ fn benchmark_remap(c: &mut Criterion) {
6768

6869
let event = {
6970
let mut event = Event::Log(LogEvent::from("augment me"));
70-
event.as_mut_log().insert("copy_from", "buz".to_owned());
71+
event
72+
.as_mut_log()
73+
.insert(event_path!("copy_from"), "buz".to_owned());
7174
event
7275
};
7376

@@ -88,11 +91,11 @@ fn benchmark_remap(c: &mut Criterion) {
8891
let output_1 = result.first().unwrap().as_log();
8992

9093
debug_assert_eq!(
91-
output_1.get("foo").unwrap().to_string_lossy(),
94+
output_1.get(event_path!("foo")).unwrap().to_string_lossy(),
9295
r#"{"key": "value"}"#
9396
);
9497
debug_assert_eq!(
95-
output_1.get("bar").unwrap().to_string_lossy(),
98+
output_1.get(event_path!("bar")).unwrap().to_string_lossy(),
9699
r#"{"key":"value"}"#
97100
);
98101

@@ -141,10 +144,10 @@ fn benchmark_remap(c: &mut Criterion) {
141144
let result = outputs.take_primary();
142145
let output_1 = result.first().unwrap().as_log();
143146

144-
debug_assert_eq!(output_1.get("number").unwrap(), &Value::Integer(1234));
145-
debug_assert_eq!(output_1.get("bool").unwrap(), &Value::Boolean(true));
147+
debug_assert_eq!(output_1.get(event_path!("number")).unwrap(), &Value::Integer(1234));
148+
debug_assert_eq!(output_1.get(event_path!("bool")).unwrap(), &Value::Boolean(true));
146149
debug_assert_eq!(
147-
output_1.get("timestamp").unwrap(),
150+
output_1.get(event_path!("timestamp")).unwrap(),
148151
&Value::Timestamp(timestamp),
149152
);
150153

@@ -176,7 +179,7 @@ fn benchmark_remap(c: &mut Criterion) {
176179
("bool", "yes"),
177180
("timestamp", "19/06/2019:17:20:49 -0400"),
178181
] {
179-
event.as_mut_log().insert(key, value.to_owned());
182+
event.as_mut_log().insert(event_path!(key), value.to_owned());
180183
}
181184

182185
let timestamp =

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 owned target 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+
/// Lazily initialized singleton.
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: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@ use criterion::{
66
use lookup::event_path;
77
use vector_core::event::LogEvent;
88

9+
fn default_log_event() -> LogEvent {
10+
let mut log_event = LogEvent::default();
11+
log_event.insert(event_path!("one"), 1);
12+
log_event.insert(event_path!("two"), 2);
13+
log_event.insert(event_path!("three"), 3);
14+
log_event
15+
}
16+
917
fn rename_key_flat(c: &mut Criterion) {
1018
let mut group: BenchmarkGroup<WallTime> =
1119
c.benchmark_group("vector_core::event::log_event::LogEvent::rename_key_flat");
1220
group.sampling_mode(SamplingMode::Auto);
1321

1422
group.bench_function("rename_flat_key (key is present)", move |b| {
1523
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-
},
24+
default_log_event,
2325
|mut log_event| {
2426
log_event.rename_key(event_path!("one"), event_path!("1"));
2527
},
@@ -29,13 +31,7 @@ fn rename_key_flat(c: &mut Criterion) {
2931

3032
group.bench_function("rename_flat_key (key is NOT present)", move |b| {
3133
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-
},
34+
default_log_event,
3935
|mut log_event| {
4036
log_event.rename_key(event_path!("four"), event_path!("4"));
4137
},

0 commit comments

Comments
 (0)