Skip to content

Commit a324a07

Browse files
authored
feat(journald source): add journal_namespace option (#17648)
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 -->
1 parent 380d7ad commit a324a07

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

src/sources/journald.rs

+58-6
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ pub struct JournaldConfig {
174174
#[serde(default)]
175175
pub journal_directory: Option<PathBuf>,
176176

177+
/// The [journal namespace][journal-namespace].
178+
///
179+
/// This value is passed to `journalctl` through the [`--namespace` option][journalctl-namespace-option].
180+
/// If not set, `journalctl` uses the default namespace.
181+
///
182+
/// [journal-namespace]: https://www.freedesktop.org/software/systemd/man/systemd-journald.service.html#Journal%20Namespaces
183+
/// [journalctl-namespace-option]: https://www.freedesktop.org/software/systemd/man/journalctl.html#--namespace=NAMESPACE
184+
#[serde(default)]
185+
pub journal_namespace: Option<String>,
186+
177187
#[configurable(derived)]
178188
#[serde(default, deserialize_with = "bool_or_struct")]
179189
acknowledgements: SourceAcknowledgementsConfig,
@@ -290,6 +300,7 @@ impl Default for JournaldConfig {
290300
batch_size: default_batch_size(),
291301
journalctl_path: None,
292302
journal_directory: None,
303+
journal_namespace: None,
293304
acknowledgements: Default::default(),
294305
remap_priority: false,
295306
log_namespace: None,
@@ -341,6 +352,7 @@ impl SourceConfig for JournaldConfig {
341352
let starter = StartJournalctl::new(
342353
journalctl_path,
343354
self.journal_directory.clone(),
355+
self.journal_namespace.clone(),
344356
self.current_boot_only,
345357
self.since_now,
346358
);
@@ -610,6 +622,7 @@ type JournalStream = BoxStream<'static, Result<Bytes, BoxedFramingError>>;
610622
struct StartJournalctl {
611623
path: PathBuf,
612624
journal_dir: Option<PathBuf>,
625+
journal_namespace: Option<String>,
613626
current_boot_only: bool,
614627
since_now: bool,
615628
}
@@ -618,12 +631,14 @@ impl StartJournalctl {
618631
const fn new(
619632
path: PathBuf,
620633
journal_dir: Option<PathBuf>,
634+
journal_namespace: Option<String>,
621635
current_boot_only: bool,
622636
since_now: bool,
623637
) -> Self {
624638
Self {
625639
path,
626640
journal_dir,
641+
journal_namespace,
627642
current_boot_only,
628643
since_now,
629644
}
@@ -641,6 +656,10 @@ impl StartJournalctl {
641656
command.arg(format!("--directory={}", dir.display()));
642657
}
643658

659+
if let Some(namespace) = &self.journal_namespace {
660+
command.arg(format!("--namespace={}", namespace));
661+
}
662+
644663
if self.current_boot_only {
645664
command.arg("--boot");
646665
}
@@ -1400,43 +1419,76 @@ mod tests {
14001419
let path = PathBuf::from("journalctl");
14011420

14021421
let journal_dir = None;
1422+
let journal_namespace = None;
14031423
let current_boot_only = false;
14041424
let cursor = None;
14051425
let since_now = false;
14061426

1407-
let command = create_command(&path, journal_dir, current_boot_only, since_now, cursor);
1427+
let command = create_command(
1428+
&path,
1429+
journal_dir,
1430+
journal_namespace,
1431+
current_boot_only,
1432+
since_now,
1433+
cursor,
1434+
);
14081435
let cmd_line = format!("{:?}", command);
14091436
assert!(!cmd_line.contains("--directory="));
1437+
assert!(!cmd_line.contains("--namespace="));
14101438
assert!(!cmd_line.contains("--boot"));
14111439
assert!(cmd_line.contains("--since=2000-01-01"));
14121440

1413-
let since_now = true;
14141441
let journal_dir = None;
1442+
let journal_namespace = None;
1443+
let since_now = true;
14151444

1416-
let command = create_command(&path, journal_dir, current_boot_only, since_now, cursor);
1445+
let command = create_command(
1446+
&path,
1447+
journal_dir,
1448+
journal_namespace,
1449+
current_boot_only,
1450+
since_now,
1451+
cursor,
1452+
);
14171453
let cmd_line = format!("{:?}", command);
14181454
assert!(cmd_line.contains("--since=now"));
14191455

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

1424-
let command = create_command(&path, journal_dir, current_boot_only, since_now, cursor);
1461+
let command = create_command(
1462+
&path,
1463+
journal_dir,
1464+
journal_namespace,
1465+
current_boot_only,
1466+
since_now,
1467+
cursor,
1468+
);
14251469
let cmd_line = format!("{:?}", command);
14261470
assert!(cmd_line.contains("--directory=/tmp/journal-dir"));
1471+
assert!(cmd_line.contains("--namespace=my_namespace"));
14271472
assert!(cmd_line.contains("--boot"));
14281473
assert!(cmd_line.contains("--after-cursor="));
14291474
}
14301475

14311476
fn create_command(
14321477
path: &Path,
14331478
journal_dir: Option<PathBuf>,
1479+
journal_namespace: Option<String>,
14341480
current_boot_only: bool,
14351481
since_now: bool,
14361482
cursor: Option<&str>,
14371483
) -> Command {
1438-
StartJournalctl::new(path.into(), journal_dir, current_boot_only, since_now)
1439-
.make_command(cursor)
1484+
StartJournalctl::new(
1485+
path.into(),
1486+
journal_dir,
1487+
journal_namespace,
1488+
current_boot_only,
1489+
since_now,
1490+
)
1491+
.make_command(cursor)
14401492
}
14411493

14421494
fn message(event: &Event) -> Value {

website/cue/reference/components/sources/base/journald.cue

+13
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ base: components: sources: journald: configuration: {
126126
required: false
127127
type: string: {}
128128
}
129+
journal_namespace: {
130+
description: """
131+
The [journal namespace][journal-namespace].
132+
133+
This value is passed to `journalctl` through the [`--namespace` option][journalctl-namespace-option].
134+
If not set, `journalctl` uses the default namespace.
135+
136+
[journal-namespace]: https://www.freedesktop.org/software/systemd/man/systemd-journald.service.html#Journal%20Namespaces
137+
[journalctl-namespace-option]: https://www.freedesktop.org/software/systemd/man/journalctl.html#--namespace=NAMESPACE
138+
"""
139+
required: false
140+
type: string: {}
141+
}
129142
journalctl_path: {
130143
description: """
131144
The full path of the `journalctl` executable.

0 commit comments

Comments
 (0)