-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/ottl] introduce Timestamp() converter function
Signed-off-by: odubajDT <[email protected]>
- Loading branch information
Showing
6 changed files
with
310 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,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# 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: "Introduce new Timestamp() converter function." | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [36870] | ||
|
||
# (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: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
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,51 @@ | ||
// 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 TimestampArguments[K any] struct { | ||
Time ottl.TimeGetter[K] | ||
Format string | ||
} | ||
|
||
func NewTimestampFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("Timestamp", &TimestampArguments[K]{}, createTimestampFunction[K]) | ||
} | ||
|
||
func createTimestampFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*TimestampArguments[K]) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("TimestampFactory args must be of type *TimestampArguments[K]") | ||
} | ||
|
||
return Timestamp(args.Time, args.Format) | ||
} | ||
|
||
func Timestamp[K any](timeValue ottl.TimeGetter[K], format string) (ottl.ExprFunc[K], error) { | ||
if format == "" { | ||
return nil, fmt.Errorf("format cannot be nil") | ||
} | ||
|
||
gotimeFormat, err := timeutils.StrptimeToGotime(format) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return func(ctx context.Context, tCtx K) (any, error) { | ||
t, err := timeValue.Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return t.Format(gotimeFormat), 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,169 @@ | ||
// 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_Timestamp(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
time ottl.TimeGetter[any] | ||
format string | ||
expected string | ||
errorMsg string | ||
funcErrorMsg string | ||
}{ | ||
{ | ||
name: "empty format", | ||
time: &ottl.StandardTimeGetter[any]{}, | ||
format: "", | ||
errorMsg: "format cannot be nil", | ||
}, | ||
{ | ||
name: "invalid time", | ||
time: &ottl.StandardTimeGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return "something", nil | ||
}, | ||
}, | ||
format: "%Y-%m-%d", | ||
funcErrorMsg: "expected time but got string", | ||
}, | ||
{ | ||
name: "simple short form", | ||
time: &ottl.StandardTimeGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return time.Date(2023, 4, 12, 0, 0, 0, 0, time.Local), nil | ||
}, | ||
}, | ||
format: "%Y-%m-%d", | ||
expected: "2023-04-12", | ||
}, | ||
{ | ||
name: "simple short form with short year and slashes", | ||
time: &ottl.StandardTimeGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return time.Date(2011, 11, 11, 0, 0, 0, 0, time.Local), nil | ||
}, | ||
}, | ||
format: "%d/%m/%y", | ||
expected: "11/11/11", | ||
}, | ||
{ | ||
name: "month day year", | ||
time: &ottl.StandardTimeGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return time.Date(2023, 2, 4, 0, 0, 0, 0, time.Local), nil | ||
}, | ||
}, | ||
format: "%m/%d/%Y", | ||
expected: "02/04/2023", | ||
}, | ||
{ | ||
name: "simple long form", | ||
time: &ottl.StandardTimeGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return time.Date(1993, 7, 31, 0, 0, 0, 0, time.Local), nil | ||
}, | ||
}, | ||
format: "%B %d, %Y", | ||
expected: "July 31, 1993", | ||
}, | ||
{ | ||
name: "date with timestamp", | ||
time: &ottl.StandardTimeGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return time.Date(2023, 3, 14, 17, 0o2, 59, 0, time.Local), nil | ||
}, | ||
}, | ||
format: "%b %d %Y %H:%M:%S", | ||
expected: "Mar 14 2023 17:02:59", | ||
}, | ||
{ | ||
name: "day of the week long form", | ||
time: &ottl.StandardTimeGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return time.Date(2023, 5, 1, 0, 0, 0, 0, time.Local), nil | ||
}, | ||
}, | ||
format: "%A, %B %d, %Y", | ||
expected: "Monday, May 01, 2023", | ||
}, | ||
{ | ||
name: "short weekday, short month, long format", | ||
time: &ottl.StandardTimeGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return time.Date(2023, 5, 20, 0, 0, 0, 0, time.Local), nil | ||
}, | ||
}, | ||
format: "%a, %b %d, %Y", | ||
expected: "Sat, May 20, 2023", | ||
}, | ||
{ | ||
name: "short months", | ||
time: &ottl.StandardTimeGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return time.Date(2023, 2, 15, 0, 0, 0, 0, time.Local), nil | ||
}, | ||
}, | ||
format: "%b %d, %Y", | ||
expected: "Feb 15, 2023", | ||
}, | ||
{ | ||
name: "simple short form with time", | ||
time: &ottl.StandardTimeGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return time.Date(2023, 5, 26, 12, 34, 56, 0, time.Local), nil | ||
}, | ||
}, | ||
format: "%Y-%m-%d %H:%M:%S", | ||
expected: "2023-05-26 12:34:56", | ||
}, | ||
{ | ||
name: "RFC 3339 in custom format", | ||
time: &ottl.StandardTimeGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return time.Date(2012, 11, 0o1, 22, 8, 41, 0, time.Local), nil | ||
}, | ||
}, | ||
format: "%Y-%m-%dT%H:%M:%S", | ||
expected: "2012-11-01T22:08:41", | ||
}, | ||
{ | ||
name: "RFC 3339 in custom format before 2000", | ||
time: &ottl.StandardTimeGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return time.Date(1986, 10, 0o1, 0o0, 17, 33, 0o0, time.Local), nil | ||
}, | ||
}, | ||
format: "%Y-%m-%dT%H:%M:%S", | ||
expected: "1986-10-01T00:17:33", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc, err := Timestamp(tt.time, tt.format) | ||
if tt.errorMsg != "" { | ||
assert.Contains(t, err.Error(), tt.errorMsg) | ||
} else { | ||
assert.NoError(t, err) | ||
result, err := exprFunc(nil, nil) | ||
if tt.funcErrorMsg != "" { | ||
assert.Contains(t, err.Error(), tt.funcErrorMsg) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expected, 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