-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/ottl]: Add MD5 function (#33793)
**Description:** <Describe what has changed.> <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> Introduced the `MD5` converter function which returns the MD5 hash/digest of the given `value` (path expression to a string telemetry field or a literal string). **Link to tracking Issue:** #33792 **Testing:** <Describe what testing was performed and which tests were added.> - Unit tests - E2E tests **Documentation:** A new README entry was added for the `MD5` function. --------- Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
- Loading branch information
1 parent
9727f23
commit 46a9f94
Showing
6 changed files
with
184 additions
and
0 deletions.
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
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: pkg/ottl | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: "Add the `MD5` function to convert the `value` into a MD5 hash/digest" | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [33792] | ||
|
||
# (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: [user] |
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,47 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs" | ||
|
||
import ( | ||
"context" | ||
"crypto/md5" // #nosec | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type MD5Arguments[K any] struct { | ||
Target ottl.StringGetter[K] | ||
} | ||
|
||
func NewMD5Factory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("MD5", &MD5Arguments[K]{}, createMD5Function[K]) | ||
} | ||
|
||
func createMD5Function[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*MD5Arguments[K]) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("MD5Factory args must be of type *MD5Arguments[K]") | ||
} | ||
|
||
return MD5HashString(args.Target) | ||
} | ||
|
||
func MD5HashString[K any](target ottl.StringGetter[K]) (ottl.ExprFunc[K], error) { | ||
|
||
return func(ctx context.Context, tCtx K) (any, error) { | ||
val, err := target.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
hash := md5.New() // #nosec | ||
_, err = hash.Write([]byte(val)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return hex.EncodeToString(hash.Sum(nil)), nil | ||
}, 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,82 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
func Test_MD5(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value any | ||
expected any | ||
err bool | ||
}{ | ||
{ | ||
name: "string", | ||
value: "hello world", | ||
expected: "5eb63bbbe01eeed093cb22bb8f5acdc3", | ||
}, | ||
{ | ||
name: "empty string", | ||
value: "", | ||
expected: "d41d8cd98f00b204e9800998ecf8427e", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc, err := MD5HashString[any](&ottl.StandardStringGetter[any]{ | ||
Getter: func(context.Context, any) (any, error) { | ||
return tt.value, nil | ||
}, | ||
}) | ||
assert.NoError(t, err) | ||
result, err := exprFunc(nil, nil) | ||
if tt.err { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} | ||
|
||
func Test_MD5Error(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value any | ||
err bool | ||
expectedError string | ||
}{ | ||
{ | ||
name: "non-string", | ||
value: 10, | ||
expectedError: "expected string but got int", | ||
}, | ||
{ | ||
name: "nil", | ||
value: nil, | ||
expectedError: "expected string but got nil", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc, err := MD5HashString[any](&ottl.StandardStringGetter[any]{ | ||
Getter: func(context.Context, any) (any, error) { | ||
return tt.value, nil | ||
}, | ||
}) | ||
assert.NoError(t, err) | ||
_, err = exprFunc(nil, nil) | ||
assert.ErrorContains(t, err, tt.expectedError) | ||
}) | ||
} | ||
} |
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