Skip to content

Commit

Permalink
Normalize times to common.Time with UTC time zone (elastic#4658)
Browse files Browse the repository at this point in the history
During event normalization convert any time objects to a common.Time and ensure that the time zone is UTC.

Fixes elastic#4569
(cherry picked from commit 29f4cc7)
  • Loading branch information
andrewkroh committed Jul 17, 2017
1 parent 85fa363 commit 943b960
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ https://github.com/elastic/beats/compare/v5.5.0...master[Check the HEAD diff]

*Affecting all Beats*

- 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*

*Heartbeat*
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

0 comments on commit 943b960

Please sign in to comment.