From de266b61e952d89329af166fa104aac6b580c0e5 Mon Sep 17 00:00:00 2001 From: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com> Date: Thu, 8 Jun 2023 14:41:02 -0600 Subject: [PATCH] Replace StrptimeToGo with ParseStrptime --- internal/coreinternal/timeutils/parser.go | 10 +++++++--- internal/coreinternal/timeutils/parser_test.go | 2 +- pkg/stanza/go.mod | 2 +- pkg/stanza/operator/helper/time.go | 7 ++++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/internal/coreinternal/timeutils/parser.go b/internal/coreinternal/timeutils/parser.go index 72f9293941ad9..7645224025081 100644 --- a/internal/coreinternal/timeutils/parser.go +++ b/internal/coreinternal/timeutils/parser.go @@ -11,8 +11,12 @@ import ( strptime "github.com/observiq/ctimefmt" ) -func StrptimeToGo(layout string) (string, error) { - return strptime.ToNative(layout) +func ParseStrptime(layout string, value any, location *time.Location) (time.Time, error) { + goLayout, err := strptime.ToNative(layout) + if err != nil { + return time.Time{}, err + } + return ParseGotime(goLayout, value, location) } func GetLocation(location *string, layout *string) (*time.Location, error) { @@ -33,7 +37,7 @@ func GetLocation(location *string, layout *string) (*time.Location, error) { return time.Local, nil } -func ParseGoTime(layout string, value any, location *time.Location) (time.Time, error) { +func ParseGotime(layout string, value any, location *time.Location) (time.Time, error) { timeValue, err := parseGotime(layout, value, location) if err != nil { return time.Time{}, err diff --git a/internal/coreinternal/timeutils/parser_test.go b/internal/coreinternal/timeutils/parser_test.go index 4f8bbe34b0c46..7427a2e34d652 100644 --- a/internal/coreinternal/timeutils/parser_test.go +++ b/internal/coreinternal/timeutils/parser_test.go @@ -11,7 +11,7 @@ import ( ) func TestParseGoTimeBadLocation(t *testing.T) { - _, err := ParseGoTime(time.RFC822, "02 Jan 06 15:04 BST", time.UTC) + _, err := ParseGotime(time.RFC822, "02 Jan 06 15:04 BST", time.UTC) require.Error(t, err) require.Contains(t, err.Error(), "failed to load location BST") } diff --git a/pkg/stanza/go.mod b/pkg/stanza/go.mod index 9da61832c782e..0f34f26c9f282 100644 --- a/pkg/stanza/go.mod +++ b/pkg/stanza/go.mod @@ -9,6 +9,7 @@ require ( github.com/influxdata/go-syslog/v3 v3.0.1-0.20210608084020-ac565dc76ba6 github.com/jpillora/backoff v1.0.0 github.com/json-iterator/go v1.1.12 + github.com/observiq/ctimefmt v1.0.0 github.com/observiq/nanojack v0.0.0-20201106172433-343928847ebc github.com/open-telemetry/opentelemetry-collector-contrib/extension/storage v0.79.0 github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.79.0 @@ -42,7 +43,6 @@ 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 github.com/stretchr/objx v0.5.0 // indirect go.opencensus.io v0.24.0 // indirect diff --git a/pkg/stanza/operator/helper/time.go b/pkg/stanza/operator/helper/time.go index e348ab3a54f8b..07d672d632ac8 100644 --- a/pkg/stanza/operator/helper/time.go +++ b/pkg/stanza/operator/helper/time.go @@ -9,6 +9,7 @@ import ( "strings" "time" + strptime "github.com/observiq/ctimefmt" "go.opentelemetry.io/collector/confmap" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/timeutils" @@ -79,7 +80,7 @@ func (t *TimeParser) Validate() error { case NativeKey, GotimeKey: // ok case StrptimeKey: var err error - t.Layout, err = timeutils.StrptimeToGo(t.Layout) + t.Layout, err = strptime.ToNative(t.Layout) if err != nil { return errors.Wrap(err, "parse strptime layout") } @@ -149,11 +150,11 @@ func (t *TimeParser) Parse(entry *entry.Entry) error { } entry.Timestamp = timeutils.SetTimestampYear(timeValue) case GotimeKey: - timeValue, err := timeutils.ParseGoTime(t.Layout, value, t.location) + timeValue, err := timeutils.ParseGotime(t.Layout, value, t.location) if err != nil { return err } - // timeutils.ParseGoTime calls timeutils.SetTimestampYear before returning the timeValue + // timeutils.ParseGotime calls timeutils.SetTimestampYear before returning the timeValue entry.Timestamp = timeValue case EpochKey: timeValue, err := t.parseEpochTime(value)