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

feat(journald source): add journal_namespace option #17648

Merged
merged 1 commit into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
64 changes: 58 additions & 6 deletions src/sources/journald.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ pub struct JournaldConfig {
#[serde(default)]
pub journal_directory: Option<PathBuf>,

/// The [journal namespace][journal-namespace].
///
/// This value is passed to `journalctl` through the [`--namespace` option][journalctl-namespace-option].
/// If not set, `journalctl` uses the default namespace.
///
/// [journal-namespace]: https://www.freedesktop.org/software/systemd/man/systemd-journald.service.html#Journal%20Namespaces
/// [journalctl-namespace-option]: https://www.freedesktop.org/software/systemd/man/journalctl.html#--namespace=NAMESPACE
#[serde(default)]
pub journal_namespace: Option<String>,

#[configurable(derived)]
#[serde(default, deserialize_with = "bool_or_struct")]
acknowledgements: SourceAcknowledgementsConfig,
Expand Down Expand Up @@ -290,6 +300,7 @@ impl Default for JournaldConfig {
batch_size: default_batch_size(),
journalctl_path: None,
journal_directory: None,
journal_namespace: None,
acknowledgements: Default::default(),
remap_priority: false,
log_namespace: None,
Expand Down Expand Up @@ -341,6 +352,7 @@ impl SourceConfig for JournaldConfig {
let starter = StartJournalctl::new(
journalctl_path,
self.journal_directory.clone(),
self.journal_namespace.clone(),
self.current_boot_only,
self.since_now,
);
Expand Down Expand Up @@ -610,6 +622,7 @@ type JournalStream = BoxStream<'static, Result<Bytes, BoxedFramingError>>;
struct StartJournalctl {
path: PathBuf,
journal_dir: Option<PathBuf>,
journal_namespace: Option<String>,
current_boot_only: bool,
since_now: bool,
}
Expand All @@ -618,12 +631,14 @@ impl StartJournalctl {
const fn new(
path: PathBuf,
journal_dir: Option<PathBuf>,
journal_namespace: Option<String>,
current_boot_only: bool,
since_now: bool,
) -> Self {
Self {
path,
journal_dir,
journal_namespace,
current_boot_only,
since_now,
}
Expand All @@ -641,6 +656,10 @@ impl StartJournalctl {
command.arg(format!("--directory={}", dir.display()));
}

if let Some(namespace) = &self.journal_namespace {
command.arg(format!("--namespace={}", namespace));
}

if self.current_boot_only {
command.arg("--boot");
}
Expand Down Expand Up @@ -1400,43 +1419,76 @@ mod tests {
let path = PathBuf::from("journalctl");

let journal_dir = None;
let journal_namespace = None;
let current_boot_only = false;
let cursor = None;
let since_now = false;

let command = create_command(&path, journal_dir, current_boot_only, since_now, cursor);
let command = create_command(
&path,
journal_dir,
journal_namespace,
current_boot_only,
since_now,
cursor,
);
let cmd_line = format!("{:?}", command);
assert!(!cmd_line.contains("--directory="));
assert!(!cmd_line.contains("--namespace="));
assert!(!cmd_line.contains("--boot"));
assert!(cmd_line.contains("--since=2000-01-01"));

let since_now = true;
let journal_dir = None;
let journal_namespace = None;
let since_now = true;

let command = create_command(&path, journal_dir, current_boot_only, since_now, cursor);
let command = create_command(
&path,
journal_dir,
journal_namespace,
current_boot_only,
since_now,
cursor,
);
let cmd_line = format!("{:?}", command);
assert!(cmd_line.contains("--since=now"));

let journal_dir = Some(PathBuf::from("/tmp/journal-dir"));
let journal_namespace = Some(String::from("my_namespace"));
let current_boot_only = true;
let cursor = Some("2021-01-01");

let command = create_command(&path, journal_dir, current_boot_only, since_now, cursor);
let command = create_command(
&path,
journal_dir,
journal_namespace,
current_boot_only,
since_now,
cursor,
);
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="));
}

fn create_command(
path: &Path,
journal_dir: Option<PathBuf>,
journal_namespace: Option<String>,
current_boot_only: bool,
since_now: bool,
cursor: Option<&str>,
) -> Command {
StartJournalctl::new(path.into(), journal_dir, current_boot_only, since_now)
.make_command(cursor)
StartJournalctl::new(
path.into(),
journal_dir,
journal_namespace,
current_boot_only,
since_now,
)
.make_command(cursor)
}

fn message(event: &Event) -> Value {
Expand Down
13 changes: 13 additions & 0 deletions website/cue/reference/components/sources/base/journald.cue
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ base: components: sources: journald: configuration: {
required: false
type: string: {}
}
journal_namespace: {
description: """
The [journal namespace][journal-namespace].

This value is passed to `journalctl` through the [`--namespace` option][journalctl-namespace-option].
If not set, `journalctl` uses the default namespace.

[journal-namespace]: https://www.freedesktop.org/software/systemd/man/systemd-journald.service.html#Journal%20Namespaces
[journalctl-namespace-option]: https://www.freedesktop.org/software/systemd/man/journalctl.html#--namespace=NAMESPACE
"""
required: false
type: string: {}
}
journalctl_path: {
description: """
The full path of the `journalctl` executable.
Expand Down