Skip to content

Commit

Permalink
enhancement (journald source): Add extra_journalctl_args option to …
Browse files Browse the repository at this point in the history
…specify arbitrary command line arguments to journalctl (vectordotdev#18568)

* Add extra_journalctl_args configuration option to journald source.

* Change extra_journalctl_args to vector to allow multiple arguments to be added.

* Rename option to extra_args, update cue file.

* Fix coding style failing on `cargo fmt --check`.

---------

Co-authored-by: a-bali <a-bali@github.com>
  • Loading branch information
a-bali and a-bali committed Sep 25, 2023
1 parent 3c662f3 commit 21aec0c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/sources/journald.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ pub struct JournaldConfig {
#[configurable(metadata(docs::human_name = "Data Directory"))]
pub data_dir: Option<PathBuf>,

/// A list of extra command line arguments to pass to `journalctl`.
///
/// If specified, it is merged to the command line arguments as-is.
#[serde(default)]
#[configurable(metadata(docs::examples = "--merge"))]
pub extra_args: Vec<String>,

/// The systemd journal is read in batches, and a checkpoint is set at the end of each batch.
///
/// This option limits the size of the batch.
Expand Down Expand Up @@ -297,6 +304,7 @@ impl Default for JournaldConfig {
journalctl_path: None,
journal_directory: None,
journal_namespace: None,
extra_args: vec![],
acknowledgements: Default::default(),
remap_priority: false,
log_namespace: None,
Expand Down Expand Up @@ -351,6 +359,7 @@ impl SourceConfig for JournaldConfig {
self.journal_namespace.clone(),
self.current_boot_only,
self.since_now,
self.extra_args.clone(),
);

let batch_size = self.batch_size;
Expand Down Expand Up @@ -621,6 +630,7 @@ struct StartJournalctl {
journal_namespace: Option<String>,
current_boot_only: bool,
since_now: bool,
extra_args: Vec<String>,
}

impl StartJournalctl {
Expand All @@ -630,13 +640,15 @@ impl StartJournalctl {
journal_namespace: Option<String>,
current_boot_only: bool,
since_now: bool,
extra_args: Vec<String>,
) -> Self {
Self {
path,
journal_dir,
journal_namespace,
current_boot_only,
since_now,
extra_args,
}
}

Expand Down Expand Up @@ -669,6 +681,10 @@ impl StartJournalctl {
command.arg("--since=2000-01-01");
}

if !self.extra_args.is_empty() {
command.args(&self.extra_args);
}

command
}

Expand Down Expand Up @@ -1414,6 +1430,7 @@ mod tests {
let current_boot_only = false;
let cursor = None;
let since_now = false;
let extra_args = vec![];

let command = create_command(
&path,
Expand All @@ -1422,6 +1439,7 @@ mod tests {
current_boot_only,
since_now,
cursor,
extra_args,
);
let cmd_line = format!("{:?}", command);
assert!(!cmd_line.contains("--directory="));
Expand All @@ -1432,6 +1450,7 @@ mod tests {
let journal_dir = None;
let journal_namespace = None;
let since_now = true;
let extra_args = vec![];

let command = create_command(
&path,
Expand All @@ -1440,6 +1459,7 @@ mod tests {
current_boot_only,
since_now,
cursor,
extra_args,
);
let cmd_line = format!("{:?}", command);
assert!(cmd_line.contains("--since=now"));
Expand All @@ -1448,6 +1468,7 @@ mod tests {
let journal_namespace = Some(String::from("my_namespace"));
let current_boot_only = true;
let cursor = Some("2021-01-01");
let extra_args = vec!["--merge".to_string()];

let command = create_command(
&path,
Expand All @@ -1456,12 +1477,14 @@ mod tests {
current_boot_only,
since_now,
cursor,
extra_args,
);
let cmd_line = format!("{:?}", command);
assert!(cmd_line.contains("--directory=/tmp/journal-dir"));
assert!(cmd_line.contains("--namespace=my_namespace"));
assert!(cmd_line.contains("--boot"));
assert!(cmd_line.contains("--after-cursor="));
assert!(cmd_line.contains("--merge"));
}

fn create_command(
Expand All @@ -1471,13 +1494,15 @@ mod tests {
current_boot_only: bool,
since_now: bool,
cursor: Option<&str>,
extra_args: Vec<String>,
) -> Command {
StartJournalctl::new(
path.into(),
journal_dir,
journal_namespace,
current_boot_only,
since_now,
extra_args,
)
.make_command(cursor)
}
Expand Down
12 changes: 12 additions & 0 deletions website/cue/reference/components/sources/base/journald.cue
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ base: components: sources: journald: configuration: {
items: type: string: examples: ["badservice", "sysinit.target"]
}
}
extra_args: {
description: """
A list of extra command line arguments to pass to `journalctl`.
If specified, it is merged to the command line arguments as-is.
"""
required: false
type: array: {
default: []
items: type: string: examples: ["--merge"]
}
}
include_matches: {
description: """
A list of sets of field/value pairs to monitor.
Expand Down

0 comments on commit 21aec0c

Please sign in to comment.