Skip to content

Commit

Permalink
fix(journald source): correctly emit metadata to log namespace (#19812)
Browse files Browse the repository at this point in the history
* fix(journald source): correctly emit metadata to log namespace

* Spelling

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

---------

Signed-off-by: Jesse Szwedko <[email protected]>
Co-authored-by: Jesse Szwedko <[email protected]>
  • Loading branch information
dalegaard and jszwedko authored Feb 8, 2024
1 parent de24167 commit 0e6cf3e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ customizability
customtype
cwl
Dailywarehousing
dalegaard
daschl
dashmap
datadir
Expand Down
4 changes: 4 additions & 0 deletions changelog.d/19812_journald_metadata_missing.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fixed an issue where the `journald` source was not correctly emitting metadata when `log_namespace =
True`.

authors: @dalegaard
44 changes: 33 additions & 11 deletions src/sources/journald.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,20 +735,42 @@ impl Drop for RunningJournalctl {
}

fn enrich_log_event(log: &mut LogEvent, log_namespace: LogNamespace) {
if let Some(host) = log.remove(event_path!(HOSTNAME)) {
log_namespace.insert_source_metadata(
JournaldConfig::NAME,
log,
log_schema().host_key().map(LegacyKey::Overwrite),
path!("host"),
host,
);
match log_namespace {
LogNamespace::Vector => {
if let Some(host) = log
.get(metadata_path!(JournaldConfig::NAME, "metadata"))
.and_then(|meta| meta.get(HOSTNAME))
{
log.insert(metadata_path!(JournaldConfig::NAME, "host"), host.clone());
}
}
LogNamespace::Legacy => {
if let Some(host) = log.remove(event_path!(HOSTNAME)) {
log_namespace.insert_source_metadata(
JournaldConfig::NAME,
log,
log_schema().host_key().map(LegacyKey::Overwrite),
path!("host"),
host,
);
}
}
}

// Create a Utc timestamp from an existing log field if present.
let timestamp = log
.get(event_path!(SOURCE_TIMESTAMP))
.or_else(|| log.get(event_path!(RECEIVED_TIMESTAMP)))
let timestamp_value = match log_namespace {
LogNamespace::Vector => log
.get(metadata_path!(JournaldConfig::NAME, "metadata"))
.and_then(|meta| {
meta.get(SOURCE_TIMESTAMP)
.or_else(|| meta.get(RECEIVED_TIMESTAMP))
}),
LogNamespace::Legacy => log
.get(event_path!(SOURCE_TIMESTAMP))
.or_else(|| log.get(event_path!(RECEIVED_TIMESTAMP))),
};

let timestamp = timestamp_value
.filter(|&ts| ts.is_bytes())
.and_then(|ts| {
String::from_utf8_lossy(ts.as_bytes().unwrap())
Expand Down

0 comments on commit 0e6cf3e

Please sign in to comment.