Skip to content

Commit

Permalink
Improve no timezone db error (#289)
Browse files Browse the repository at this point in the history
* Wrap error from time.LoadLocation to include reason

* time zone -> timezone

* Remove timezone database suggestion
  • Loading branch information
BinaryFissionGames authored Oct 12, 2021
1 parent a54a0a1 commit a5830d6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion operator/builtin/parser/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c SyslogParserConfig) Build(context operator.BuildContext) ([]operator.Ope

location, err := time.LoadLocation(c.Location)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to load location %s: %w", c.Location, err)
}

syslogParser := &SyslogParser{
Expand Down
11 changes: 11 additions & 0 deletions operator/builtin/parser/syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/stretchr/testify/require"
"go.uber.org/zap"
"gopkg.in/yaml.v2"

"github.com/open-telemetry/opentelemetry-log-collection/entry"
Expand Down Expand Up @@ -101,3 +102,13 @@ parse_to: $.to`
require.Equal(t, expect, &actual)
})
}

func TestSyslogParserInvalidLocation(t *testing.T) {
config := NewSyslogParserConfig("test")
config.Location = "not_a_location"
config.Protocol = RFC3164

_, err := config.Build(operator.NewBuildContext(zap.NewNop().Sugar()))
require.Error(t, err)
require.Contains(t, err.Error(), "failed to load location "+config.Location)
}
4 changes: 2 additions & 2 deletions operator/helper/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (t *TimeParser) setLocation() error {
// If "location" is specified, it must be in the local timezone database
loc, err := time.LoadLocation(t.Location)
if err != nil {
return err
return fmt.Errorf("failed to load location %s: %w", t.Location, err)
}
t.location = loc
return nil
Expand Down Expand Up @@ -198,7 +198,7 @@ func (t *TimeParser) parseGotime(value interface{}) (time.Time, error) {
loc, locErr := time.LoadLocation(zone)
if locErr != nil {
// can't correct offset, just return what we have
return result, err
return result, fmt.Errorf("failed to load location %s: %w", zone, locErr)
}

// Reparse the timestamp, with the location
Expand Down
21 changes: 21 additions & 0 deletions operator/helper/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,3 +736,24 @@ func defaultTimeCfg() *TimeParser {
newCfg := NewTimeParser()
return &newCfg
}

func TestSetInvalidLocation(t *testing.T) {
tp := NewTimeParser()
tp.Location = "not_a_location"
err := tp.setLocation()
require.Error(t, err)
require.Contains(t, err.Error(), "failed to load location "+"not_a_location")
}

func TestParseGoTimeBadLocation(t *testing.T) {
tp := NewTimeParser()
tp.Location = "America/New_York"

err := tp.setLocation()
require.NoError(t, err)

tp.Layout = time.RFC822
_, err = tp.parseGotime("02 Jan 06 15:04 BST")
require.Error(t, err)
require.Contains(t, err.Error(), "failed to load location BST")
}

0 comments on commit a5830d6

Please sign in to comment.