-
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.
[pkg/ottl] add time converter function (#22811)
Description: Added a converter function that takes a time string and a format string and converts to a golang time.Time object. Link to tracking Issue: #22007 Testing: Unit tests to check proper conversion from `stringGetter` to golang `time.Time` Documentation: --------- Co-authored-by: Tyler Helmuth <[email protected]>
- Loading branch information
1 parent
5f9577b
commit e5ccd1f
Showing
20 changed files
with
336 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: "Adds new `Time` converter to convert a string to a Golang time struct based on a supplied format" | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [22007] | ||
|
||
# (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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,55 @@ | ||
// 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/internal/coreinternal/timeutils" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type TimeArguments[K any] struct { | ||
Time ottl.StringGetter[K] `ottlarg:"0"` | ||
Format string `ottlarg:"1"` | ||
} | ||
|
||
func NewTimeFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("Time", &TimeArguments[K]{}, createTimeFunction[K]) | ||
} | ||
func createTimeFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*TimeArguments[K]) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("TimeFactory args must be of type *TimeArguments[K]") | ||
} | ||
|
||
return Time(args.Time, args.Format) | ||
} | ||
|
||
func Time[K any](inputTime ottl.StringGetter[K], format string) (ottl.ExprFunc[K], error) { | ||
if format == "" { | ||
return nil, fmt.Errorf("format cannot be nil") | ||
} | ||
loc, err := timeutils.GetLocation(nil, &format) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return func(ctx context.Context, tCtx K) (interface{}, error) { | ||
t, err := inputTime.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if t == "" { | ||
return nil, fmt.Errorf("time cannot be nil") | ||
} | ||
timestamp, err := timeutils.ParseStrptime(format, t, loc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return timestamp, 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,228 @@ | ||
// 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_Time(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
time ottl.StringGetter[interface{}] | ||
format string | ||
expected time.Time | ||
}{ | ||
{ | ||
name: "simple short form", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "2023-04-12", nil | ||
}, | ||
}, | ||
format: "%Y-%m-%d", | ||
expected: time.Date(2023, 4, 12, 0, 0, 0, 0, time.Local), | ||
}, | ||
{ | ||
name: "simple short form with short year and slashes", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "11/11/11", nil | ||
}, | ||
}, | ||
format: "%d/%m/%y", | ||
expected: time.Date(2011, 11, 11, 0, 0, 0, 0, time.Local), | ||
}, | ||
{ | ||
name: "month day year", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "02/04/2023", nil | ||
}, | ||
}, | ||
format: "%m/%d/%Y", | ||
expected: time.Date(2023, 2, 4, 0, 0, 0, 0, time.Local), | ||
}, | ||
{ | ||
name: "simple long form", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "July 31, 1993", nil | ||
}, | ||
}, | ||
format: "%B %d, %Y", | ||
expected: time.Date(1993, 7, 31, 0, 0, 0, 0, time.Local), | ||
}, | ||
{ | ||
name: "date with timestamp", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "Mar 14 2023 17:02:59", nil | ||
}, | ||
}, | ||
format: "%b %d %Y %H:%M:%S", | ||
expected: time.Date(2023, 3, 14, 17, 02, 59, 0, time.Local), | ||
}, | ||
{ | ||
name: "day of the week long form", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "Monday, May 01, 2023", nil | ||
}, | ||
}, | ||
format: "%A, %B %d, %Y", | ||
expected: time.Date(2023, 5, 1, 0, 0, 0, 0, time.Local), | ||
}, | ||
{ | ||
name: "short weekday, short month, long format", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "Sat, May 20, 2023", nil | ||
}, | ||
}, | ||
format: "%a, %b %d, %Y", | ||
expected: time.Date(2023, 5, 20, 0, 0, 0, 0, time.Local), | ||
}, | ||
{ | ||
name: "short months", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "Feb 15, 2023", nil | ||
}, | ||
}, | ||
format: "%b %d, %Y", | ||
expected: time.Date(2023, 2, 15, 0, 0, 0, 0, time.Local), | ||
}, | ||
{ | ||
name: "timestamp with time zone offset", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "2023-05-26 12:34:56 HST", nil | ||
}, | ||
}, | ||
format: "%Y-%m-%d %H:%M:%S %Z", | ||
expected: time.Date(2023, 5, 26, 12, 34, 56, 0, time.FixedZone("HST", -10*60*60)), | ||
}, | ||
{ | ||
name: "short date with timestamp without time zone offset", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "2023-05-26T12:34:56 GMT", nil | ||
}, | ||
}, | ||
format: "%Y-%m-%dT%H:%M:%S %Z", | ||
expected: time.Date(2023, 5, 26, 12, 34, 56, 0, time.FixedZone("GMT", 0)), | ||
}, | ||
{ | ||
name: "RFC 3339 in custom format", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "2012-11-01T22:08:41+0000 EST", nil | ||
}, | ||
}, | ||
format: "%Y-%m-%dT%H:%M:%S%z %Z", | ||
expected: time.Date(2012, 11, 01, 22, 8, 41, 0, time.FixedZone("EST", 0)), | ||
}, | ||
{ | ||
name: "RFC 3339 in custom format before 2000", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "1986-10-01T00:17:33 MST", nil | ||
}, | ||
}, | ||
format: "%Y-%m-%dT%H:%M:%S %Z", | ||
expected: time.Date(1986, 10, 01, 00, 17, 33, 00, time.FixedZone("MST", -7*60*60)), | ||
}, | ||
{ | ||
name: "no location", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "2022/01/01", nil | ||
}, | ||
}, | ||
format: "%Y/%m/%d", | ||
expected: time.Date(2022, 01, 01, 0, 0, 0, 0, time.Local), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc, err := Time(tt.time, tt.format) | ||
assert.NoError(t, err) | ||
result, err := exprFunc(nil, nil) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expected.UnixNano(), result.(time.Time).UnixNano()) | ||
}) | ||
} | ||
} | ||
|
||
func Test_TimeError(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
time ottl.StringGetter[interface{}] | ||
format string | ||
expectedError string | ||
}{ | ||
{ | ||
name: "invalid short format", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "11/11/11", nil | ||
}, | ||
}, | ||
format: "%Y/%m/%d", | ||
expectedError: "cannot parse", | ||
}, | ||
{ | ||
name: "invalid RFC3339 with no time", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "", nil | ||
}, | ||
}, | ||
format: "%Y-%m-%dT%H:%M:%S", | ||
expectedError: "time cannot be nil", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc, err := Time[any](tt.time, tt.format) | ||
require.NoError(t, err) | ||
_, err = exprFunc(context.Background(), nil) | ||
assert.ErrorContains(t, err, tt.expectedError) | ||
}) | ||
} | ||
} | ||
|
||
func Test_TimeFormatError(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
time ottl.StringGetter[interface{}] | ||
format string | ||
expectedError string | ||
}{ | ||
{ | ||
name: "invalid short with no format", | ||
time: &ottl.StandardStringGetter[interface{}]{ | ||
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) { | ||
return "11/11/11", nil | ||
}, | ||
}, | ||
format: "", | ||
expectedError: "format cannot be nil", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := Time[any](tt.time, tt.format) | ||
assert.ErrorContains(t, err, tt.expectedError) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.