-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[pkg/ottl] Add Decode
function
#33942
Merged
TylerHelmuth
merged 40 commits into
open-telemetry:main
from
bacherfl:feat/32493/decode-function
Sep 5, 2024
Merged
Changes from 14 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
4e9baac
add decode function
bacherfl e6a21c1
additional tests and documentation
bacherfl 7c3ac25
fix typo in docs
bacherfl 8a682c2
add link to iana index
bacherfl a4d6e74
remove obsolete todo comment
bacherfl 51ccfa5
add changelog entry
bacherfl 9dea2c9
appease linter
bacherfl c8a744e
add license comments
bacherfl 04d67bb
appease linter
bacherfl f8d3041
go mod tidy
bacherfl b6aac52
Merge branch 'main' into feat/32493/decode-function
bacherfl bfff733
go mod tidy
bacherfl f6682e2
Merge branch 'main' into feat/32493/decode-function
bacherfl a7f04aa
trigger CI checks
bacherfl 7e18876
incorporate review comments
bacherfl 275933f
incorporate review comments
bacherfl 0f1d591
Merge branch 'main' into feat/32493/decode-function
bacherfl 921b6fe
go mod tidy after merging main
bacherfl 413190e
fix dependencies
bacherfl d31f342
fix dependencies
bacherfl 90939bd
fix dependencies
bacherfl bc294fe
fix dependencies
bacherfl 74be1cf
go mod tidy
bacherfl 874e87c
remove dependency to stanza from ottl
bacherfl d9abbe8
appease linter
bacherfl 1941c07
trigger CI checks
bacherfl 3f2a84b
Merge branch 'main' into feat/32493/decode-function
bacherfl 5647ab2
update dependencies after merging main
bacherfl 5f8b384
Merge branch 'main' into feat/32493/decode-function
bacherfl 822cecc
Merge branch 'main' into feat/32493/decode-function
bacherfl e12de34
Merge branch 'main' into feat/32493/decode-function
bacherfl 9232575
use encodingOverrides from stanza
bacherfl e871462
Merge branch 'main' into feat/32493/decode-function
bacherfl c30068a
fix linting
bacherfl 2f4b94d
fix deps
bacherfl 6a82df1
Merge branch 'main' into feat/32493/decode-function
bacherfl 522fc2d
Merge branch 'main' into feat/32493/decode-function
djaglowski 52056fd
fix linting
bacherfl 208a14d
Merge branch 'main' into feat/32493/decode-function
bacherfl 46029c8
Merge branch 'main' into feat/32493/decode-function
bacherfl 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
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: Added Decode() converter function | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [32493] | ||
|
||
# (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: [] |
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,78 @@ | ||
// 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" | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"golang.org/x/text/encoding/ianaindex" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type DecodeArguments[K any] struct { | ||
Target ottl.Getter[K] | ||
Encoding string | ||
} | ||
|
||
func NewDecodeFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("Decode", &DecodeArguments[K]{}, createDecodeFunction[K]) | ||
} | ||
|
||
func createDecodeFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*DecodeArguments[K]) | ||
if !ok { | ||
return nil, fmt.Errorf("DecodeFactory args must be of type *DecodeArguments[K]") | ||
} | ||
|
||
return Decode(args.Target, args.Encoding) | ||
} | ||
|
||
func Decode[K any](target ottl.Getter[K], encoding string) (ottl.ExprFunc[K], error) { | ||
TylerHelmuth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return func(ctx context.Context, tCtx K) (any, error) { | ||
val, err := target.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var stringValue string | ||
|
||
switch v := val.(type) { | ||
case []byte: | ||
evan-bradley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
stringValue = string(v) | ||
case *string: | ||
stringValue = *v | ||
case string: | ||
stringValue = v | ||
default: | ||
return nil, fmt.Errorf("unsupported type provided to Decode function: %T", v) | ||
} | ||
|
||
switch encoding { | ||
case "base64": | ||
// base64 is not in IANA index, so we have to deal with this encoding separately | ||
decodedBytes, err := base64.StdEncoding.DecodeString(stringValue) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not decode: %w", err) | ||
} | ||
return string(decodedBytes), nil | ||
default: | ||
e, err := ianaindex.IANA.Encoding(encoding) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get encoding for %s: %w", encoding, err) | ||
} | ||
if e == nil { | ||
// for some encodings a nil error and a nil encoding is returned, so we need to double check | ||
// if the encoding is actually set here | ||
return nil, fmt.Errorf("no decoder available for encoding: %s", encoding) | ||
} | ||
decodedString, err := e.NewDecoder().String(stringValue) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not decode: %w", err) | ||
} | ||
return decodedString, 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,121 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
func TestDecode(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
value any | ||
encoding string | ||
want any | ||
expectedError string | ||
} | ||
tests := []testCase{ | ||
evan-bradley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
name: "convert base64 byte array", | ||
value: []byte("dGVzdAo="), | ||
encoding: "base64", | ||
want: "test\n", | ||
}, | ||
{ | ||
name: "convert base64 string", | ||
value: "aGVsbG8gd29ybGQ=", | ||
encoding: "base64", | ||
want: "hello world", | ||
}, | ||
{ | ||
name: "decode us-ascii encoded string", | ||
value: "test string", | ||
encoding: "us-ascii", | ||
want: "test string", | ||
}, | ||
{ | ||
name: "decode ISO-8859-1 encoded string", | ||
value: "test string", | ||
encoding: "ISO-8859-1", | ||
want: "test string", | ||
}, | ||
{ | ||
name: "decode WINDOWS-1251 encoded string", | ||
value: "test string", | ||
encoding: "WINDOWS-1251", | ||
want: "test string", | ||
}, | ||
{ | ||
name: "decode WINDOWS-1252 encoded string", | ||
value: "test string", | ||
encoding: "WINDOWS-1252", | ||
want: "test string", | ||
}, | ||
{ | ||
name: "decode UTF-8 encoded string", | ||
value: "test string", | ||
encoding: "UTF-8", | ||
want: "test string", | ||
}, | ||
{ | ||
name: "decode GB2312 encoded string; no decoder available", | ||
value: "test string", | ||
encoding: "GB2312", | ||
want: nil, | ||
expectedError: "no decoder available for encoding: GB2312", | ||
}, | ||
{ | ||
name: "non-string", | ||
value: 10, | ||
encoding: "base64", | ||
expectedError: "unsupported type provided to Decode function: int", | ||
}, | ||
{ | ||
name: "nil", | ||
value: nil, | ||
encoding: "base64", | ||
expectedError: "unsupported type provided to Decode function: <nil>", | ||
}, | ||
{ | ||
name: "not-base64-string", | ||
value: "!@#$%^&*()_+", | ||
encoding: "base64", | ||
expectedError: "illegal base64 data at input byte", | ||
}, | ||
{ | ||
name: "missing-base64-padding", | ||
value: "cmVtb3ZlZCBwYWRkaW5nCg", | ||
encoding: "base64", | ||
expectedError: "illegal base64 data at input byte", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
expressionFunc, err := createDecodeFunction[any](ottl.FunctionContext{}, &DecodeArguments[any]{ | ||
Target: &ottl.StandardGetSetter[any]{ | ||
Getter: func(context.Context, any) (any, error) { | ||
return tt.value, nil | ||
}, | ||
}, | ||
Encoding: tt.encoding, | ||
}) | ||
|
||
require.Nil(t, err) | ||
|
||
result, err := expressionFunc(nil, nil) | ||
if tt.expectedError != "" { | ||
require.ErrorContains(t, err, tt.expectedError) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, tt.want, result) | ||
}) | ||
} | ||
} |
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
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.
We should mark base64decode as deprecated and link to decode in this doc.
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.
good point - i added a deprecation notice to the base64decode function