forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/ottl] Added ottl functions for hashing strings (open-telemetry#2…
…2968) * [pkg/ottl] Added ottl functions for hashing strings * [pkg/ottl] Return nil on errors * Documentation and test changes for the hash functions Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com> --------- Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com>
- Loading branch information
1 parent
7603472
commit 38410c6
Showing
8 changed files
with
473 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,20 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
# If your change doesn't affect end users, such as a test fix or a tooling change, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
|
||
# 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 hash converters/functions for OTTL" | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [22725] | ||
|
||
# (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: |
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" | ||
"fmt" | ||
"hash/fnv" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type FnvArguments[K any] struct { | ||
Target ottl.StringGetter[K] `ottlarg:"0"` | ||
} | ||
|
||
func NewFnvFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("FNV", &FnvArguments[K]{}, createFnvFunction[K]) | ||
} | ||
|
||
func createFnvFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*FnvArguments[K]) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("FNVFactory args must be of type *FnvArguments[K]") | ||
} | ||
|
||
return FNVHashString(args.Target) | ||
} | ||
|
||
func FNVHashString[K any](target ottl.StringGetter[K]) (ottl.ExprFunc[K], error) { | ||
|
||
return func(ctx context.Context, tCtx K) (interface{}, error) { | ||
val, err := target.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
hash := fnv.New64a() | ||
_, err = hash.Write([]byte(val)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
hashValue := hash.Sum64() | ||
return int64(hashValue), 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_FNV(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value interface{} | ||
expected interface{} | ||
err bool | ||
}{ | ||
{ | ||
name: "string", | ||
value: "hello world", | ||
expected: int64(8618312879776256743), | ||
}, | ||
{ | ||
name: "empty string", | ||
value: "", | ||
expected: int64(-3750763034362895579), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc, err := FNVHashString[interface{}](&ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(context.Context, interface{}) (interface{}, 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_FNVError(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value interface{} | ||
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 := FNVHashString[interface{}](&ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(context.Context, interface{}) (interface{}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// 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/sha1" // #nosec | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type SHA1Arguments[K any] struct { | ||
Target ottl.StringGetter[K] `ottlarg:"0"` | ||
} | ||
|
||
func NewSHA1Factory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("SHA1", &SHA1Arguments[K]{}, createSHA1Function[K]) | ||
} | ||
|
||
func createSHA1Function[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*SHA1Arguments[K]) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("SHA1Factory args must be of type *SHA1Arguments[K]") | ||
} | ||
|
||
return SHA1HashString(args.Target) | ||
} | ||
|
||
func SHA1HashString[K any](target ottl.StringGetter[K]) (ottl.ExprFunc[K], error) { | ||
|
||
return func(ctx context.Context, tCtx K) (interface{}, error) { | ||
val, err := target.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
hash := sha1.New() // #nosec | ||
_, err = hash.Write([]byte(val)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
hashValue := hex.EncodeToString(hash.Sum(nil)) | ||
return hashValue, 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_SHA1(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value interface{} | ||
expected interface{} | ||
err bool | ||
}{ | ||
{ | ||
name: "string", | ||
value: "hello world", | ||
expected: "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", | ||
}, | ||
{ | ||
name: "empty string", | ||
value: "", | ||
expected: "da39a3ee5e6b4b0d3255bfef95601890afd80709", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc, err := SHA1HashString[interface{}](&ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(context.Context, interface{}) (interface{}, 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_SHA1Error(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value interface{} | ||
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 := SHA1HashString[interface{}](&ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(context.Context, interface{}) (interface{}, error) { | ||
return tt.value, nil | ||
}, | ||
}) | ||
assert.NoError(t, err) | ||
_, err = exprFunc(nil, nil) | ||
assert.ErrorContains(t, err, tt.expectedError) | ||
}) | ||
} | ||
} |
Oops, something went wrong.