Skip to content

Commit

Permalink
Cherry-pick elastic#4658 to 5.x: Normalize times to common.Time with …
Browse files Browse the repository at this point in the history
…UTC time zone (elastic#4684)

* Normalize times to common.Time with UTC time zone (elastic#4658)

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)

* Fix changelog
  • Loading branch information
andrewkroh authored and tsg committed Jul 18, 2017
1 parent d19b1c9 commit 884c7e8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ https://github.com/elastic/beats/compare/v5.4.1...master[Check the HEAD diff]
- Fix console output {pull}4045[4045]

- Usage of field `_type` is now ignored and hardcoded to `doc`. {pull}3757[3757]
- Normalize all times to UTC to ensure proper index naming. {issue}4569[4569]

*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

0 comments on commit 884c7e8

Please sign in to comment.