-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[libbeat]: Add lowercase processor #41424
Merged
Merged
Changes from all commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
83e75be
[libbeat] Add lowercase_fields and uppercase_fields processors
davidifr d63677c
[libbeat]: Add lowercase_key processor
khushijain21 12437a2
only loweercase
khushijain21 5f44cef
changelog
khushijain21 459c3c3
addressed comments minus documentation
khushijain21 d5bf4fc
addressed comments & documentation
khushijain21 c81af0e
Update libbeat/processors/actions/docs/lowercase.asciidoc
khushijain21 344decc
Update libbeat/processors/actions/alterFieldProcessor.go
khushijain21 c400052
Update libbeat/processors/actions/alterFieldProcessor.go
khushijain21 173feeb
Update libbeat/processors/actions/alterFieldProcessor.go
khushijain21 8b57c05
Update libbeat/processors/actions/docs/lowercase.asciidoc
khushijain21 db2141f
Update libbeat/processors/actions/lowercase.go
khushijain21 2826596
Update libbeat/processors/actions/alterFieldProcessor.go
khushijain21 1779aea
Update libbeat/processors/actions/alterFieldProcessor.go
khushijain21 9b76e1d
Update libbeat/processors/actions/alterFieldProcessor.go
khushijain21 7242069
Update libbeat/processors/actions/lowercase.go
khushijain21 bbcca15
Update libbeat/processors/actions/docs/lowercase.asciidoc
khushijain21 63b3e1c
Update libbeat/processors/actions/docs/lowercase.asciidoc
khushijain21 d7d0101
case insensitive search
khushijain21 9a97f8f
Update libbeat/processors/actions/docs/lowercase.asciidoc
khushijain21 1017841
Update libbeat/processors/actions/docs/lowercase.asciidoc
khushijain21 c9efe9f
Update libbeat/processors/actions/docs/lowercase.asciidoc
khushijain21 991fcb2
Update libbeat/processors/actions/docs/lowercase.asciidoc
khushijain21 e7efcfe
Update libbeat/processors/actions/docs/lowercase.asciidoc
khushijain21 c043d92
case insensitive search
khushijain21 3adafd9
using flattenkeys
khushijain21 d5bb79e
changelog
khushijain21 f3ac575
cosmetic changes
khushijain21 20fbb94
lowercase with flattenKeys
khushijain21 38deda3
lowercase with foldvalue
khushijain21 f5adc68
Update CHANGELOG.next.asciidoc
khushijain21 ec55fe1
resolved conflicts
khushijain21 a757787
added license
khushijain21 1cebb60
full_path search
khushijain21 ddf7d69
cosmetic changes
khushijain21 7aafb02
using alterpath
khushijain21 c5f7322
update version
khushijain21 780cb54
refactor
khushijain21 9e05d53
Merge branch 'main' into issue-22254
khushijain21 e4fc2dd
linter issues
khushijain21 f0a9064
fix
khushijain21 193e179
fix
khushijain21 38722db
Update libbeat/processors/actions/docs/lowercase.asciidoc
khushijain21 959710b
Update libbeat/processors/actions/docs/lowercase.asciidoc
khushijain21 8bc524f
Update libbeat/processors/actions/alterFieldProcessor.go
khushijain21 eb0ec6a
Update libbeat/processors/actions/docs/lowercase.asciidoc
khushijain21 68875e4
Update libbeat/processors/actions/docs/lowercase.asciidoc
khushijain21 4640ae3
skip mandatory fields with test
khushijain21 7b3bcd4
skip mandatory fields
khushijain21 93ca71f
additonal logic to skipping mandatory field
khushijain21 d2dc946
optimization
khushijain21 1e91271
fix test case
khushijain21 71fa1bb
fix flaky test
khushijain21 b3addc5
Merge branch 'main' into issue-22254
khushijain21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package actions | ||
khushijain21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/elastic/beats/v7/libbeat/beat" | ||
"github.com/elastic/beats/v7/libbeat/processors" | ||
conf "github.com/elastic/elastic-agent-libs/config" | ||
"github.com/elastic/elastic-agent-libs/mapstr" | ||
) | ||
|
||
type alterFieldProcessor struct { | ||
Fields []string | ||
IgnoreMissing bool | ||
FailOnError bool | ||
AlterFullField bool | ||
|
||
processorName string | ||
alterFunc mapstr.AlterFunc | ||
} | ||
|
||
// NewAlterFieldProcessor is an umbrella method for processing events based on provided fields. Such as converting event keys to uppercase/lowercase | ||
func NewAlterFieldProcessor(c *conf.C, processorName string, alterFunc mapstr.AlterFunc) (beat.Processor, error) { | ||
config := struct { | ||
Fields []string `config:"fields"` | ||
IgnoreMissing bool `config:"ignore_missing"` | ||
FailOnError bool `config:"fail_on_error"` | ||
AlterFullField bool `config:"alter_full_field"` | ||
}{ | ||
IgnoreMissing: false, | ||
FailOnError: true, | ||
AlterFullField: true, | ||
} | ||
|
||
if err := c.Unpack(&config); err != nil { | ||
return nil, fmt.Errorf("failed to unpack the %s fields configuration: %w", processorName, err) | ||
} | ||
|
||
// Skip mandatory fields | ||
var configFields []string | ||
var lowerField string | ||
for _, readOnly := range processors.MandatoryExportedFields { | ||
readOnly = strings.ToLower(readOnly) | ||
for _, field := range config.Fields { | ||
// Skip fields that match "readOnly" or start with "readOnly." | ||
lowerField = strings.ToLower(field) | ||
if strings.HasPrefix(lowerField, readOnly+".") || lowerField == readOnly { | ||
continue | ||
} | ||
// Add fields that do not match "readOnly" criteria | ||
configFields = append(configFields, field) | ||
} | ||
} | ||
return &alterFieldProcessor{ | ||
Fields: configFields, | ||
IgnoreMissing: config.IgnoreMissing, | ||
FailOnError: config.FailOnError, | ||
processorName: processorName, | ||
AlterFullField: config.AlterFullField, | ||
alterFunc: alterFunc, | ||
}, nil | ||
|
||
} | ||
|
||
func (a *alterFieldProcessor) String() string { | ||
return fmt.Sprintf("%s fields=%+v", a.processorName, *a) | ||
} | ||
|
||
func (a *alterFieldProcessor) Run(event *beat.Event) (*beat.Event, error) { | ||
var backup *beat.Event | ||
if a.FailOnError { | ||
backup = event.Clone() | ||
} | ||
|
||
for _, field := range a.Fields { | ||
err := a.alter(event, field) | ||
if err != nil { | ||
if a.IgnoreMissing && errors.Is(err, mapstr.ErrKeyNotFound) { | ||
continue | ||
} | ||
if a.FailOnError { | ||
event = backup | ||
_, _ = event.PutValue("error.message", err.Error()) | ||
return event, err | ||
} | ||
} | ||
} | ||
|
||
return event, nil | ||
} | ||
|
||
func (a *alterFieldProcessor) alter(event *beat.Event, field string) error { | ||
|
||
// modify all segments of the key | ||
if a.AlterFullField { | ||
err := event.Fields.AlterPath(field, mapstr.CaseInsensitiveMode, a.alterFunc) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
// modify only the last segment | ||
segmentCount := strings.Count(field, ".") | ||
err := event.Fields.AlterPath(field, mapstr.CaseInsensitiveMode, func(key string) (string, error) { | ||
if segmentCount > 0 { | ||
segmentCount-- | ||
return key, nil | ||
} | ||
return a.alterFunc(key) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
[[lowercase]] | ||
=== Lowercase fields in events | ||
|
||
++++ | ||
<titleabbrev>lowercase</titleabbrev> | ||
++++ | ||
|
||
The `lowercase` processor specifies a list of fields that should be converted to lowercase. This transformation applies to keys that match the specified fields. Matching is performed case-insensitively. | ||
|
||
|
||
==== Examples: | ||
|
||
1. Default scenario | ||
|
||
[source,yaml] | ||
---- | ||
processors: | ||
- rename: | ||
fields: | ||
- "ab.cd" | ||
ignore_missing: false | ||
fail_on_error: true | ||
full_path: true | ||
---- | ||
[source,json] | ||
---- | ||
// Input | ||
{ | ||
"AB": {"CD":"data"}, | ||
"CD": {"ef":"data"} | ||
} | ||
|
||
|
||
// output | ||
{ | ||
"ab": {"cd":"data"}, // `AB.CD` -> `ab.cd` | ||
"CD": {"ef":"data"} | ||
} | ||
---- | ||
|
||
[start=2] | ||
2. When `full_path` is false | ||
|
||
[source,yaml] | ||
---- | ||
processors: | ||
- rename: | ||
fields: | ||
- "ab.cd" | ||
ignore_missing: false | ||
fail_on_error: true | ||
alter_full_field: false | ||
---- | ||
|
||
[source,json] | ||
---- | ||
// Input | ||
{ | ||
"AB": {"CD":"data"}, | ||
"CD": {"ef":"data"} | ||
} | ||
|
||
|
||
// output | ||
{ | ||
"AB": {"cd":"data"}, // `AB.CD` -> `AB.cd` (only `cd` is lowercased) | ||
"CD": {"ef":"data"} | ||
} | ||
---- | ||
|
||
[start=2] | ||
2. In case of non unique path to the key | ||
|
||
[source,yaml] | ||
---- | ||
processors: | ||
- rename: | ||
fields: | ||
- "ab" | ||
ignore_missing: false | ||
fail_on_error: true | ||
alter_full_field: true | ||
---- | ||
|
||
[source,json] | ||
---- | ||
// Input | ||
{ | ||
"ab": "first", | ||
"aB": "second" | ||
} | ||
|
||
// Output | ||
{ | ||
"ab": "first", | ||
"aB": "second", | ||
"err": "... Error: key collision" | ||
} | ||
---- | ||
|
||
==== Configuration: | ||
|
||
The `lowercase` processor has the following configuration settings: | ||
|
||
`fields`:: The field names to lowercase. The match is case-insensitive, e.g. `a.b.c.d` would match `A.b.C.d` or `A.B.C.D`. | ||
`ignore_missing`:: (Optional) Indicates whether to ignore events that lack the source field. | ||
The default is `false`, which will fail processing of an event if a field is missing. | ||
`fail_on_error`:: (Optional) If set to `true` and an error occurs, the changes are reverted and the original event is returned. | ||
If set to `false`, processing continues if an error occurs. Default is `true`. | ||
`alter_full_field`:: (Optional) If set to `true`, the entire key path is lowercased. If set to `false` only the final part of the key path is lowercased. Default is true | ||
|
||
|
||
|
||
See <<conditions>> for a list of supported conditions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package actions | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/elastic/beats/v7/libbeat/beat" | ||
"github.com/elastic/beats/v7/libbeat/processors" | ||
"github.com/elastic/beats/v7/libbeat/processors/checks" | ||
conf "github.com/elastic/elastic-agent-libs/config" | ||
) | ||
|
||
func init() { | ||
processors.RegisterPlugin( | ||
"lowercase", | ||
checks.ConfigChecked( | ||
NewLowerCaseProcessor, | ||
checks.RequireFields("fields"), | ||
checks.AllowedFields("fields", "when", "ignore_missing", "fail_on_error", "alter_full_field"), | ||
), | ||
) | ||
} | ||
|
||
// NewLowerCaseProcessor converts event keys matching the provided fields to lowercase | ||
func NewLowerCaseProcessor(c *conf.C) (beat.Processor, error) { | ||
return NewAlterFieldProcessor(c, "lowercase", lowerCase) | ||
} | ||
|
||
func lowerCase(field string) (string, error) { | ||
return strings.ToLower(field), nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to go in the
*Affecting all Beats*
section :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missed that, will update it in following PR