Skip to content

Commit

Permalink
Support for "ts" timestamps in JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
gholt committed May 22, 2017
1 parent 04911e1 commit e58d25e
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions json_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (h *JSONHandler) clear() {

// TryHandle tells if this line was handled by this handler.
func (h *JSONHandler) TryHandle(d []byte) bool {
if !bytes.Contains(d, []byte(`"time":`)) {
if !bytes.Contains(d, []byte(`"time":`)) && !bytes.Contains(d, []byte(`"ts":`)) {
return false
}
err := h.UnmarshalJSON(d)
Expand All @@ -62,13 +62,26 @@ func (h *JSONHandler) UnmarshalJSON(data []byte) error {
}

timeStr, ok := raw["time"].(string)
if ok {
delete(raw, "time")
} else {
timeStr, ok = raw["ts"].(string)
if ok {
delete(raw, "ts")
}
}
if ok {
h.Time, ok = tryParseTime(timeStr)
if !ok {
return fmt.Errorf("field time is not a known timestamp: %v", timeStr)
}
} else if i, iOk := raw["ts"].(int64); iOk {
h.Time = time.Unix(i, 0)
delete(raw, "ts")
} else if f, fOk := raw["ts"].(float64); fOk {
h.Time = time.Unix(int64(f), int64((f-float64(int64(f)))*1000000000))
delete(raw, "ts")
}
delete(raw, "time")
h.Message, _ = raw["msg"].(string)
delete(raw, "msg")

Expand Down

0 comments on commit e58d25e

Please sign in to comment.