Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/stanza] Move core time parsing capabilities to coreinternal #23232

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replace StrptimeToGo with ParseStrptime
  • Loading branch information
TylerHelmuth committed Jun 8, 2023
commit de266b61e952d89329af166fa104aac6b580c0e5
10 changes: 7 additions & 3 deletions internal/coreinternal/timeutils/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/coreinternal/timeutils/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/stanza/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions pkg/stanza/operator/helper/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
case EpochKey:
timeValue, err := t.parseEpochTime(value)
Expand Down
Loading