Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promtail: support unix timestamps with fractional seconds #2301

Merged
merged 5 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/clients/promtail/stages/timestamp.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ of pre-defined formats to represent common forms:
Additionally, support for common Unix timestamps is supported with the following
`format` values:

- `Unix`: `1562708916`
- `Unix`: `1562708916` or with fractions `1562708916.000000123`
- `UnixMs`: `1562708916414`
- `UnixUs`: `1562708916414123`
- `UnixNs`: `1562708916000000123`
Expand Down
22 changes: 22 additions & 0 deletions pkg/logentry/stages/timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,28 @@ func TestTimestampStage_Process(t *testing.T) {
},
time.Date(2019, 7, 9, 21, 48, 36, 0, time.UTC),
},
"unix fractions ms success": {
TimestampConfig{
Source: "ts",
Format: "Unix",
},
map[string]interface{}{
"somethigelse": "notimportant",
"ts": "1562708916.414123",
},
time.Date(2019, 7, 9, 21, 48, 36, 414123*1000, time.UTC),
},
"unix fractions ns success": {
TimestampConfig{
Source: "ts",
Format: "Unix",
},
map[string]interface{}{
"somethigelse": "notimportant",
"ts": "1562708916.000000123",
},
time.Date(2019, 7, 9, 21, 48, 36, 123, time.UTC),
},
"unix millisecond success": {
TimestampConfig{
Source: "ts",
Expand Down
17 changes: 17 additions & 0 deletions pkg/logentry/stages/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stages

import (
"fmt"
"math"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -64,6 +65,22 @@ func convertDateLayout(predef string, location *time.Location) parser {
}
case "Unix":
return func(t string) (time.Time, error) {
if strings.Count(t, ".") == 1 {
split := strings.Split(t, ".")
cyriltovena marked this conversation as resolved.
Show resolved Hide resolved
if len(split) != 2 {
return time.Time{}, fmt.Errorf("can't split %v into two parts", t)
}
sec, err := strconv.ParseInt(split[0], 10, 64)
if err != nil {
return time.Time{}, err
}
frac, err := strconv.ParseInt(split[1], 10, 64)
if err != nil {
return time.Time{}, err
}
nsec := int64(float64(frac) * math.Pow(10, float64(9-len(split[1]))))
return time.Unix(sec, nsec), nil
}
i, err := strconv.ParseInt(t, 10, 64)
if err != nil {
return time.Time{}, err
Expand Down