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

feat: add duration to int float converters #25069

Merged
merged 16 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 20 additions & 0 deletions .chloggen/feat_dur-to-num.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# 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: "Add converters to covert duration to nanoseconds, microseconds, milliseconds, seconds, minutes or hours"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [24686]

# (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:
43 changes: 43 additions & 0 deletions pkg/ottl/ottlfuncs/func_duration_to_hours.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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"
"fmt"
"time"

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

type DurationToHoursArguments[K any] struct {
Duration ottl.StringGetter[K] `ottlarg:"0"`
fchikwekwe marked this conversation as resolved.
Show resolved Hide resolved
}

func NewDurationToHoursFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("DurationToHours", &DurationToHoursArguments[K]{}, createDurationToHoursFunction[K])
fchikwekwe marked this conversation as resolved.
Show resolved Hide resolved
}
func createDurationToHoursFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
args, ok := oArgs.(*DurationToHoursArguments[K])

if !ok {
return nil, fmt.Errorf("DurationToHoursFactory args must be of type *DurationToHoursArguments[K]")
}

return DurationToHours(args.Duration)
}

func DurationToHours[K any](duration ottl.StringGetter[K]) (ottl.ExprFunc[K], error) {
return func(ctx context.Context, tCtx K) (interface{}, error) {
d, err := duration.Get(ctx, tCtx)
if err != nil {
return nil, err
}
dur, err := time.ParseDuration(d)
if err != nil {
return nil, err
}
return dur.Hours(), nil
}, nil
}
67 changes: 67 additions & 0 deletions pkg/ottl/ottlfuncs/func_duration_to_hours_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package ottlfuncs

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

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

func Test_DurationToHours(t *testing.T) {
tests := []struct {
name string
duration ottl.StringGetter[interface{}]
expected float64
}{
{
name: "100 hours",
duration: &ottl.StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "100h", nil
},
},
expected: 100,
},
{
name: "1 min",
duration: &ottl.StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "100m", nil
},
},
expected: 1.6666666666666665,
},
{
name: "234 milliseconds",
duration: &ottl.StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "234ms", nil
},
},
expected: 0.000065,
},
{
name: "1 hour 40 mins 3 seconds 30 milliseconds 100 microseconds 1 nanosecond",
duration: &ottl.StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "1h40m3s30ms100us1ns", nil
},
},
expected: 1.667508361111389,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc, err := DurationToHours(tt.duration)
assert.NoError(t, err)
result, err := exprFunc(nil, nil)
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}
43 changes: 43 additions & 0 deletions pkg/ottl/ottlfuncs/func_duration_to_micro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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"
"fmt"
"time"

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

type DurationToMicroArguments[K any] struct {
Duration ottl.StringGetter[K] `ottlarg:"0"`
}

func NewDurationToMicroFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("DurationToMicro", &DurationToMicroArguments[K]{}, createDurationToMicroFunction[K])
}
func createDurationToMicroFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
args, ok := oArgs.(*DurationToMicroArguments[K])

if !ok {
return nil, fmt.Errorf("DurationToMicroFactory args must be of type *DurationToMicroArguments[K]")
}

return DurationToMicro(args.Duration)
}

func DurationToMicro[K any](duration ottl.StringGetter[K]) (ottl.ExprFunc[K], error) {
return func(ctx context.Context, tCtx K) (interface{}, error) {
d, err := duration.Get(ctx, tCtx)
if err != nil {
return nil, err
}
dur, err := time.ParseDuration(d)
if err != nil {
return nil, err
}
return dur.Microseconds(), nil
}, nil
}
67 changes: 67 additions & 0 deletions pkg/ottl/ottlfuncs/func_duration_to_micro_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package ottlfuncs

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

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

func Test_DurationToMicro(t *testing.T) {
tests := []struct {
name string
duration ottl.StringGetter[interface{}]
expected int64
}{
{
name: "100 microseconds",
duration: &ottl.StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "100us", nil
},
},
expected: 100,
},
{
name: "1000 hour",
duration: &ottl.StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "100h", nil
},
},
expected: 360000000000,
},
{
name: "50 mins",
duration: &ottl.StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "50m", nil
},
},
expected: 3000000000,
},
{
name: "1 hour 40 mins 3 seconds 30 milliseconds 100 microseconds",
duration: &ottl.StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "1h40m3s30ms100us", nil
},
},
expected: 6003030100,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc, err := DurationToMicro(tt.duration)
assert.NoError(t, err)
result, err := exprFunc(nil, nil)
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}
43 changes: 43 additions & 0 deletions pkg/ottl/ottlfuncs/func_duration_to_milli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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"
"fmt"
"time"

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

type DurationToMilliArguments[K any] struct {
Duration ottl.StringGetter[K] `ottlarg:"0"`
}

func NewDurationToMilliFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("DurationToMilli", &DurationToMilliArguments[K]{}, createDurationToMilliFunction[K])
}
func createDurationToMilliFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
args, ok := oArgs.(*DurationToMilliArguments[K])

if !ok {
return nil, fmt.Errorf("DurationToMilliFactory args must be of type *DurationToMilliArguments[K]")
}

return DurationToMilli(args.Duration)
}

func DurationToMilli[K any](duration ottl.StringGetter[K]) (ottl.ExprFunc[K], error) {
return func(ctx context.Context, tCtx K) (interface{}, error) {
d, err := duration.Get(ctx, tCtx)
if err != nil {
return nil, err
}
dur, err := time.ParseDuration(d)
if err != nil {
return nil, err
}
return dur.Milliseconds(), nil
}, nil
}
67 changes: 67 additions & 0 deletions pkg/ottl/ottlfuncs/func_duration_to_milli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package ottlfuncs

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

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

func Test_DurationToMilli(t *testing.T) {
tests := []struct {
name string
duration ottl.StringGetter[interface{}]
expected int64
}{
{
name: "100 Milliseconds",
duration: &ottl.StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "100ms", nil
},
},
expected: 100,
},
{
name: "1000 hour",
duration: &ottl.StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "100h", nil
},
},
expected: 360000000,
},
{
name: "47 mins",
duration: &ottl.StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "47m", nil
},
},
expected: 2820000,
},
{
name: "1 hour 40 mins 3 seconds 30 milliseconds",
duration: &ottl.StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "1h40m3s30ms", nil
},
},
expected: 6003030,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc, err := DurationToMilli(tt.duration)
assert.NoError(t, err)
result, err := exprFunc(nil, nil)
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}
Loading