Skip to content

Commit

Permalink
Support microsecond timestamp format (#1426)
Browse files Browse the repository at this point in the history
Adds microsecond timestamp support.

#1407
  • Loading branch information
wphan authored and cyriltovena committed Dec 19, 2019
1 parent a2c2a90 commit d627cda
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/clients/promtail/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ timestamp:
# Determines how to parse the time string. Can use
# pre-defined formats by name: [ANSIC UnixDate RubyDate RFC822
# RFC822Z RFC850 RFC1123 RFC1123Z RFC3339 RFC3339Nano Unix
# UnixMs UnixNs].
# UnixMs UnixUs UnixNs].
format: <string>
# IANA Timezone Database string.
Expand Down
3 changes: 2 additions & 1 deletion docs/clients/promtail/stages/timestamp.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ timestamp:
# Determines how to parse the time string. Can use
# pre-defined formats by name: [ANSIC UnixDate RubyDate RFC822
# RFC822Z RFC850 RFC1123 RFC1123Z RFC3339 RFC3339Nano Unix
# UnixMs UnixNs].
# UnixMs UnixUs UnixNs].
format: <string>

# IANA Timezone Database string.
Expand Down Expand Up @@ -46,6 +46,7 @@ Additionally, support for common Unix timestamps is supported with the following

- `Unix`: `1562708916`
- `UnixMs`: `1562708916414`
- `UnixUs`: `1562708916414123`
- `UnixNs`: `1562708916000000123`

Custom formats are passed directly to the layout parameter in Go's
Expand Down
1 change: 1 addition & 0 deletions pkg/logentry/stages/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (

Unix = "Unix"
UnixMs = "UnixMs"
UnixUs = "UnixUs"
UnixNs = "UnixNs"

TimestampActionOnFailureSkip = "skip"
Expand Down
11 changes: 11 additions & 0 deletions pkg/logentry/stages/timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ func TestTimestampStage_Process(t *testing.T) {
},
time.Date(2019, 7, 9, 21, 48, 36, 414*1000000, time.UTC),
},
"unix microsecond success": {
TimestampConfig{
Source: "ts",
Format: "UnixUs",
},
map[string]interface{}{
"somethigelse": "notimportant",
"ts": "1562708916414123",
},
time.Date(2019, 7, 9, 21, 48, 36, 414123*1000, time.UTC),
},
"unix nano success": {
TimestampConfig{
Source: "ts",
Expand Down
8 changes: 8 additions & 0 deletions pkg/logentry/stages/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ func convertDateLayout(predef string, location *time.Location) parser {
}
return time.Unix(0, i*int64(time.Millisecond)), nil
}
case "UnixUs":
return func(t string) (time.Time, error) {
i, err := strconv.ParseInt(t, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(0, i*int64(time.Microsecond)), nil
}
case "UnixNs":
return func(t string) (time.Time, error) {
i, err := strconv.ParseInt(t, 10, 64)
Expand Down

0 comments on commit d627cda

Please sign in to comment.