From 426e0a4e40a02a6eb1931e007908d1aa1bfff26c Mon Sep 17 00:00:00 2001 From: fuero <108815+fuero@users.noreply.github.com> Date: Tue, 29 Oct 2024 19:56:31 +0100 Subject: [PATCH] [receiver/journald] adds namespace support (#36031) #### Description Adds support for querying journald namespaces See https://www.freedesktop.org/software/systemd/man/latest/systemd-journald.service.html#Journal%20Namespaces for details #### Testing Adds tests for correctly generating the `journalctl` command. Tested on a real world example as well with the resulting binary #### Documentation Adds description of the setting the PR introduces --- .chloggen/36031-add-namespace-support.yaml | 27 +++++++++++++++++++ .../operator/input/journald/config_all.go | 1 + .../operator/input/journald/config_linux.go | 4 +++ .../operator/input/journald/input_test.go | 7 +++++ receiver/journaldreceiver/README.md | 1 + 5 files changed, 40 insertions(+) create mode 100644 .chloggen/36031-add-namespace-support.yaml diff --git a/.chloggen/36031-add-namespace-support.yaml b/.chloggen/36031-add-namespace-support.yaml new file mode 100644 index 000000000000..8875adeae59b --- /dev/null +++ b/.chloggen/36031-add-namespace-support.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: 'enhancement' + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: journaldreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: allows querying a journald namespace + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [36031] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] diff --git a/pkg/stanza/operator/input/journald/config_all.go b/pkg/stanza/operator/input/journald/config_all.go index e7af99b14f24..7e579a5a4d87 100644 --- a/pkg/stanza/operator/input/journald/config_all.go +++ b/pkg/stanza/operator/input/journald/config_all.go @@ -37,6 +37,7 @@ type Config struct { Grep string `mapstructure:"grep,omitempty"` Dmesg bool `mapstructure:"dmesg,omitempty"` All bool `mapstructure:"all,omitempty"` + Namespace string `mapstructure:"namespace,omitempty"` } type MatchConfig map[string]string diff --git a/pkg/stanza/operator/input/journald/config_linux.go b/pkg/stanza/operator/input/journald/config_linux.go index 9b183e5162ca..489a62111514 100644 --- a/pkg/stanza/operator/input/journald/config_linux.go +++ b/pkg/stanza/operator/input/journald/config_linux.go @@ -90,6 +90,10 @@ func (c Config) buildArgs() ([]string, error) { args = append(args, "--dmesg") } + if len(c.Namespace) > 0 { + args = append(args, "--namespace", c.Namespace) + } + switch { case c.Directory != nil: args = append(args, "--directory", *c.Directory) diff --git a/pkg/stanza/operator/input/journald/input_test.go b/pkg/stanza/operator/input/journald/input_test.go index fd3d00ba0f2d..ae8f4548d6d6 100644 --- a/pkg/stanza/operator/input/journald/input_test.go +++ b/pkg/stanza/operator/input/journald/input_test.go @@ -200,6 +200,13 @@ func TestBuildConfig(t *testing.T) { }, Expected: []string{"--utc", "--output=json", "--follow", "--priority", "info", "--grep", "test_grep"}, }, + { + Name: "namespace", + Config: func(cfg *Config) { + cfg.Namespace = "foo" + }, + Expected: []string{"--utc", "--output=json", "--follow", "--priority", "info", "--namespace", "foo"}, + }, { Name: "dmesg", Config: func(cfg *Config) { diff --git a/receiver/journaldreceiver/README.md b/receiver/journaldreceiver/README.md index 32bde83ceb42..6f3c3329cbb2 100644 --- a/receiver/journaldreceiver/README.md +++ b/receiver/journaldreceiver/README.md @@ -35,6 +35,7 @@ Journald receiver requires that: | `dmesg` | 'false' | Show only kernel messages. This shows logs from current boot and adds the match `_TRANSPORT=kernel`. See [Multiple filtering options](#multiple-filtering-options) examples. | | `storage` | none | The ID of a storage extension to be used to store cursors. Cursors allow the receiver to pick up where it left off in the case of a collector restart. If no storage extension is used, the receiver will manage cursors in memory only. | | `all` | 'false' | If `true`, very long logs and logs with unprintable characters will also be included. | +| `namespace` | | Will query the given namespace. See man page [`systemd-journald.service(8)`](https://www.man7.org/linux/man-pages/man8/systemd-journald.service.8.html#JOURNAL_NAMESPACES) for details. | | `retry_on_failure.enabled` | `false` | If `true`, the receiver will pause reading a file and attempt to resend the current batch of logs if it encounters an error from downstream components. | | `retry_on_failure.initial_interval` | `1 second` | Time to wait after the first failure before retrying. | | `retry_on_failure.max_interval` | `30 seconds` | Upper bound on retry backoff interval. Once this value is reached the delay between consecutive retries will remain constant at the specified value. |