-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add time to int converters (#25147)
Description: Allows conversion of time to nanoseconds, microseconds, milliseconds or seconds Link to tracking Issue: Related to #24686 Testing: Unit tests for each converter Documentation: --------- Co-authored-by: Alex Boten <[email protected]> Co-authored-by: Tyler Helmuth <[email protected]> Co-authored-by: Evan Bradley <[email protected]>
- Loading branch information
1 parent
4fabc48
commit 3a6f547
Showing
12 changed files
with
571 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 convert time to unix nanoseconds, unix microseconds, unix milliseconds or unix seconds" | ||
|
||
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type UnixMicroArguments[K any] struct { | ||
Time ottl.TimeGetter[K] `ottlarg:"0"` | ||
} | ||
|
||
func NewUnixMicroFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("UnixMicro", &UnixMicroArguments[K]{}, createUnixMicroFunction[K]) | ||
} | ||
func createUnixMicroFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*UnixMicroArguments[K]) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("UnixMicroFactory args must be of type *UnixMicroArguments[K]") | ||
} | ||
|
||
return UnixMicro(args.Time) | ||
} | ||
|
||
func UnixMicro[K any](inputTime ottl.TimeGetter[K]) (ottl.ExprFunc[K], error) { | ||
return func(ctx context.Context, tCtx K) (interface{}, error) { | ||
t, err := inputTime.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return t.UnixMicro(), nil | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
func Test_TimeUnixMicro(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
time ottl.TimeGetter[interface{}] | ||
expected time.Time | ||
}{ | ||
{ | ||
name: "January 1, 2023", | ||
time: &ottl.StandardTimeGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return time.Date(2023, 1, 1, 0, 0, 0, 0, time.Local), nil | ||
}, | ||
}, | ||
expected: time.Date(2023, 1, 1, 0, 0, 0, 0, time.Local), | ||
}, | ||
{ | ||
name: "April 30, 2001, 3pm", | ||
time: &ottl.StandardTimeGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return time.Date(2001, 4, 30, 15, 0, 0, 0, time.Local), nil | ||
}, | ||
}, | ||
expected: time.Date(2001, 4, 30, 15, 0, 0, 0, time.Local), | ||
}, | ||
{ | ||
name: "November 12, 1980, 4:35:01am", | ||
time: &ottl.StandardTimeGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return time.Date(1980, 11, 12, 4, 35, 1, 0, time.Local), nil | ||
}, | ||
}, | ||
expected: time.Date(1980, 11, 12, 4, 35, 1, 0, time.Local), | ||
}, | ||
{ | ||
name: "October 4, 2020, 5:05 5 microseconds 5 nanosecs", | ||
time: &ottl.StandardTimeGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return time.Date(2020, 10, 4, 5, 5, 5, 5, time.Local), nil | ||
}, | ||
}, | ||
expected: time.Date(2020, 10, 4, 5, 5, 5, 5, time.Local), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc, err := UnixMicro(tt.time) | ||
assert.NoError(t, err) | ||
result, err := exprFunc(nil, nil) | ||
assert.NoError(t, err) | ||
want := tt.expected.UnixMicro() | ||
assert.Equal(t, want, result) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type UnixMilliArguments[K any] struct { | ||
Time ottl.TimeGetter[K] `ottlarg:"0"` | ||
} | ||
|
||
func NewUnixMilliFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("UnixMilli", &UnixMilliArguments[K]{}, createUnixMilliFunction[K]) | ||
} | ||
func createUnixMilliFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*UnixMilliArguments[K]) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("UnixMilliFactory args must be of type *UnixMilliArguments[K]") | ||
} | ||
|
||
return UnixMilli(args.Time) | ||
} | ||
|
||
func UnixMilli[K any](inputTime ottl.TimeGetter[K]) (ottl.ExprFunc[K], error) { | ||
return func(ctx context.Context, tCtx K) (interface{}, error) { | ||
t, err := inputTime.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return t.UnixMilli(), nil | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
func Test_TimeUnixMilli(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
time ottl.TimeGetter[interface{}] | ||
expected time.Time | ||
}{ | ||
{ | ||
name: "January 1, 2022", | ||
time: &ottl.StandardTimeGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return time.Date(2022, 1, 1, 0, 0, 0, 0, time.Local), nil | ||
}, | ||
}, | ||
expected: time.Date(2022, 1, 1, 0, 0, 0, 0, time.Local), | ||
}, | ||
{ | ||
name: "May 30, 2002, 3pm", | ||
time: &ottl.StandardTimeGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return time.Date(2002, 5, 30, 15, 0, 0, 0, time.Local), nil | ||
}, | ||
}, | ||
expected: time.Date(2002, 5, 30, 15, 0, 0, 0, time.Local), | ||
}, | ||
{ | ||
name: "September 12, 1980, 4:35:01am", | ||
time: &ottl.StandardTimeGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return time.Date(1980, 9, 12, 4, 35, 1, 0, time.Local), nil | ||
}, | ||
}, | ||
expected: time.Date(1980, 9, 12, 4, 35, 1, 0, time.Local), | ||
}, | ||
{ | ||
name: "October 4, 2020, 5:05 5 microseconds 5 nanosecs", | ||
time: &ottl.StandardTimeGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return time.Date(2020, 10, 4, 5, 5, 5, 5, time.Local), nil | ||
}, | ||
}, | ||
expected: time.Date(2020, 10, 4, 5, 5, 5, 5, time.Local), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc, err := UnixMilli(tt.time) | ||
assert.NoError(t, err) | ||
result, err := exprFunc(nil, nil) | ||
assert.NoError(t, err) | ||
want := tt.expected.UnixMilli() | ||
assert.Equal(t, want, result) | ||
}) | ||
} | ||
} |
Oops, something went wrong.