Skip to content
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 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pkg/telemetryquerylanguage/functions/tqlcommon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,32 @@
The following functions can be used in any implementation of the Telemetry Query Language. Although they are tested using [pdata](https://github.com/open-telemetry/opentelemetry-collector/tree/main/pdata) for convenience, the function implementation only interact with native Go types or types defined in the [tql package](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/pkg/telemetryquerylanguage/tql).

Factory Functions
- [Join](#join)
- [IsMatch](#ismatch)

Functions
- [set](#set)
- [replace_match](#replace_match)
- [replace_pattern](#replace_pattern)

## Join

`Join(delimiter, ...values)`

The `Join` factory function takes a delimiter and a sequence of values and concatenates their string representation. Unsupported values, such as lists or maps that may substantially increase payload size, are not added to the resulting string.

`delimiter` is a string value that is used to join the string. If no delimiter is desired, then simply pass an empty string.

`values` is a series of values passed as arguments. It supports paths, primitive values, and byte slices (such as trace IDs or span IDs).

Examples:

- `Join(": ", attributes["http.method"], attributes["http.path"])`

- `Join(" ", name, 1)`

- `Join("", "HTTP method is: ", attributes["http.method"])`

## IsMatch

`IsMatch(target, pattern)`
Expand Down
49 changes: 49 additions & 0 deletions pkg/telemetryquerylanguage/functions/tqlcommon/func_join.go
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) {
Copy link
Contributor Author

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.

Copy link
Member

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.

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 pkg/telemetryquerylanguage/functions/tqlcommon/func_join_test.go
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)
})
}
}
16 changes: 16 additions & 0 deletions unreleased/add-join-factory.yaml
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: