-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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/telemetryquerylanguage] Add Join factory #13120
Merged
Merged
Changes from all commits
Commits
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
49 changes: 49 additions & 0 deletions
49
pkg/telemetryquerylanguage/functions/tqlcommon/func_join.go
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,49 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tqlcommon // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/telemetryquerylanguage/functions/tqlcommon" | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/telemetryquerylanguage/tql" | ||
) | ||
|
||
func Join(delimiter string, vals []tql.Getter) (tql.ExprFunc, error) { | ||
return func(ctx tql.TransformContext) interface{} { | ||
builder := strings.Builder{} | ||
for i, rv := range vals { | ||
switch val := rv.Get(ctx).(type) { | ||
case string: | ||
builder.WriteString(val) | ||
case []byte: | ||
builder.WriteString(fmt.Sprintf("%x", val)) | ||
case int64: | ||
builder.WriteString(fmt.Sprint(val)) | ||
case float64: | ||
builder.WriteString(fmt.Sprint(val)) | ||
case bool: | ||
builder.WriteString(fmt.Sprint(val)) | ||
case nil: | ||
builder.WriteString(fmt.Sprint(val)) | ||
} | ||
TylerHelmuth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if i != len(vals)-1 { | ||
builder.WriteString(delimiter) | ||
} | ||
} | ||
return builder.String() | ||
}, nil | ||
} |
244 changes: 244 additions & 0 deletions
244
pkg/telemetryquerylanguage/functions/tqlcommon/func_join_test.go
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,244 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tqlcommon | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/telemetryquerylanguage/tql" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/telemetryquerylanguage/tql/tqltest" | ||
) | ||
|
||
func Test_join(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
delimiter string | ||
vals []tql.StandardGetSetter | ||
expected string | ||
}{ | ||
{ | ||
name: "join strings", | ||
delimiter: " ", | ||
vals: []tql.StandardGetSetter{ | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return "hello" | ||
}, | ||
}, | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return "world" | ||
}, | ||
}, | ||
}, | ||
expected: "hello world", | ||
}, | ||
{ | ||
name: "nil", | ||
delimiter: "", | ||
vals: []tql.StandardGetSetter{ | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return "hello" | ||
}, | ||
}, | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return nil | ||
}, | ||
}, | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return "world" | ||
}, | ||
}, | ||
}, | ||
expected: "hello<nil>world", | ||
}, | ||
{ | ||
name: "integers", | ||
delimiter: "", | ||
vals: []tql.StandardGetSetter{ | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return "hello" | ||
}, | ||
}, | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return int64(1) | ||
}, | ||
}, | ||
}, | ||
expected: "hello1", | ||
}, | ||
{ | ||
name: "floats", | ||
delimiter: "", | ||
vals: []tql.StandardGetSetter{ | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return "hello" | ||
}, | ||
}, | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return 3.14159 | ||
}, | ||
}, | ||
}, | ||
expected: "hello3.14159", | ||
}, | ||
{ | ||
name: "booleans", | ||
delimiter: " ", | ||
vals: []tql.StandardGetSetter{ | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return "hello" | ||
}, | ||
}, | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return true | ||
}, | ||
}, | ||
}, | ||
expected: "hello true", | ||
}, | ||
{ | ||
name: "byte slices", | ||
delimiter: "", | ||
vals: []tql.StandardGetSetter{ | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xd2, 0xe6, 0x3c, 0xbe, 0x71, 0xf5, 0xa8} | ||
}, | ||
}, | ||
}, | ||
expected: "00000000000000000ed2e63cbe71f5a8", | ||
}, | ||
{ | ||
name: "non-byte slices", | ||
delimiter: "", | ||
vals: []tql.StandardGetSetter{ | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 0} | ||
}, | ||
}, | ||
}, | ||
expected: "", | ||
}, | ||
{ | ||
name: "maps", | ||
delimiter: "", | ||
vals: []tql.StandardGetSetter{ | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return map[string]string{"key": "value"} | ||
}, | ||
}, | ||
}, | ||
expected: "", | ||
}, | ||
{ | ||
name: "unprintable value in the middle", | ||
delimiter: "-", | ||
vals: []tql.StandardGetSetter{ | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return "hello" | ||
}, | ||
}, | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return map[string]string{"key": "value"} | ||
}, | ||
}, | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return "world" | ||
}, | ||
}, | ||
}, | ||
expected: "hello--world", | ||
}, | ||
{ | ||
name: "empty string values", | ||
delimiter: "__", | ||
vals: []tql.StandardGetSetter{ | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return "" | ||
}, | ||
}, | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return "" | ||
}, | ||
}, | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return "" | ||
}, | ||
}, | ||
}, | ||
expected: "____", | ||
}, | ||
{ | ||
name: "single argument", | ||
delimiter: "-", | ||
vals: []tql.StandardGetSetter{ | ||
{ | ||
Getter: func(ctx tql.TransformContext) interface{} { | ||
return "hello" | ||
}, | ||
}, | ||
}, | ||
expected: "hello", | ||
}, | ||
{ | ||
name: "no arguments", | ||
delimiter: "-", | ||
vals: []tql.StandardGetSetter{}, | ||
expected: "", | ||
}, | ||
{ | ||
name: "no arguments with an empty delimiter", | ||
delimiter: "", | ||
vals: []tql.StandardGetSetter{}, | ||
expected: "", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := tqltest.TestTransformContext{} | ||
|
||
getters := make([]tql.Getter, len(tt.vals)) | ||
|
||
for i, val := range tt.vals { | ||
getters[i] = val | ||
} | ||
|
||
exprFunc, _ := Join(tt.delimiter, getters) | ||
actual := exprFunc(ctx) | ||
|
||
assert.Equal(t, tt.expected, actual) | ||
}) | ||
} | ||
} |
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,16 @@ | ||
# 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/telemetryquerylanguage | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add `Join`, which allows joining an arbitrary number of strings with a delimiter | ||
|
||
# One or more tracking issues related to the change | ||
issues: [12476] | ||
|
||
# (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: |
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.
One thing I want to call out here is that this function could be either more efficient if we streamline the loop and Builder allocations to cut out some edge cases, or could be simpler if we are okay with allocating additional memory for a string list and letting the
strings.Join
function handle the joining. I took a middle-of-the-road approach since we're likely not going to be joining a substantial number of strings, but I also didn't want to be wasteful. I'm happy to consider alternate approaches if we want to optimize or simplify this.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.
Your implementation seems good to me.