diff --git a/docs/clients/promtail/configuration.md b/docs/clients/promtail/configuration.md index 5e4ad035f6691..1d042e824e4b4 100644 --- a/docs/clients/promtail/configuration.md +++ b/docs/clients/promtail/configuration.md @@ -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: # IANA Timezone Database string. diff --git a/docs/clients/promtail/stages/timestamp.md b/docs/clients/promtail/stages/timestamp.md index aea6eb24a8aa6..9aac07abb1b01 100644 --- a/docs/clients/promtail/stages/timestamp.md +++ b/docs/clients/promtail/stages/timestamp.md @@ -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: # IANA Timezone Database string. @@ -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 diff --git a/pkg/logentry/stages/timestamp.go b/pkg/logentry/stages/timestamp.go index b6427e0d79aa7..e77b82a7aa41d 100644 --- a/pkg/logentry/stages/timestamp.go +++ b/pkg/logentry/stages/timestamp.go @@ -26,6 +26,7 @@ const ( Unix = "Unix" UnixMs = "UnixMs" + UnixUs = "UnixUs" UnixNs = "UnixNs" TimestampActionOnFailureSkip = "skip" diff --git a/pkg/logentry/stages/timestamp_test.go b/pkg/logentry/stages/timestamp_test.go index 9750995ae86da..cc30d5206227f 100644 --- a/pkg/logentry/stages/timestamp_test.go +++ b/pkg/logentry/stages/timestamp_test.go @@ -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", diff --git a/pkg/logentry/stages/util.go b/pkg/logentry/stages/util.go index 14d474abc609e..bd4eac044fdbc 100644 --- a/pkg/logentry/stages/util.go +++ b/pkg/logentry/stages/util.go @@ -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)