From ac6130ee55811c39d15b4a43d0f58f14eef083cd Mon Sep 17 00:00:00 2001 From: fchikwekwe Date: Fri, 16 Jun 2023 11:57:48 -0400 Subject: [PATCH 1/8] feat: convert string to duration --- pkg/ottl/ottlfuncs/func_duration.go | 46 ++++++ pkg/ottl/ottlfuncs/func_duration_test.go | 201 +++++++++++++++++++++++ 2 files changed, 247 insertions(+) create mode 100644 pkg/ottl/ottlfuncs/func_duration.go create mode 100644 pkg/ottl/ottlfuncs/func_duration_test.go diff --git a/pkg/ottl/ottlfuncs/func_duration.go b/pkg/ottl/ottlfuncs/func_duration.go new file mode 100644 index 000000000000..e8d244dd1389 --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_duration.go @@ -0,0 +1,46 @@ +// 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 DurationArguments[K any] struct { + Duration ottl.StringGetter[K] `ottlarg:"0"` +} + +func NewDurationFactory[K any]() ottl.Factory[K] { + return ottl.NewFactory("Duration", &DurationArguments[K]{}, createDurationFunction[K]) +} +func createDurationFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { + args, ok := oArgs.(*DurationArguments[K]) + + if !ok { + return nil, fmt.Errorf("DurationFactory args must be of type *DurationArguments[K]") + } + + return Duration(args.Duration) +} + +func Duration[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 + } + // if d == "" { + // return nil, fmt.Errorf("duration cannot be nil") + // } + dur, err := time.ParseDuration(d) + if err != nil { + return nil, err + } + return dur, nil + }, nil +} diff --git a/pkg/ottl/ottlfuncs/func_duration_test.go b/pkg/ottl/ottlfuncs/func_duration_test.go new file mode 100644 index 000000000000..09163df48c46 --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_duration_test.go @@ -0,0 +1,201 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package ottlfuncs + +import ( + "context" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" +) + +func Test_Duration(t *testing.T) { + tests := []struct { + name string + duration ottl.StringGetter[interface{}] + expected time.Duration + }{ + { + name: "100 milliseconds", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "100ms", nil + }, + }, + expected: time.Duration(100000000), + }, { + name: "234 microseconds", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "234us", nil + }, + }, + expected: time.Duration(234000), + }, { + name: "777 nanoseconds", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "777ns", nil + }, + }, + expected: time.Duration(777), + }, + { + name: "one second", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "1s", nil + }, + }, + expected: time.Duration(1000000000), + }, + { + name: "two hundred second", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "200s", nil + }, + }, + expected: time.Duration(200000000000), + }, + { + name: "three minutes", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "3m", nil + }, + }, + expected: time.Duration(180000000000), + }, + { + name: "45 minutes", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "45m", nil + }, + }, + expected: time.Duration(2700000000000), + }, + { + name: "7 mins, 12 secs", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "7m12s", nil + }, + }, + expected: time.Duration(432000000000), + }, + { + name: "4 hours", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "4h", nil + }, + }, + expected: time.Duration(14400000000000), + }, + { + name: "5 hours, 23 mins, 59 secs", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "5h23m59s", nil + }, + }, + expected: time.Duration(19439000000000), + }, + { + name: "5 hours, 59 secs", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "5h59s", nil + }, + }, + expected: time.Duration(18059000000000), + }, + { + name: "5 hours, 23 mins", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "5h23m", nil + }, + }, + expected: time.Duration(19380000000000), + }, + { + name: "2 mins, 1 sec, 64 microsecs", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "2m1s64us", nil + }, + }, + expected: time.Duration(121000064000), + }, + { + name: "59 hours, 1 min, 78 millisecs", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "59h1m78ms", nil + }, + }, + expected: time.Duration(212460078000000), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + exprFunc, err := Duration(tt.duration) + assert.NoError(t, err) + result, err := exprFunc(nil, nil) + assert.NoError(t, err) + assert.Equal(t, tt.expected, result) + }) + } +} + +func Test_DurationError(t *testing.T) { + tests := []struct { + name string + duration ottl.StringGetter[interface{}] + expectedError string + }{ + { + name: "empty duration", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "", nil + }, + }, + expectedError: "invalid duration", + }, + { + name: "empty duration", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "one second", nil + }, + }, + expectedError: "invalid duration", + }, + { + name: "empty duration", + duration: &ottl.StandardStringGetter[interface{}]{ + Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { + return "unknown unit", nil + }, + }, + expectedError: "invalid duration", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + exprFunc, err := Duration[any](tt.duration) + require.NoError(t, err) + _, err = exprFunc(context.Background(), nil) + assert.ErrorContains(t, err, tt.expectedError) + }) + } +} From aa03f1949db927cdcea0b34015c6d7b7f3273db7 Mon Sep 17 00:00:00 2001 From: fchikwekwe Date: Fri, 16 Jun 2023 12:30:40 -0400 Subject: [PATCH 2/8] chore: add changelog and add func to func list --- .chloggen/feat_duration-func.yaml | 20 ++++++++++++++++++++ pkg/ottl/ottlfuncs/functions.go | 1 + 2 files changed, 21 insertions(+) create mode 100755 .chloggen/feat_duration-func.yaml diff --git a/.chloggen/feat_duration-func.yaml b/.chloggen/feat_duration-func.yaml new file mode 100755 index 000000000000..3bb772a410d2 --- /dev/null +++ b/.chloggen/feat_duration-func.yaml @@ -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: 'enchancement' + +# 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 new `Duration` converter to convert string to a Golang duration" + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [22015] + +# (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: diff --git a/pkg/ottl/ottlfuncs/functions.go b/pkg/ottl/ottlfuncs/functions.go index 45b6f5a15a07..e8499781ca16 100644 --- a/pkg/ottl/ottlfuncs/functions.go +++ b/pkg/ottl/ottlfuncs/functions.go @@ -36,6 +36,7 @@ func converters[K any]() []ottl.Factory[K] { // Converters NewConcatFactory[K](), NewConvertCaseFactory[K](), + NewDurationFactory[K](), NewFnvFactory[K](), NewIntFactory[K](), NewIsMapFactory[K](), From 6697bb6f4e1f8c847824d53e3e9047647df97697 Mon Sep 17 00:00:00 2001 From: fchikwekwe Date: Fri, 16 Jun 2023 12:33:23 -0400 Subject: [PATCH 3/8] fix: remove typo --- .chloggen/feat_duration-func.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/feat_duration-func.yaml b/.chloggen/feat_duration-func.yaml index 3bb772a410d2..6b4145713e2a 100755 --- a/.chloggen/feat_duration-func.yaml +++ b/.chloggen/feat_duration-func.yaml @@ -3,7 +3,7 @@ # 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: 'enchancement' +change_type: 'enhancement' # The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) component: 'pkg/ottl' From d7782d9ec7ad90419f83a2566759ec53bfacf85f Mon Sep 17 00:00:00 2001 From: fchikwekwe Date: Thu, 6 Jul 2023 10:52:50 -0400 Subject: [PATCH 4/8] doc: add converter func docs for time and duration --- .chloggen/feat_duration-func.yaml | 2 +- pkg/ottl/ottlfuncs/README.md | 28 ++++++++++++++++++++++++ pkg/ottl/ottlfuncs/func_duration.go | 3 --- pkg/ottl/ottlfuncs/func_duration_test.go | 9 -------- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/.chloggen/feat_duration-func.yaml b/.chloggen/feat_duration-func.yaml index 6b4145713e2a..2cf9f9a49750 100755 --- a/.chloggen/feat_duration-func.yaml +++ b/.chloggen/feat_duration-func.yaml @@ -9,7 +9,7 @@ change_type: 'enhancement' component: 'pkg/ottl' # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). -note: "Add new `Duration` converter to convert string to a Golang duration" +note: "Add new `Duration` converter to convert string to a Golang time.duration" # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. issues: [22015] diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index e63d09fe7cc2..0feeab37f3bd 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -278,6 +278,7 @@ Available Converters: - [Concat](#concat) - [ConvertCase](#convertcase) - [FNV](#fnv) +- [Duration](#duration) - [Int](#int) - [IsMap](#ismap) - [IsMatch](#ismatch) @@ -288,6 +289,7 @@ Available Converters: - [SHA256](#sha256) - [SpanID](#spanid) - [Split](#split) +- [Time](#time) - [TraceID](#traceid) - [Substring](#substring) - [UUID](#UUID) @@ -335,6 +337,20 @@ Examples: - `ConvertCase(metric.name, "snake")` +### Duration + +The `Duration` Converter taking a string representation of a duration and converts it to a Golang `time.duration`. + +`duration` is a string. + +If either `duration` is nil or is in a format that cannot be converted to Golang `time.duration`, an error is returned. + +Examples: + +- `Duration("3s")` +- `Duration("333ms")` +- `Duration("1000000h")` + ### FNV `FNV(value)` @@ -569,6 +585,18 @@ Examples: - ```Split("A|B|C", "|")``` +### Time + +The `Time` Converter taking a string representation of a duration and converts it to a Golang `time.duration`. + +`time` is a string. `format` is a string. + +If either `time` or `format` are nil, an error is returned. The parser used is the parser at [internal/coreinternal/parser](github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/timeutils). If the time and format do not follow the parsing rules used by this parser, an error is returned. + +Examples: + +- `Time("02/04/2023", "%m/%d/%Y")` + ### TraceID `TraceID(bytes)` diff --git a/pkg/ottl/ottlfuncs/func_duration.go b/pkg/ottl/ottlfuncs/func_duration.go index e8d244dd1389..85d0c6f27eda 100644 --- a/pkg/ottl/ottlfuncs/func_duration.go +++ b/pkg/ottl/ottlfuncs/func_duration.go @@ -34,9 +34,6 @@ func Duration[K any](duration ottl.StringGetter[K]) (ottl.ExprFunc[K], error) { if err != nil { return nil, err } - // if d == "" { - // return nil, fmt.Errorf("duration cannot be nil") - // } dur, err := time.ParseDuration(d) if err != nil { return nil, err diff --git a/pkg/ottl/ottlfuncs/func_duration_test.go b/pkg/ottl/ottlfuncs/func_duration_test.go index 09163df48c46..878e289080fe 100644 --- a/pkg/ottl/ottlfuncs/func_duration_test.go +++ b/pkg/ottl/ottlfuncs/func_duration_test.go @@ -180,15 +180,6 @@ func Test_DurationError(t *testing.T) { }, expectedError: "invalid duration", }, - { - name: "empty duration", - duration: &ottl.StandardStringGetter[interface{}]{ - Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { - return "unknown unit", nil - }, - }, - expectedError: "invalid duration", - }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From de007450545310c01523577fbd80f9c8b61a5849 Mon Sep 17 00:00:00 2001 From: Faith Chikwekwe Date: Thu, 6 Jul 2023 10:59:33 -0400 Subject: [PATCH 5/8] Update pkg/ottl/ottlfuncs/README.md Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com> --- pkg/ottl/ottlfuncs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index 0feeab37f3bd..3f272ca695f7 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -339,7 +339,7 @@ Examples: ### Duration -The `Duration` Converter taking a string representation of a duration and converts it to a Golang `time.duration`. +The `Duration` Converter takes a string representation of a duration and converts it to a Golang `time.duration`. `duration` is a string. From 3aab5fc85e0ea1a1ad542fc88698bbe39834511a Mon Sep 17 00:00:00 2001 From: Faith Chikwekwe Date: Thu, 6 Jul 2023 10:59:42 -0400 Subject: [PATCH 6/8] Update pkg/ottl/ottlfuncs/README.md Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com> --- pkg/ottl/ottlfuncs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index 3f272ca695f7..e7d44fa6fc05 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -587,7 +587,7 @@ Examples: ### Time -The `Time` Converter taking a string representation of a duration and converts it to a Golang `time.duration`. +The `Time` Converter takes a string representation of a time and converts it to a Golang `time.Time`. `time` is a string. `format` is a string. From 65cdec9011017f9c664bf49ac1086e4aa9588593 Mon Sep 17 00:00:00 2001 From: Faith Chikwekwe Date: Thu, 6 Jul 2023 10:59:51 -0400 Subject: [PATCH 7/8] Update pkg/ottl/ottlfuncs/README.md Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com> --- pkg/ottl/ottlfuncs/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index e7d44fa6fc05..ed27007ce3ad 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -339,6 +339,8 @@ Examples: ### Duration +`Duration(duration)` + The `Duration` Converter takes a string representation of a duration and converts it to a Golang `time.duration`. `duration` is a string. From 6382ed759ccedc527d09bcc431715ca72f75b679 Mon Sep 17 00:00:00 2001 From: fchikwekwe Date: Thu, 6 Jul 2023 11:24:04 -0400 Subject: [PATCH 8/8] fix: remove dead link --- pkg/ottl/ottlfuncs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index 0feeab37f3bd..5d8e7bd4d3c1 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -591,7 +591,7 @@ The `Time` Converter taking a string representation of a duration and converts i `time` is a string. `format` is a string. -If either `time` or `format` are nil, an error is returned. The parser used is the parser at [internal/coreinternal/parser](github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/timeutils). If the time and format do not follow the parsing rules used by this parser, an error is returned. +If either `time` or `format` are nil, an error is returned. The parser used is the parser at [internal/coreinternal/parser](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/internal/coreinternal/timeutils). If the time and format do not follow the parsing rules used by this parser, an error is returned. Examples: