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

Normalize times to common.Time with UTC time zone #4658

Merged
merged 2 commits into from
Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ https://github.com/elastic/beats/compare/v6.0.0-alpha2...master[Check the HEAD d

- Don't stop with error loading the ES template if the ES output is not enabled. {pull}4436[4436]
- Fix race condition in internal logging rotator. {pull}4519[4519]
- Normalize all times to UTC to ensure proper index naming. {issue}4569[4569]
- Fix issue with loading dashboards to ES 6.0 when .kibana index did not already exist. {issue}4659[4659]

*Filebeat*
Expand Down
21 changes: 21 additions & 0 deletions libbeat/common/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"strconv"
"strings"
"time"

"github.com/elastic/beats/libbeat/logp"

Expand Down Expand Up @@ -118,6 +119,26 @@ func normalizeValue(value interface{}, keys ...string) (interface{}, []error) {
return nil, nil
}

// Normalize time values to a common.Time with UTC time zone.
switch v := value.(type) {
case time.Time:
value = Time(v.UTC())
case []time.Time:
times := make([]Time, 0, len(v))
for _, t := range v {
times = append(times, Time(t.UTC()))
}
value = times
case Time:
value = Time(time.Time(v).UTC())
case []Time:
times := make([]Time, 0, len(v))
for _, t := range v {
times = append(times, Time(time.Time(t).UTC()))
}
value = times
}

switch value.(type) {
case encoding.TextMarshaler:
text, err := value.(encoding.TextMarshaler).MarshalText()
Expand Down
22 changes: 22 additions & 0 deletions libbeat/common/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"encoding/json"
"testing"
"time"

"github.com/elastic/beats/libbeat/logp"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -315,6 +316,27 @@ func TestMarshalFloatValues(t *testing.T) {
assert.Equal(string(b), "{\"f\":5.000000}")
}

func TestNormalizeTime(t *testing.T) {
ny, err := time.LoadLocation("America/New_York")
if err != nil {
t.Fatal(err)
}

now := time.Now().In(ny)
v, errs := normalizeValue(now, "@timestamp")
if len(errs) > 0 {
t.Fatal(errs)
}

utcCommonTime, ok := v.(Time)
if !ok {
t.Fatalf("expected common.Time, but got %T (%v)", v, v)
}

assert.Equal(t, time.UTC, time.Time(utcCommonTime).Location())
assert.True(t, now.Equal(time.Time(utcCommonTime)))
}

// Uses TextMarshaler interface.
func BenchmarkConvertToGenericEventNetString(b *testing.B) {
for i := 0; i < b.N; i++ {
Expand Down