Skip to content

Commit

Permalink
[pkg/telemetryquerylanguage] Added Int factory function (#11810) (#13441
Browse files Browse the repository at this point in the history
)

* [pkg/telemetryquerylanguage] Added Int factory function to TQL (#11810)

* [pkg/telemetryquerylanguage] review fixes

* [pkg/telemetryquerylanguage] review fixes

* [pkg/telemetryquerylanguage] changelog
  • Loading branch information
mcdoker18 authored Sep 7, 2022
1 parent 3817f98 commit a4a6e62
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/telemetryquerylanguage/functions/tqlcommon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The following functions can be used in any implementation of the Telemetry Query
Factory Functions
- [Join](#join)
- [IsMatch](#ismatch)
- [Int](#int)

Functions
- [set](#set)
Expand Down Expand Up @@ -46,6 +47,31 @@ Examples:

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

## Int

`Int(value)`

The `Int` factory function converts the `value` to int type.

The returned type is int64.

The input `value` types:
* float64. Fraction is discharged (truncation towards zero).
* string. Trying to parse an integer from string if it fails then nil will be returned.
* bool. If `value` is true, then the function will return 1 otherwise 0.
* int64. The function returns the `value` without changes.

If `value` is another type or parsing failed nil is always returned.

The `value` is either a path expression to a telemetry field to retrieve or a literal.

Examples:

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


- `Int("2.0")`

## set

`set(target, value)`
Expand Down
47 changes: 47 additions & 0 deletions pkg/telemetryquerylanguage/functions/tqlcommon/func_int.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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
}
97 changes: 97 additions & 0 deletions pkg/telemetryquerylanguage/functions/tqlcommon/func_int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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: "float64 without decimal",
value: float64(55),
expected: int64(55),
},
{
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)
})
}
}
4 changes: 4 additions & 0 deletions unreleased/tql_int_function.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
change_type: enhancement
component: telemetryquerylanguage
note: Add the Int factory function.
issues: [11810]

0 comments on commit a4a6e62

Please sign in to comment.