Skip to content

Commit

Permalink
feat(logparser): add generic json parser
Browse files Browse the repository at this point in the history
Updates: #243
  • Loading branch information
ernado committed Dec 4, 2023
1 parent f56cb2a commit cf2ecae
Show file tree
Hide file tree
Showing 9 changed files with 573 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/logparser/_golden/genericjson_01.json
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
}
1 change: 1 addition & 0 deletions internal/logparser/_testdata/genericjson/zap.jsonl
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}
26 changes: 26 additions & 0 deletions internal/logparser/deduct.go
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
}
33 changes: 33 additions & 0 deletions internal/logparser/deduct_test.go
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)
}
}
15 changes: 15 additions & 0 deletions internal/logparser/gold_test.go
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())
}
Loading

0 comments on commit cf2ecae

Please sign in to comment.