Skip to content

Commit

Permalink
fix(config): For templates using stftime specifiers use semantic time…
Browse files Browse the repository at this point in the history
…stamp (#20851)

* fix(config): For templates using stftime specifiers use semantic timestamp

Closes: #20785

Signed-off-by: Jesse Szwedko <[email protected]>

* Use Vector namespace

Signed-off-by: Jesse Szwedko <[email protected]>

---------

Signed-off-by: Jesse Szwedko <[email protected]>
  • Loading branch information
jszwedko authored Jul 19, 2024
1 parent 0c1d3b1 commit b679245
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
2 changes: 2 additions & 0 deletions changelog.d/template-semantic-timestamp.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Templates using strftime format specifiers now correctly use the semantic timestamp rather than
always looking for the `log_schema` timestamp. This is required when `log_namespacing` is enabled.
37 changes: 30 additions & 7 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,7 @@ fn render_timestamp(
tz_offset: Option<FixedOffset>,
) -> String {
let timestamp = match event {
EventRef::Log(log) => log_schema()
.timestamp_key_target_path()
.and_then(|timestamp_key| {
log.get(timestamp_key)
.and_then(Value::as_timestamp)
.copied()
}),
EventRef::Log(log) => log.get_timestamp().and_then(Value::as_timestamp).copied(),
EventRef::Metric(metric) => metric.timestamp(),
EventRef::Trace(trace) => {
log_schema()
Expand Down Expand Up @@ -398,6 +392,7 @@ fn render_timestamp(
mod tests {
use chrono::{Offset, TimeZone, Utc};
use chrono_tz::Tz;
use vector_lib::config::LogNamespace;
use vector_lib::lookup::{metadata_path, PathPrefix};
use vector_lib::metric_tags;

Expand Down Expand Up @@ -537,6 +532,34 @@ mod tests {
assert_eq!(Ok(Bytes::from("abcd-2001-02-03")), template.render(&event))
}

#[test]
fn render_log_timestamp_strftime_style_namespace() {
let ts = Utc
.with_ymd_and_hms(2001, 2, 3, 4, 5, 6)
.single()
.expect("invalid timestamp");

let mut event = Event::Log(LogEvent::from("hello world"));
event.as_mut_log().insert("@timestamp", ts);
// use Vector namespace instead of legacy
LogNamespace::Vector.insert_vector_metadata(event.as_mut_log(), Some("foo"), "foo", "bar");
let new_schema = event
.as_mut_log()
.metadata()
.schema_definition()
.as_ref()
.clone()
.with_meaning(parse_target_path("@timestamp").unwrap(), "timestamp");
event
.as_mut_log()
.metadata_mut()
.set_schema_definition(&std::sync::Arc::new(new_schema));

let template = Template::try_from("abcd-%F").unwrap();

assert_eq!(Ok(Bytes::from("abcd-2001-02-03")), template.render(&event))
}

#[test]
fn render_log_timestamp_multiple_strftime_style() {
let ts = Utc
Expand Down

0 comments on commit b679245

Please sign in to comment.