-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logparser): add generic json parser
Updates: #243
- Loading branch information
Showing
9 changed files
with
573 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"severity_number_str": "Info", | ||
"severity_text": "info", | ||
"body": "Events", | ||
"timestamp": "2023-12-04T09:11:59.1613184Z", | ||
"logger": "poll", | ||
"caller": "gh-archived/main.go:197", | ||
"duration": 0.437301455, | ||
"integers": [ | ||
1, | ||
2, | ||
3 | ||
], | ||
"new_count": 100, | ||
"remaining": 1040, | ||
"used": 3960, | ||
"reset": 660.838704106, | ||
"reset_human": "in 12 minutes", | ||
"sleep": 0.198120375, | ||
"target_rate": 0.63542183 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"level":"info","ts":1701681119.1613183,"logger":"poll","caller":"gh-archived/main.go:197","msg":"Events","duration":0.437301455,"integers":[1, 2, 3],"new_count":100,"remaining":1040,"used":3960,"reset":660.838704106,"reset_human":"in 12 minutes","sleep":0.198120375,"target_rate":0.63542183} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package logparser | ||
|
||
import "time" | ||
|
||
// ISO8601Millis time format with millisecond precision. | ||
const ISO8601Millis = "2006-01-02T15:04:05.000Z0700" | ||
|
||
// we are not expecting logs from past. | ||
var deductStart = time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) | ||
|
||
// deductNanos returns unix nano from arbitrary time integer, deducting resolution by range. | ||
func deductNanos(n int64) (int64, bool) { | ||
if n > deductStart.UnixNano() { | ||
return n, true | ||
} | ||
if n > deductStart.UnixMicro() { | ||
return n * 1e3, true | ||
} | ||
if n > deductStart.UnixMilli() { | ||
return n * 1e6, true | ||
} | ||
if n > deductStart.Unix() { | ||
return n * 1e9, true | ||
} | ||
return 0, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package logparser | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDeductNanos(t *testing.T) { | ||
var ( | ||
start = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) | ||
end = time.Date(2200, 1, 1, 0, 0, 0, 0, time.UTC) | ||
) | ||
truncate := func(nanos int64, zeroes int) int64 { | ||
d := int64(math.Pow10(zeroes)) | ||
return nanos - nanos%d | ||
} | ||
assert := func(a, b int64, msgAndArgs ...interface{}) { | ||
t.Helper() | ||
v, ok := deductNanos(a) | ||
require.True(t, ok, msgAndArgs...) | ||
require.Equal(t, b, v, msgAndArgs...) | ||
} | ||
for v := start; v.Before(end); v = v.Add(time.Second*44 + time.Nanosecond*1337123 + time.Hour*6) { | ||
expected := v.UnixNano() | ||
assert(v.Unix(), truncate(expected, 9), "v=%v", v) | ||
assert(v.UnixMilli(), truncate(expected, 6), "v=%v", v) | ||
assert(v.UnixMicro(), truncate(expected, 3), "v=%v", v) | ||
assert(v.UnixNano(), expected, "v=%v", v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package logparser | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/go-faster/sdk/gold" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
// Explicitly registering flags for golden files. | ||
gold.Init() | ||
|
||
os.Exit(m.Run()) | ||
} |
Oops, something went wrong.