Skip to content

Commit

Permalink
feat(journald source): add journal_namespace option (#17648)
Browse files Browse the repository at this point in the history
Closes: #16808

<!--
**Your PR title must conform to the conventional commit spec!**

  <type>(<scope>)!: <description>

  * `type` = chore, enhancement, feat, fix, docs
  * `!` = OPTIONAL: signals a breaking change
* `scope` = Optional when `type` is "chore" or "docs", available scopes
https://github.com/vectordotdev/vector/blob/master/.github/semantic.yml#L20
  * `description` = short description of the change

Examples:

  * enhancement(file source): Add `sort` option to sort discovered files
  * feat(new source): Initial `statsd` source
  * fix(file source): Fix a bug discovering new files
  * chore(external docs): Clarify `batch_size` option
-->
  • Loading branch information
dsmith3197 authored Jun 8, 2023
1 parent 380d7ad commit 7a01fa3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
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

0 comments on commit 7a01fa3

Please sign in to comment.