Skip to content

Commit

Permalink
[pkg/ottl] add time converter function (#22811)
Browse files Browse the repository at this point in the history
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
fchikwekwe and TylerHelmuth authored Jun 22, 2023
1 parent 5f9577b commit e5ccd1f
Show file tree
Hide file tree
Showing 20 changed files with 336 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .chloggen/main.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: "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:
1 change: 1 addition & 0 deletions connector/countconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/observiq/ctimefmt v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.80.0 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0013 // indirect
Expand Down
3 changes: 3 additions & 0 deletions connector/countconnector/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/filter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/observiq/ctimefmt v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.80.0 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions internal/filter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/ottl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/observiq/ctimefmt v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.80.0 // indirect
go.opentelemetry.io/collector/confmap v0.80.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions pkg/ottl/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions pkg/ottl/ottlfuncs/func_time.go
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
}
228 changes: 228 additions & 0 deletions pkg/ottl/ottlfuncs/func_time_test.go
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)
})
}
}
1 change: 1 addition & 0 deletions pkg/ottl/ottlfuncs/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func converters[K any]() []ottl.Factory[K] {
NewSpanIDFactory[K](),
NewSplitFactory[K](),
NewSubstringFactory[K](),
NewTimeFactory[K](),
NewTraceIDFactory[K](),
NewUUIDFactory[K](),
}
Expand Down
1 change: 1 addition & 0 deletions processor/attributesprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/observiq/ctimefmt v1.0.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.80.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
Expand Down
Loading

0 comments on commit e5ccd1f

Please sign in to comment.