Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement (journald source): Add extra_journalctl_args option to specify arbitrary command line arguments to journalctl #18568

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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![],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: For future work, vec![] is just Vec::new(), which avoids using a macro.

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
Loading