Skip to content

Commit

Permalink
[receiver/journald]: add support for identifiers (#25911)
Browse files Browse the repository at this point in the history
Adds support for `identifiers` to journaldreceiver
  • Loading branch information
sumo-drosiek committed Aug 21, 2023
1 parent 56c4ad9 commit 30348ee
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
27 changes: 27 additions & 0 deletions .chloggen/drosiek-journald-identifier.yaml
Original file line number Diff line number Diff line change
@@ -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: add support for identifiers

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [20295]

# (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: []
21 changes: 13 additions & 8 deletions pkg/stanza/operator/input/journald/journald.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ func NewConfigWithID(operatorID string) *Config {
type Config struct {
helper.InputConfig `mapstructure:",squash"`

Directory *string `mapstructure:"directory,omitempty"`
Files []string `mapstructure:"files,omitempty"`
StartAt string `mapstructure:"start_at,omitempty"`
Units []string `mapstructure:"units,omitempty"`
Priority string `mapstructure:"priority,omitempty"`
Matches []MatchConfig `mapstructure:"matches,omitempty"`
Grep string `mapstructure:"grep,omitempty"`
Dmesg bool `mapstructure:"dmesg,omitempty"`
Directory *string `mapstructure:"directory,omitempty"`
Files []string `mapstructure:"files,omitempty"`
StartAt string `mapstructure:"start_at,omitempty"`
Units []string `mapstructure:"units,omitempty"`
Priority string `mapstructure:"priority,omitempty"`
Matches []MatchConfig `mapstructure:"matches,omitempty"`
Identifiers []string `mapstructure:"identifiers,omitempty"`
Grep string `mapstructure:"grep,omitempty"`
Dmesg bool `mapstructure:"dmesg,omitempty"`
}

type MatchConfig map[string]string
Expand Down Expand Up @@ -114,6 +115,10 @@ func (c Config) buildArgs() ([]string, error) {
args = append(args, "--unit", unit)
}

for _, identifier := range c.Identifiers {
args = append(args, "--identifier", identifier)
}

args = append(args, "--priority", c.Priority)

if len(c.Grep) > 0 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/stanza/operator/input/journald/journald_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ func TestBuildConfig(t *testing.T) {
},
Expected: []string{"--utc", "--output=json", "--follow", "--unit", "ssh", "--priority", "info", "_SYSTEMD_UNIT=dbus.service"},
},
{
Name: "identifiers",
Config: func(cfg *Config) {
cfg.Identifiers = []string{"wireplumber", "systemd"}
},
Expected: []string{"--utc", "--output=json", "--follow", "--identifier", "wireplumber", "--identifier", "systemd", "--priority", "info"},
},
{
Name: "grep",
Config: func(cfg *Config) {
Expand Down
8 changes: 7 additions & 1 deletion receiver/journaldreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Journald receiver requires that:
| `files` | | A list of journal files to read entries from |
| `start_at` | `end` | At startup, where to start reading logs from the file. Options are beginning or end |
| `units` | | A list of units to read entries from. See [Multiple filtering options](#multiple-filtering-options) examples. |
| `identifiers` | | Filter output by message identifiers (`SYSTEMD_IDENTIFIER`). See [Multiple filtering options](#multiple-filtering-options) examples. |
| `matches` | | A list of matches to read entries from. See [Matches](#matches) and [Multiple filtering options](#multiple-filtering-options) examples. |
| `priority` | `info` | Filter output by message priorities or priority ranges. See [Multiple filtering options](#multiple-filtering-options) examples. |
| `grep` | | Filter output to entries where the MESSAGE= field matches the specified regular expression. See [Multiple filtering options](#multiple-filtering-options) examples. |
Expand Down Expand Up @@ -92,6 +93,8 @@ AND
AND
( units[0] OR units[1] OR units[2] OR ... units[U] )
AND
( identifier[0] OR identifier[1] OR identifier[2] OR ... identifier[I] )
AND
( matches[0] OR matches[1] OR matches[2] OR ... matches[M] )
AND
( grep )
Expand All @@ -109,14 +112,17 @@ Consider the following example:
- kubelet
- systemd
priority: info
identifiers:
- systemd
```

The above configuration will be passed to `journalctl` as the following arguments
`journalctl ... --priority=info --unit=kubelet --unit=systemd _SYSTEMD_UNIT=ssh + _SYSTEMD_UNIT=kubelet _UID=1000`,
`journalctl ... --priority=info --unit=kubelet --unit=systemd --identifier=systemd _SYSTEMD_UNIT=ssh + _SYSTEMD_UNIT=kubelet _UID=1000`,
which is going to effectively retrieve all entries which matches the following set of rules:

- `_PRIORITY` is `6`, and
- `_SYSTEMD_UNIT` is `kubelet` or `systemd`, and
- `SYSLOG_IDENTIFIER` `systemd`, and
- entry matches at least one of the following rules:

- `_SYSTEMD_UNIT` is `ssh`
Expand Down

0 comments on commit 30348ee

Please sign in to comment.