-
Notifications
You must be signed in to change notification settings - Fork 53
/
json_handler_test.go
236 lines (201 loc) · 7.16 KB
/
json_handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package humanlog
import (
"fmt"
"testing"
"time"
"github.com/google/go-cmp/cmp"
typesv1 "github.com/humanlogio/api/go/types/v1"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/known/timestamppb"
)
func TestJSONHandler_UnmarshalJSON_ParsesFields(t *testing.T) {
msg := `The service is running on port 8080.`
level := `info`
timeFormat := time.RFC3339Nano
tm, err := time.Parse(timeFormat, "2012-11-01T22:08:41+00:00")
if err != nil {
t.Fatalf("failed to parse time: %v", err)
}
raw := []byte(fmt.Sprintf(`{ "message": %q, "level": %q, "time": %q }`, msg, level, tm))
opts := DefaultOptions()
h := JSONHandler{Opts: opts}
ev := new(typesv1.StructuredLogEvent)
if !h.TryHandle(raw, ev) {
t.Fatalf("failed to parse log level")
}
if h.Level != level {
t.Fatalf("not equal: expected %q, got %q", level, h.Level)
}
if h.Message != msg {
t.Fatalf("not equal: expected %q, got %q", msg, h.Message)
}
if !h.Time.Equal(tm) {
t.Fatalf("not equal: expected %q, got %q", tm, h.Time)
}
}
func TestJSONHandler_UnmarshalJSON_ParsesCustomFields(t *testing.T) {
msg := `The service is running on port 8080.`
level := `info`
timeFormat := time.RFC3339Nano
tm, err := time.Parse(timeFormat, "2012-11-01T22:08:41+00:00")
if err != nil {
t.Fatalf("failed to parse time: %v", err)
}
raw := []byte(fmt.Sprintf(`{ "mymessage": %q, "mylevel": %q, "mytime": %q }`, msg, level, tm))
opts := DefaultOptions()
opts.LevelFields = []string{"mylevel"}
opts.MessageFields = []string{"mymessage"}
opts.TimeFields = []string{"mytime"}
h := JSONHandler{Opts: opts}
ev := new(typesv1.StructuredLogEvent)
if !h.TryHandle(raw, ev) {
t.Fatalf("failed to parse log level")
}
if h.Level != level {
t.Fatalf("not equal: expected %q, got %q", level, h.Level)
}
if h.Message != msg {
t.Fatalf("not equal: expected %q, got %q", msg, h.Message)
}
if !h.Time.Equal(tm) {
t.Fatalf("not equal: expected %q, got %q", tm, h.Time)
}
}
func TestJSONHandler_UnmarshalJSON_ParsesCustomNestedFields(t *testing.T) {
msg := `The service is running on port 8080.`
level := `info`
timeFormat := time.RFC3339Nano
tm, err := time.Parse(timeFormat, "2012-11-01T22:08:41+00:00")
if err != nil {
t.Fatalf("failed to parse time: %v", err)
}
raw := []byte(fmt.Sprintf(`{ "data": { "message": %q, "level": %q, "time": %q }}`, msg, level, tm))
opts := DefaultOptions()
opts.LevelFields = []string{"data.level"}
opts.MessageFields = []string{"data.message"}
opts.TimeFields = []string{"data.time"}
h := JSONHandler{Opts: opts}
ev := new(typesv1.StructuredLogEvent)
if !h.TryHandle(raw, ev) {
t.Fatalf("failed to handle log")
}
if h.Level != level {
t.Fatalf("not equal: expected %q, got %q", level, h.Level)
}
if h.Message != msg {
t.Fatalf("not equal: expected %q, got %q", msg, h.Message)
}
if !h.Time.Equal(tm) {
t.Fatalf("not equal: expected %q, got %q", tm, h.Time)
}
}
func TestJSONHandler_UnmarshalJSON_ParsesCustomMultiNestedFields(t *testing.T) {
msg := `The service is running on port 8080.`
level := `info`
timeFormat := time.RFC3339Nano
tm, err := time.Parse(timeFormat, "2012-11-01T22:08:41+00:00")
if err != nil {
t.Fatalf("failed to parse time: %v", err)
}
raw := []byte(fmt.Sprintf(`{
"data": {
"l2": {
"message": %q,
"level": %q,
"time": %q
}
}
}`, msg, level, tm))
opts := DefaultOptions()
opts.LevelFields = []string{"data.l2.level"}
opts.MessageFields = []string{"data.l2.message"}
opts.TimeFields = []string{"data.l2.time"}
h := JSONHandler{Opts: opts}
ev := new(typesv1.StructuredLogEvent)
if !h.TryHandle(raw, ev) {
t.Fatalf("failed to handle log")
}
if h.Level != level {
t.Fatalf("not equal: expected %q, got %q", level, h.Level)
}
if h.Message != msg {
t.Fatalf("not equal: expected %q, got %q", msg, h.Message)
}
if !h.Time.Equal(tm) {
t.Fatalf("not equal: expected %q, got %q", tm, h.Time)
}
}
func TestJsonHandler_TryHandle_LargeNumbers(t *testing.T) {
h := JSONHandler{Opts: DefaultOptions()}
ev := new(typesv1.StructuredLogEvent)
raw := []byte(`{"storage":{"session.id":1730187806608637000, "some": {"float": 1.2345}}}`)
if !h.TryHandle(raw, ev) {
t.Fatalf("failed to handle log")
}
require.Equal(t, 1.2345, h.Fields["storage.some.float"].GetF64())
require.Equal(t, int64(1730187806608637000), h.Fields["storage.session.id"].GetI64())
}
func TestJsonHandler_TryHandle_FlattendArrayFields(t *testing.T) {
handler := JSONHandler{Opts: DefaultOptions()}
ev := new(typesv1.StructuredLogEvent)
raw := []byte(`{"peers":[{"ID":"10.244.0.126:8083","URI":"10.244.0.126:8083"},{"ID":"10.244.0.206:8083","URI":"10.244.0.206:8083"},{"ID":"10.244.1.150:8083","URI":"10.244.1.150:8083"}],"storage":{"session.id":1730187806608637000, "some": {"float": 1.2345}}}`)
if !handler.TryHandle(raw, ev) {
t.Fatalf("failed to handle log")
}
require.Equal(t, "10.244.0.126:8083", handler.Fields["peers.0.ID"].GetStr())
require.Equal(t, "10.244.0.126:8083", handler.Fields["peers.0.URI"].GetStr())
require.Equal(t, "10.244.0.206:8083", handler.Fields["peers.1.ID"].GetStr())
require.Equal(t, "10.244.0.206:8083", handler.Fields["peers.1.URI"].GetStr())
require.Equal(t, "10.244.1.150:8083", handler.Fields["peers.2.ID"].GetStr())
require.Equal(t, "10.244.1.150:8083", handler.Fields["peers.2.URI"].GetStr())
}
func TestJsonHandler_TryHandle_FlattenedArrayFields_NestedArray(t *testing.T) {
handler := JSONHandler{Opts: DefaultOptions()}
ev := new(typesv1.StructuredLogEvent)
raw := []byte(`{"peers":[[1,2,3.14],[4,50.55,[6,7]],["hello","world"],{"foo":"bar"}]}`)
if !handler.TryHandle(raw, ev) {
t.Fatalf("failed to handle log")
}
require.Equal(t, int64(1), handler.Fields["peers.0.0"].GetI64())
require.Equal(t, int64(2), handler.Fields["peers.0.1"].GetI64())
require.Equal(t, float64(3.14), handler.Fields["peers.0.2"].GetF64())
require.Equal(t, int64(4), handler.Fields["peers.1.0"].GetI64())
require.Equal(t, float64(50.55), handler.Fields["peers.1.1"].GetF64())
require.Equal(t, int64(6), handler.Fields["peers.1.2.0"].GetI64())
require.Equal(t, int64(7), handler.Fields["peers.1.2.1"].GetI64())
require.Equal(t, "hello", handler.Fields["peers.2.0"].GetStr())
require.Equal(t, "world", handler.Fields["peers.2.1"].GetStr())
require.Equal(t, "bar", handler.Fields["peers.3.foo"].GetStr())
}
func TestParseAsctimeFields(t *testing.T) {
tests := []struct {
name string
raw []byte
want *timestamppb.Timestamp
}{
{
name: "asctime",
raw: []byte(`{"asctime": ["12-05-05 22:11:08,506248"]}`),
want: timestamppb.New(time.Date(2012, 5, 5, 22, 11, 8, 506248000, time.UTC)),
},
{
name: "time",
raw: []byte(`{"time": "12-05-05 22:11:08,506248"}`),
want: timestamppb.New(time.Date(2012, 5, 5, 22, 11, 8, 506248000, time.UTC)),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
opts := DefaultOptions()
h := JSONHandler{Opts: opts}
ev := new(typesv1.StructuredLogEvent)
if !h.TryHandle(test.raw, ev) {
t.Fatalf("failed to handle log")
}
// timezone should be identified before parsing... we can't just treat as UTC
got := ev.Timestamp
require.Empty(t, cmp.Diff(test.want, got, protocmp.Transform()))
})
}
}