Skip to content

Commit

Permalink
[pkg/telemetryquerylanguage] Added Int factory function to TQL (#11810)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdoker18 committed Aug 20, 2022
1 parent 4f0b506 commit 7a0240e
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/telemetryquerylanguage/functions/tqlcommon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ The following functions can be used in any implementation of the Telemetry Query

Factory Functions
- [IsMatch](#ismatch)
- [Int](#int)

Functions
- [set](#set)
Expand All @@ -27,6 +28,22 @@ Examples:

- `IsMatch("string", ".*ring")`

## Int

`Int(value)`

The `Int` factory function converts a float, bool and string data to the int type.

`value` is either a path expression to a telemetry field to retrieve or a literal.
If `value` is another type or parsing failed nil is always returned.

Examples:

- `Int(attributes["http.status_code"])`


- `Int("2.0")`

## set

`set(target, value)`
Expand Down
48 changes: 48 additions & 0 deletions pkg/telemetryquerylanguage/functions/tqlcommon/func_int.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 (
"strconv"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/telemetryquerylanguage/tql"
)

func Int(target tql.Getter) (tql.ExprFunc, error) {
return func(ctx tql.TransformContext) interface{} {
value := target.Get(ctx)
switch value := value.(type) {
case int64:
return value
case string:
intValue, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return nil
}

return intValue
case float64:
return (int64)(value)
case bool:
if value {
return int64(1)
}

return int64(0)
default:
return nil
}
}, nil
}
92 changes: 92 additions & 0 deletions pkg/telemetryquerylanguage/functions/tqlcommon/func_int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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_Int(t *testing.T) {
tests := []struct {
name string
value interface{}
expected interface{}
}{
{
name: "string",
value: "50",
expected: int64(50),
},
{
name: "empty string",
value: "",
expected: nil,
},
{
name: "not a number string",
value: "test",
expected: nil,
},
{
name: "int64",
value: int64(333),
expected: int64(333),
},
{
name: "float64",
value: float64(2.7),
expected: int64(2),
},
{
name: "true",
value: true,
expected: int64(1),
},
{
name: "false",
value: false,
expected: int64(0),
},
{
name: "nil",
value: nil,
expected: nil,
},
{
name: "some struct",
value: struct{}{},
expected: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := tqltest.TestTransformContext{}

exprFunc, _ := Int(&tql.StandardGetSetter{
Getter: func(_ tql.TransformContext) interface{} {
return tt.value
},
})
actual := exprFunc(ctx)

assert.Equal(t, tt.expected, actual)
})
}
}

0 comments on commit 7a0240e

Please sign in to comment.