Skip to content

Commit b788bce

Browse files
author
Dominik Rosiek
committed
feat(journalctl): allow to configure units and matches together
Signed-off-by: Dominik Rosiek <[email protected]>
1 parent 3c89455 commit b788bce

File tree

4 files changed

+87
-17
lines changed

4 files changed

+87
-17
lines changed

pkg/stanza/docs/operators/journald_input.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ The `journald_input` operator will use the `__REALTIME_TIMESTAMP` field of the j
1414
| `output` | Next in pipeline | The connected operator(s) that will receive all outbound entries. |
1515
| `directory` | | A directory containing journal files to read entries from. |
1616
| `files` | | A list of journal files to read entries from. |
17-
| `units` | | A list of units to read entries from. This option cannot be used together with `matches`. |
18-
| `matches` | | A list of matches to read entries from. This option cannot be used together with `units`. See [Matches](#matches) example. |
19-
| `priority` | `info` | Filter output by message priorities or priority ranges. |
17+
| `units` | | A list of units to read entries from. See [Multiple filtering options](#multiple-filtering-options) examples, if you want to use it together with `matches` and/or `priority`. |
18+
| `matches` | | A list of matches to read entries from. See [Matches](#matches) and [Multiple filtering options](#multiple-filtering-options) examples. |
19+
| `priority` | `info` | Filter output by message priorities or priority ranges. See [Multiple filtering options](#multiple-filtering-options) examples, if you want to use it together with `units` and/or `matches`. |
2020
| `start_at` | `end` | At startup, where to start reading logs from the file. Options are `beginning` or `end`. |
2121
| `attributes` | {} | A map of `key: value` pairs to add to the entry's attributes. |
2222
| `resource` | {} | A map of `key: value` pairs to add to the entry's resource. |
@@ -48,12 +48,51 @@ The following configuration:
4848
_UID: "1000"
4949
```
5050
51-
will be passed to `journald` as the following arguments: `journald ... _SYSTEMD_UNIT=ssh + _SYSTEMD_UNIT=kubelet _UID=1000`,
51+
will be passed to `journalctl` as the following arguments: `journalctl ... _SYSTEMD_UNIT=ssh + _SYSTEMD_UNIT=kubelet _UID=1000`,
5252
which is going to retrieve all entries which match at least one of the following rules:
5353

5454
- `_SYSTEMD_UNIT` is `ssh`
5555
- `_SYSTEMD_UNIT` is `kubelet` and `_UID` is `1000`
5656

57+
#### Multiple filtering options
58+
59+
In case of using multiple following options, conditions between them are logically `AND`ed and within them are logically `OR`ed:
60+
61+
```text
62+
( priority )
63+
AND
64+
( units[0] OR units[1] OR units[2] OR ... units[U] )
65+
AND
66+
( matches[0] OR matches[1] OR matches[2] OR ... matches[M] )
67+
```
68+
69+
Consider the following example:
70+
71+
```yaml
72+
- type: journald_input
73+
matches:
74+
- _SYSTEMD_UNIT: ssh
75+
- _SYSTEMD_UNIT: kubelet
76+
_UID: "1000"
77+
units:
78+
- kubelet
79+
- systemd
80+
priority: info
81+
```
82+
83+
The above configuration will be passed to `journalctl` as the following arguments
84+
`journalctl ... --priority=info --unit=kubelet --unit=systemd _SYSTEMD_UNIT=ssh + _SYSTEMD_UNIT=kubelet _UID=1000`,
85+
which is going to effectively retrieve all entries which matches the following set of rules:
86+
87+
- `_PRIORITY` is `6`, and
88+
- `_SYSTEMD_UNIT` is `kubelet` or `systemd`, and
89+
- entry matches at least one of the following rules:
90+
91+
- `_SYSTEMD_UNIT` is `ssh`
92+
- `_SYSTEMD_UNIT` is `kubelet` and `_UID` is `1000`
93+
94+
Note, that if you use some fields which aren't associated with an entry, the entry will always be filtered out.
95+
5796
### Simple journald input
5897

5998
Configuration:

pkg/stanza/operator/input/journald/journald.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,6 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
9898
}
9999

100100
func (c Config) buildArgs() ([]string, error) {
101-
// validate arguments
102-
if len(c.Units) > 0 && len(c.Matches) > 0 {
103-
return nil, fmt.Errorf("cannot use both 'matches' and 'units' configurations together")
104-
}
105-
106101
args := make([]string, 0, 10)
107102

108103
// Export logs in UTC time

pkg/stanza/operator/input/journald/journald_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ func TestBuildConfig(t *testing.T) {
171171
cfg.Units = []string{"ssh"}
172172
cfg.Matches = []MatchConfig{
173173
{
174-
"-SYSTEMD_UNIT": "dbus.service",
174+
"_SYSTEMD_UNIT": "dbus.service",
175175
},
176176
}
177177
},
178-
ExpectedError: "cannot use both 'matches' and 'units' configurations together",
178+
Expected: []string{"--utc", "--output=json", "--follow", "--unit", "ssh", "--priority", "info", "_SYSTEMD_UNIT=dbus.service"},
179179
},
180180
}
181181

receiver/journaldreceiver/README.md

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ Journald receiver is dependent on `journalctl` binary to be present and must be
1616
| `directory` | `/run/log/journal` or `/run/journal` | A directory containing journal files to read entries from |
1717
| `files` | | A list of journal files to read entries from |
1818
| `start_at` | `end` | At startup, where to start reading logs from the file. Options are beginning or end |
19-
| `units` | | A list of units to read entries from. This option cannot be used together with `matches` |
20-
| `matches` | | A list of matches to read entries from. This option cannot be used together with `units`. See [Matches](#matches) example |
21-
| `priority` | `info` | Filter output by message priorities or priority ranges |
19+
| `units` | | A list of units to read entries from. See [Multiple filtering options](#multiple-filtering-options) examples, if you want to use it together with `matches` and/or `priority`. |
20+
| `matches` | | A list of matches to read entries from. See [Matches](#matches) and [Multiple filtering options](#multiple-filtering-options) examples. |
21+
| `priority` | `info` | Filter output by message priorities or priority ranges. See [Multiple filtering options](#multiple-filtering-options) examples, if you want to use it together with `units` and/or `matches`. |
2222
| `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. |
2323

2424
### Example Configurations
@@ -47,11 +47,47 @@ The following configuration:
4747
_UID: "1000"
4848
```
4949
50-
will be passed to `journald` as the following arguments: `journald ... _SYSTEMD_UNIT=ssh + _SYSTEMD_UNIT=kubelet _UID=1000`,
50+
will be passed to `journalctl` as the following arguments: `journalctl ... _SYSTEMD_UNIT=ssh + _SYSTEMD_UNIT=kubelet _UID=1000`,
5151
which is going to retrieve all entries which match at least one of the following rules:
5252

5353
- `_SYSTEMD_UNIT` is `ssh`
5454
- `_SYSTEMD_UNIT` is `kubelet` and `_UID` is `1000`
5555

56-
[alpha]: https://github.com/open-telemetry/opentelemetry-collector#alpha
57-
[contrib]: https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib
56+
#### Multiple filtering options
57+
58+
In case of using multiple following options, conditions between them are logically `AND`ed and within them are logically `OR`ed:
59+
60+
```text
61+
( priority )
62+
AND
63+
( units[0] OR units[1] OR units[2] OR ... units[U] )
64+
AND
65+
( matches[0] OR matches[1] OR matches[2] OR ... matches[M] )
66+
```
67+
68+
Consider the following example:
69+
70+
```yaml
71+
- type: journald_input
72+
matches:
73+
- _SYSTEMD_UNIT: ssh
74+
- _SYSTEMD_UNIT: kubelet
75+
_UID: "1000"
76+
units:
77+
- kubelet
78+
- systemd
79+
priority: info
80+
```
81+
82+
The above configuration will be passed to `journalctl` as the following arguments
83+
`journalctl ... --priority=info --unit=kubelet --unit=systemd _SYSTEMD_UNIT=ssh + _SYSTEMD_UNIT=kubelet _UID=1000`,
84+
which is going to effectively retrieve all entries which matches the following set of rules:
85+
86+
- `_PRIORITY` is `6`, and
87+
- `_SYSTEMD_UNIT` is `kubelet` or `systemd`, and
88+
- entry matches at least one of the following rules:
89+
90+
- `_SYSTEMD_UNIT` is `ssh`
91+
- `_SYSTEMD_UNIT` is `kubelet` and `_UID` is `1000`
92+
93+
Note, that if you use some fields which aren't associated with an entry, the entry will always be filtered out.

0 commit comments

Comments
 (0)