-
Notifications
You must be signed in to change notification settings - Fork 487
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
Add an otelcol.processor.attributes component #3349
Merged
mattdurham
merged 12 commits into
main
from
2294-flow-otelcolprocessorattributes-component-3
Apr 18, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5215731
Add a otelcol.processor.attributes component
ptodev eefaa9d
Update changelog
ptodev b6c3ab3
Fix linter errors
ptodev 5974604
Update docs/sources/flow/reference/components/otelcol.processor.attri…
ptodev bfea751
PR review fixes
ptodev 6776869
PR review fixes
ptodev 5bb4a5b
Update documentation
ptodev 32364eb
Update docs
ptodev e48a409
Fix tests
ptodev 8a6951b
Rename "cacheenabled" and "cachemaxnumentries" with "_"
ptodev da675a6
Merge branch 'main' into 2294-flow-otelcolprocessorattributes-compone…
mattdurham 0bf1649
Merge branch 'main' into 2294-flow-otelcolprocessorattributes-compone…
mattdurham 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package otelcol | ||
|
||
type AttrActionKeyValueSlice []AttrActionKeyValue | ||
|
||
func (actions AttrActionKeyValueSlice) Convert() []interface{} { | ||
ptodev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
res := make([]interface{}, 0, len(actions)) | ||
|
||
if len(actions) == 0 { | ||
return res | ||
} | ||
|
||
for _, action := range actions { | ||
res = append(res, action.convert()) | ||
} | ||
return res | ||
} | ||
|
||
type AttrActionKeyValue struct { | ||
// Key specifies the attribute to act upon. | ||
// This is a required field. | ||
Key string `river:"key,attr"` | ||
|
||
// Value specifies the value to populate for the key. | ||
// The type of the value is inferred from the configuration. | ||
Value interface{} `river:"value,attr,optional"` | ||
|
||
// A regex pattern must be specified for the action EXTRACT. | ||
// It uses the attribute specified by `key' to extract values from | ||
// The target keys are inferred based on the names of the matcher groups | ||
// provided and the names will be inferred based on the values of the | ||
// matcher group. | ||
// Note: All subexpressions must have a name. | ||
// Note: The value type of the source key must be a string. If it isn't, | ||
// no extraction will occur. | ||
RegexPattern string `river:"pattern,attr,optional"` | ||
|
||
// FromAttribute specifies the attribute to use to populate | ||
// the value. If the attribute doesn't exist, no action is performed. | ||
FromAttribute string `river:"from_attribute,attr,optional"` | ||
|
||
// FromContext specifies the context value to use to populate | ||
// the value. The values would be searched in client.Info.Metadata. | ||
// If the key doesn't exist, no action is performed. | ||
// If the key has multiple values the values will be joined with `;` separator. | ||
FromContext string `river:"from_context,attr,optional"` | ||
|
||
// ConvertedType specifies the target type of an attribute to be converted | ||
// If the key doesn't exist, no action is performed. | ||
// If the value cannot be converted, the original value will be left as-is | ||
ConvertedType string `river:"converted_type,attr,optional"` | ||
|
||
// Action specifies the type of action to perform. | ||
// The set of values are {INSERT, UPDATE, UPSERT, DELETE, HASH}. | ||
// Both lower case and upper case are supported. | ||
// INSERT - Inserts the key/value to attributes when the key does not exist. | ||
// No action is applied to attributes where the key already exists. | ||
// Either Value, FromAttribute or FromContext must be set. | ||
// UPDATE - Updates an existing key with a value. No action is applied | ||
// to attributes where the key does not exist. | ||
// Either Value, FromAttribute or FromContext must be set. | ||
// UPSERT - Performs insert or update action depending on the attributes | ||
// containing the key. The key/value is inserted to attributes | ||
// that did not originally have the key. The key/value is updated | ||
// for attributes where the key already existed. | ||
// Either Value, FromAttribute or FromContext must be set. | ||
// DELETE - Deletes the attribute. If the key doesn't exist, | ||
// no action is performed. | ||
// HASH - Calculates the SHA-1 hash of an existing value and overwrites the | ||
// value with it's SHA-1 hash result. | ||
// EXTRACT - Extracts values using a regular expression rule from the input | ||
// 'key' to target keys specified in the 'rule'. If a target key | ||
// already exists, it will be overridden. | ||
// CONVERT - converts the type of an existing attribute, if convertable | ||
// This is a required field. | ||
Action string `river:"action,attr"` | ||
} | ||
|
||
// Convert converts args into the upstream type. | ||
func (args *AttrActionKeyValue) convert() map[string]interface{} { | ||
if args == nil { | ||
return nil | ||
} | ||
|
||
return map[string]interface{}{ | ||
"key": args.Key, | ||
"action": args.Action, | ||
"value": args.Value, | ||
"pattern": args.RegexPattern, | ||
"from_attribute": args.FromAttribute, | ||
"from_context": args.FromContext, | ||
"converted_type": args.ConvertedType, | ||
} | ||
} |
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,60 @@ | ||
package otelcol_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/grafana/agent/component/otelcol" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConvertAttrAction(t *testing.T) { | ||
inputActions := otelcol.AttrActionKeyValueSlice{ | ||
{ | ||
Action: "insert", | ||
Value: 123, | ||
Key: "attribute1", | ||
}, | ||
{ | ||
Action: "delete", | ||
Key: "attribute2", | ||
}, | ||
{ | ||
Action: "upsert", | ||
Value: true, | ||
Key: "attribute3", | ||
}, | ||
} | ||
|
||
expectedActions := []interface{}{ | ||
map[string]interface{}{ | ||
"action": "insert", | ||
"converted_type": "", | ||
"from_attribute": "", | ||
"from_context": "", | ||
"key": "attribute1", | ||
"pattern": "", | ||
"value": 123, | ||
}, | ||
map[string]interface{}{ | ||
"action": "delete", | ||
"converted_type": "", | ||
"from_attribute": "", | ||
"from_context": "", | ||
"key": "attribute2", | ||
"pattern": "", | ||
"value": interface{}(nil), | ||
}, | ||
map[string]interface{}{ | ||
"action": "upsert", | ||
"converted_type": "", | ||
"from_attribute": "", | ||
"from_context": "", | ||
"key": "attribute3", | ||
"pattern": "", | ||
"value": true, | ||
}, | ||
} | ||
|
||
result := inputActions.Convert() | ||
require.Equal(t, expectedActions, result) | ||
} |
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 pattern is something I made up myself, since it makes sense to use actions as a slice and to have the convert function in config_atraction.go. If you think this pattern doesn't make senes though, let me know