Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/stanza] Add regexp based tests & examples for filter operator #31698

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/logline-filtering/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Filtering log messages based on content

Filelog receiver provides support for filtering logs based on their content. This can be achieved by using
the [filter operator](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/pkg/stanza/docs/operators/filter.md),
configured with matching regular expressions.

With this happening at the collection point, a lot of resources at the destination backend
can be saved since no additional processing would need to take place.

A full configuration example on how to filter out logs that start with the `INFO:` pattern is
provided in the [example config](./otel-col-config-filter-out-logs.yaml).
A full configuration example on how to only collect logs that start with the `WARN:` pattern is provided in
the [example config](./otel-col-config-filter-in-logs.yaml)
17 changes: 17 additions & 0 deletions examples/logline-filtering/otel-col-config-filter-in-logs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
receivers:
filelog:
include: [/var/log/busybox/simple.log]
operators:
- type: filter
expr: 'body not matches "^WARN:"'

service:
pipelines:
logs:
receivers: [filelog]
exporters: [otlp/custom]
processors: []

exporters:
otlp/custom:
endpoint: https://0.0.0.0:4242
17 changes: 17 additions & 0 deletions examples/logline-filtering/otel-col-config-filter-out-logs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
receivers:
filelog:
include: [/var/log/busybox/simple.log]
operators:
- type: filter
expr: 'body matches "^INFO:"'

service:
pipelines:
logs:
receivers: [filelog]
exporters: [otlp/custom]
processors: []

exporters:
otlp/custom:
endpoint: https://0.0.0.0:4242
20 changes: 20 additions & 0 deletions pkg/stanza/operator/transformer/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ func TestTransformer(t *testing.T) {
`body.message == "test_message"`,
false,
},
{
"FilterOutRegexp",
&entry.Entry{
Body: map[string]any{
"message": "INFO: this is an info message",
},
},
`body.message matches "^INFO:"`,
true,
},
{
"FilterInRegexp",
&entry.Entry{
Body: map[string]any{
"message": "WARN: this is a warning message",
},
},
`body.message not matches "^WARN:"`,
false,
},
{
"MatchAttribute",
&entry.Entry{
Expand Down
Loading