Skip to content

Commit 4cc4ba3

Browse files
committed
Add OTTL md5 function
1 parent 2c94519 commit 4cc4ba3

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-0
lines changed

.chloggen/ottl_md5_function.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Add the `MD5` function to convert the `value` into a MD5 hash/digest"
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [33792]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

pkg/ottl/e2e/e2e_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,12 @@ func Test_e2e_converters(t *testing.T) {
465465
tCtx.GetLogRecord().Attributes().PutDouble("test", 0)
466466
},
467467
},
468+
{
469+
statement: `set(attributes["test"], MD5("pass"))`,
470+
want: func(tCtx ottllog.TransformContext) {
471+
tCtx.GetLogRecord().Attributes().PutStr("test", "1a1dc91c907325c69271ddf0c944bc72")
472+
},
473+
},
468474
{
469475
statement: `set(attributes["test"], Microseconds(Duration("1ms")))`,
470476
want: func(tCtx ottllog.TransformContext) {

pkg/ottl/ottlfuncs/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ Available Converters:
429429
- [IsString](#isstring)
430430
- [Len](#len)
431431
- [Log](#log)
432+
- [MD5](#md5)
432433
- [Microseconds](#microseconds)
433434
- [Milliseconds](#milliseconds)
434435
- [Minute](#minute)
@@ -831,6 +832,26 @@ Examples:
831832

832833
- `Int(Log(attributes["duration_ms"])`
833834

835+
### MD5
836+
837+
`MD5(value)`
838+
839+
The `MD5` Converter converts the `value` to a md5 hash/digest.
840+
841+
The returned type is string.
842+
843+
`value` is either a path expression to a string telemetry field or a literal string. If `value` is another type an error is returned.
844+
845+
If an error occurs during hashing it will be returned.
846+
847+
Examples:
848+
849+
- `MD5(attributes["device.name"])`
850+
851+
- `MD5("name")`
852+
853+
**Note:** According to the National Institute of Standards and Technology (NIST), MD5 is no longer a recommended hash function. It should be avoided except when required for compatibility. New uses should prefer FNV whenever possible.
854+
834855
### Microseconds
835856

836857
`Microseconds(value)`

pkg/ottl/ottlfuncs/func_md5.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
5+
6+
import (
7+
"context"
8+
"crypto/md5" // #nosec
9+
"encoding/hex"
10+
"fmt"
11+
12+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
13+
)
14+
15+
type MD5Arguments[K any] struct {
16+
Target ottl.StringGetter[K]
17+
}
18+
19+
func NewMD5Factory[K any]() ottl.Factory[K] {
20+
return ottl.NewFactory("MD5", &MD5Arguments[K]{}, createMD5Function[K])
21+
}
22+
23+
func createMD5Function[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
24+
args, ok := oArgs.(*MD5Arguments[K])
25+
26+
if !ok {
27+
return nil, fmt.Errorf("MD5Factory args must be of type *MD5Arguments[K]")
28+
}
29+
30+
return MD5HashString(args.Target)
31+
}
32+
33+
func MD5HashString[K any](target ottl.StringGetter[K]) (ottl.ExprFunc[K], error) {
34+
35+
return func(ctx context.Context, tCtx K) (any, error) {
36+
val, err := target.Get(ctx, tCtx)
37+
if err != nil {
38+
return nil, err
39+
}
40+
hash := md5.New() // #nosec
41+
_, err = hash.Write([]byte(val))
42+
if err != nil {
43+
return nil, err
44+
}
45+
return hex.EncodeToString(hash.Sum(nil)), nil
46+
}, nil
47+
}

pkg/ottl/ottlfuncs/func_md5_test.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
12+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
13+
)
14+
15+
func Test_MD5(t *testing.T) {
16+
tests := []struct {
17+
name string
18+
value any
19+
expected any
20+
err bool
21+
}{
22+
{
23+
name: "string",
24+
value: "hello world",
25+
expected: "5eb63bbbe01eeed093cb22bb8f5acdc3",
26+
},
27+
{
28+
name: "empty string",
29+
value: "",
30+
expected: "d41d8cd98f00b204e9800998ecf8427e",
31+
},
32+
}
33+
for _, tt := range tests {
34+
t.Run(tt.name, func(t *testing.T) {
35+
exprFunc, err := MD5HashString[any](&ottl.StandardStringGetter[any]{
36+
Getter: func(context.Context, any) (any, error) {
37+
return tt.value, nil
38+
},
39+
})
40+
assert.NoError(t, err)
41+
result, err := exprFunc(nil, nil)
42+
if tt.err {
43+
assert.Error(t, err)
44+
} else {
45+
assert.NoError(t, err)
46+
}
47+
assert.Equal(t, tt.expected, result)
48+
})
49+
}
50+
}
51+
52+
func Test_MD5Error(t *testing.T) {
53+
tests := []struct {
54+
name string
55+
value any
56+
err bool
57+
expectedError string
58+
}{
59+
{
60+
name: "non-string",
61+
value: 10,
62+
expectedError: "expected string but got int",
63+
},
64+
{
65+
name: "nil",
66+
value: nil,
67+
expectedError: "expected string but got nil",
68+
},
69+
}
70+
for _, tt := range tests {
71+
t.Run(tt.name, func(t *testing.T) {
72+
exprFunc, err := MD5HashString[any](&ottl.StandardStringGetter[any]{
73+
Getter: func(context.Context, any) (any, error) {
74+
return tt.value, nil
75+
},
76+
})
77+
assert.NoError(t, err)
78+
_, err = exprFunc(nil, nil)
79+
assert.ErrorContains(t, err, tt.expectedError)
80+
})
81+
}
82+
}

pkg/ottl/ottlfuncs/functions.go

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func converters[K any]() []ottl.Factory[K] {
5656
NewIsStringFactory[K](),
5757
NewLenFactory[K](),
5858
NewLogFactory[K](),
59+
NewMD5Factory[K](),
5960
NewMicrosecondsFactory[K](),
6061
NewMillisecondsFactory[K](),
6162
NewMinuteFactory[K](),

0 commit comments

Comments
 (0)