Skip to content

Commit

Permalink
@timestamp doesnt get printed when specfied in message codec (#3721) (#…
Browse files Browse the repository at this point in the history
…4118)

(cherry picked from commit 715593c)
  • Loading branch information
Steffen Siering authored and ruflin committed Apr 26, 2017
1 parent 8a8635c commit 953cdc3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ https://github.com/elastic/beats/compare/v5.3.1...v5.4.0[View commits]
- Improve error message when downloading the dashboards fails. {pull}3805[3805]
- Fix potential Elasticsearch output URL parsing error if protocol scheme is missing. {pull}3671[3671]
- Downgrade Elasticsearch per batch item failure log to debug level. {issue}3953[3953]
- Make `@timestamp` accessible from format strings. {pull}3721[3721]
*Filebeat*
Expand Down
5 changes: 5 additions & 0 deletions libbeat/common/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ func ParseTime(timespec string) (Time, error) {
return Time(t), err
}

func (t Time) String() string {
return time.Time(t).Format(TsLayout)
}

// MustParseTime is a convenience equivalent of the ParseTime function
// that panics in case of errors.
func MustParseTime(timespec string) Time {
ts, err := ParseTime(timespec)
if err != nil {
panic(err)
}

return ts
}
3 changes: 3 additions & 0 deletions libbeat/common/fmtstr/formatevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ func fieldString(event common.MapStr, field string) (string, error) {
if err != nil {
logp.Warn("Can not convert key '%v' value to string", v)
}

return s, err
}

Expand All @@ -419,6 +420,8 @@ func tryConvString(v interface{}) (string, error) {
switch s := v.(type) {
case string:
return s, nil
case common.Time:
return s.String(), nil
case []byte:
return string(s), nil
case stringer:
Expand Down
12 changes: 12 additions & 0 deletions libbeat/common/fmtstr/formatevents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ func TestEventFormatString(t *testing.T) {
"timestamp: 2015.05.01",
[]string{"key"},
},
{
"test timestamp formatter",
"%{[@timestamp]}: %{+YYYY.MM.dd}",
common.MapStr{
"@timestamp": common.Time(
time.Date(2015, 5, 1, 20, 12, 34, 0, time.Local),
),
"key": "timestamp",
},
"2015-05-01T20:12:34.000Z: 2015.05.01",
[]string{"@timestamp"},
},
}

for i, test := range tests {
Expand Down

0 comments on commit 953cdc3

Please sign in to comment.