-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_test.go
More file actions
373 lines (340 loc) · 12.3 KB
/
Copy pathparse_test.go
File metadata and controls
373 lines (340 loc) · 12.3 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package main
import (
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
)
func TestExtractRequestID(t *testing.T) {
tests := []struct {
name string
line string
prefix string
want string
}{
{
"START line",
"START RequestId: abc-123-def Version: $LATEST",
"START RequestId: ",
"abc-123-def",
},
{
"REPORT line",
"REPORT RequestId: abc-123-def\tDuration: 100 ms",
"REPORT RequestId: ",
"abc-123-def",
},
{
"no trailing fields",
"START RequestId: abc-123-def",
"START RequestId: ",
"abc-123-def",
},
{
"empty request id",
"START RequestId: ",
"START RequestId: ",
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := extractRequestID(tt.line, tt.prefix)
if got != tt.want {
t.Errorf("extractRequestID(%q, %q) = %q, want %q", tt.line, tt.prefix, got, tt.want)
}
})
}
}
func TestExtractField(t *testing.T) {
report := "REPORT RequestId: abc\tDuration: 123.45 ms\tBilled Duration: 200 ms\tMemory Size: 128 MB\tMax Memory Used: 85 MB"
tests := []struct {
name string
fieldName string
want string
}{
{"duration", "Duration: ", "123.45 ms"},
{"billed", "Billed Duration: ", "200 ms"},
{"memory size", "Memory Size: ", "128 MB"},
{"max memory", "Max Memory Used: ", "85 MB"},
{"missing field", "Missing: ", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := extractField(report, tt.fieldName)
if got != tt.want {
t.Errorf("extractField(report, %q) = %q, want %q", tt.fieldName, got, tt.want)
}
})
}
}
func TestNormalizeLogLine(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
"UUID collapsed",
"Processing request 550e8400-e29b-41d4-a716-446655440000 done",
"Processing request <UUID> done",
},
{
"short hex not collapsed",
"Status code: 200 abc123",
"Status code: 200 abc123",
},
{
"truncated to 100 chars",
"This is a very long log line that should be truncated because it exceeds the one hundred character limit that is enforced by the normalize function",
"This is a very long log line that should be truncated because it exceeds the one hundred character l",
},
{
"whitespace trimmed",
" some log line ",
"some log line",
},
{
"timestamp and duration collapsed",
"2026-02-25T10:00:00.123Z processed job in 123.45 ms",
"<TIMESTAMP> processed job in <DURATION>",
},
{
"ARN and numeric ID collapsed",
"published arn:aws:lambda:us-west-2:123456789012:function:my-func for customer_id=987654321 status=200",
"published <ARN> for customer_id=<NUMBER> status=200",
},
{
"request ID collapsed",
"RequestId: abc-123-def worker request failed",
"RequestId: <REQUEST_ID> worker request failed",
},
{
"JSON request ID collapsed",
`{"requestId":"abc-123-def","message":"failed"}`,
`{"requestId":"<REQUEST_ID>","message":"failed"}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := normalizeLogLine(tt.input)
if got != tt.want {
t.Errorf("normalizeLogLine(%q) = %q, want %q", tt.input, got, tt.want)
}
})
}
}
func TestCollapseHexRuns(t *testing.T) {
tests := []struct {
name string
input string
minLen int
want string
}{
{
"long hex run replaced",
"id=550e8400e29b41d4a716446655440000",
32,
"id=<UUID>",
},
{
"short hex run preserved",
"code=abcdef",
32,
"code=abcdef",
},
{
"multiple UUIDs",
"from 550e8400-e29b-41d4-a716-446655440000 to 660f9500-f30c-52e5-b827-557766551111",
32,
"from <UUID> to <UUID>",
},
{
"no hex at all",
"plain text message",
32,
"plain text message",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := collapseHexRuns(tt.input, tt.minLen, "<UUID>")
if got != tt.want {
t.Errorf("collapseHexRuns(%q, %d) = %q, want %q", tt.input, tt.minLen, got, tt.want)
}
})
}
}
func TestParseInvocations(t *testing.T) {
ts := time.Date(2026, 2, 25, 10, 0, 0, 0, time.UTC).UnixMilli()
events := []types.OutputLogEvent{
{Message: aws.String("START RequestId: req-1 Version: $LATEST"), Timestamp: aws.Int64(ts)},
{Message: aws.String("Processing item 42"), Timestamp: aws.Int64(ts + 100)},
{Message: aws.String("END RequestId: req-1"), Timestamp: aws.Int64(ts + 200)},
{Message: aws.String("REPORT RequestId: req-1\tDuration: 150.5 ms\tBilled Duration: 200 ms\tMemory Size: 128 MB\tMax Memory Used: 85 MB"), Timestamp: aws.Int64(ts + 300)},
}
got := parseInvocations(events)
if len(got) != 1 {
t.Fatalf("expected 1 invocation, got %d", len(got))
}
inv := got[0]
if inv.RequestID != "req-1" {
t.Errorf("RequestID = %q, want %q", inv.RequestID, "req-1")
}
if inv.Duration != "150.5 ms" {
t.Errorf("Duration = %q, want %q", inv.Duration, "150.5 ms")
}
if inv.MemorySizeMB != "128 MB" {
t.Errorf("MemorySizeMB = %q, want %q", inv.MemorySizeMB, "128 MB")
}
if inv.MaxMemoryUsedMB != "85 MB" {
t.Errorf("MaxMemoryUsedMB = %q, want %q", inv.MaxMemoryUsedMB, "85 MB")
}
if inv.IsError {
t.Error("expected no error")
}
if len(inv.LogLines) != 1 || inv.LogLines[0] != "Processing item 42" {
t.Errorf("LogLines = %v, want [Processing item 42]", inv.LogLines)
}
}
func TestParseInvocations_ErrorDetection(t *testing.T) {
ts := time.Date(2026, 2, 25, 10, 0, 0, 0, time.UTC).UnixMilli()
events := []types.OutputLogEvent{
{Message: aws.String("START RequestId: req-err Version: $LATEST"), Timestamp: aws.Int64(ts)},
{Message: aws.String("something normal"), Timestamp: aws.Int64(ts + 50)},
{Message: aws.String("FATAL: database connection lost"), Timestamp: aws.Int64(ts + 100)},
{Message: aws.String("END RequestId: req-err"), Timestamp: aws.Int64(ts + 200)},
{Message: aws.String("REPORT RequestId: req-err\tDuration: 50 ms\tBilled Duration: 100 ms\tMemory Size: 128 MB\tMax Memory Used: 64 MB"), Timestamp: aws.Int64(ts + 300)},
}
got := parseInvocations(events)
if len(got) != 1 {
t.Fatalf("expected 1 invocation, got %d", len(got))
}
if !got[0].IsError {
t.Error("expected error to be detected")
}
if len(got[0].ErrorLines) != 1 || got[0].ErrorLines[0] != "FATAL: database connection lost" {
t.Errorf("ErrorLines = %v, want [FATAL: database connection lost]", got[0].ErrorLines)
}
}
func TestParseInvocations_PreservesWithoutReport(t *testing.T) {
ts := time.Date(2026, 2, 25, 10, 0, 0, 0, time.UTC).UnixMilli()
events := []types.OutputLogEvent{
{Message: aws.String("START RequestId: incomplete Version: $LATEST"), Timestamp: aws.Int64(ts)},
{Message: aws.String("some log"), Timestamp: aws.Int64(ts + 100)},
// no END or REPORT
}
got := parseInvocations(events)
if len(got) != 1 {
t.Fatalf("expected 1 invocation without REPORT, got %d", len(got))
}
if got[0].RequestID != "incomplete" {
t.Fatalf("RequestID = %q, want %q", got[0].RequestID, "incomplete")
}
if got[0].Duration != "" || got[0].MaxMemoryUsedMB != "" {
t.Fatalf("expected missing REPORT fields to stay empty, got %+v", got[0])
}
if len(got[0].LogLines) != 1 || got[0].LogLines[0] != "some log" {
t.Fatalf("LogLines = %v, want [some log]", got[0].LogLines)
}
}
func TestParseInvocations_MultipleInvocations(t *testing.T) {
ts := time.Date(2026, 2, 25, 10, 0, 0, 0, time.UTC).UnixMilli()
events := []types.OutputLogEvent{
{Message: aws.String("START RequestId: req-a Version: $LATEST"), Timestamp: aws.Int64(ts)},
{Message: aws.String("log a"), Timestamp: aws.Int64(ts + 100)},
{Message: aws.String("REPORT RequestId: req-a\tDuration: 10 ms\tBilled Duration: 100 ms\tMemory Size: 128 MB\tMax Memory Used: 50 MB"), Timestamp: aws.Int64(ts + 200)},
{Message: aws.String("START RequestId: req-b Version: $LATEST"), Timestamp: aws.Int64(ts + 300)},
{Message: aws.String("log b"), Timestamp: aws.Int64(ts + 400)},
{Message: aws.String("REPORT RequestId: req-b\tDuration: 20 ms\tBilled Duration: 100 ms\tMemory Size: 128 MB\tMax Memory Used: 60 MB"), Timestamp: aws.Int64(ts + 500)},
}
got := parseInvocations(events)
if len(got) != 2 {
t.Fatalf("expected 2 invocations, got %d", len(got))
}
if got[0].RequestID != "req-a" || got[1].RequestID != "req-b" {
t.Errorf("request IDs = [%s, %s], want [req-a, req-b]", got[0].RequestID, got[1].RequestID)
}
}
func TestParseInvocations_NilTimestampSkipped(t *testing.T) {
ts := time.Date(2026, 2, 25, 10, 0, 0, 0, time.UTC).UnixMilli()
events := []types.OutputLogEvent{
{Message: aws.String("START RequestId: req-ts Version: $LATEST"), Timestamp: nil},
{Message: aws.String("START RequestId: req-ts Version: $LATEST"), Timestamp: aws.Int64(ts)},
{Message: aws.String("REPORT RequestId: req-ts\tDuration: 5 ms\tBilled Duration: 100 ms\tMemory Size: 128 MB\tMax Memory Used: 40 MB"), Timestamp: aws.Int64(ts + 100)},
}
got := parseInvocations(events)
if len(got) != 1 {
t.Fatalf("expected 1 invocation, got %d", len(got))
}
}
func TestParseInvocations_NilMessageSkipped(t *testing.T) {
ts := time.Date(2026, 2, 25, 10, 0, 0, 0, time.UTC).UnixMilli()
events := []types.OutputLogEvent{
{Message: nil, Timestamp: aws.Int64(ts)},
{Message: aws.String("START RequestId: req-nil Version: $LATEST"), Timestamp: aws.Int64(ts + 100)},
{Message: aws.String("REPORT RequestId: req-nil\tDuration: 5 ms\tBilled Duration: 100 ms\tMemory Size: 128 MB\tMax Memory Used: 40 MB"), Timestamp: aws.Int64(ts + 200)},
}
got := parseInvocations(events)
if len(got) != 1 {
t.Fatalf("expected 1 invocation, got %d", len(got))
}
}
func TestParseInvocations_AssignsInlineRequestIDLogsToCorrectInvocation(t *testing.T) {
ts := time.Date(2026, 2, 25, 10, 0, 0, 0, time.UTC).UnixMilli()
events := []types.OutputLogEvent{
{Message: aws.String("START RequestId: req-a Version: $LATEST"), Timestamp: aws.Int64(ts)},
{Message: aws.String("START RequestId: req-b Version: $LATEST"), Timestamp: aws.Int64(ts + 100)},
{Message: aws.String("runtime extension RequestId: req-a flushed telemetry"), Timestamp: aws.Int64(ts + 150)},
{Message: aws.String("REPORT RequestId: req-a\tDuration: 10 ms\tBilled Duration: 100 ms\tMemory Size: 128 MB\tMax Memory Used: 50 MB"), Timestamp: aws.Int64(ts + 200)},
{Message: aws.String("REPORT RequestId: req-b\tDuration: 20 ms\tBilled Duration: 100 ms\tMemory Size: 128 MB\tMax Memory Used: 60 MB"), Timestamp: aws.Int64(ts + 300)},
}
got := parseInvocations(events)
if len(got) != 2 {
t.Fatalf("expected 2 invocations, got %d", len(got))
}
if len(got[0].LogLines) != 1 || got[0].LogLines[0] != "runtime extension RequestId: req-a flushed telemetry" {
t.Fatalf("req-a LogLines = %v, want runtime line attached to req-a", got[0].LogLines)
}
if len(got[1].LogLines) != 0 {
t.Fatalf("req-b LogLines = %v, want none", got[1].LogLines)
}
}
func TestParseInvocations_IgnoresBenignErrorPhrases(t *testing.T) {
ts := time.Date(2026, 2, 25, 10, 0, 0, 0, time.UTC).UnixMilli()
events := []types.OutputLogEvent{
{Message: aws.String("START RequestId: req-ok Version: $LATEST"), Timestamp: aws.Int64(ts)},
{Message: aws.String("error_count=0"), Timestamp: aws.Int64(ts + 10)},
{Message: aws.String("no errors detected in prior batch"), Timestamp: aws.Int64(ts + 20)},
{Message: aws.String("REPORT RequestId: req-ok\tDuration: 5 ms\tBilled Duration: 100 ms\tMemory Size: 128 MB\tMax Memory Used: 40 MB"), Timestamp: aws.Int64(ts + 100)},
}
got := parseInvocations(events)
if len(got) != 1 {
t.Fatalf("expected 1 invocation, got %d", len(got))
}
if got[0].IsError {
t.Fatalf("expected benign lines not to mark invocation as error: %+v", got[0])
}
if len(got[0].ErrorLines) != 0 {
t.Fatalf("ErrorLines = %v, want none", got[0].ErrorLines)
}
}
func TestParseInvocations_PreservesInlineRequestIDLogsWithoutStartOrReport(t *testing.T) {
ts := time.Date(2026, 2, 25, 10, 0, 0, 0, time.UTC).UnixMilli()
events := []types.OutputLogEvent{
{Message: aws.String("worker RequestId: req-inline process exited before completing request"), Timestamp: aws.Int64(ts)},
}
got := parseInvocations(events)
if len(got) != 1 {
t.Fatalf("expected 1 invocation, got %d", len(got))
}
if got[0].RequestID != "req-inline" {
t.Fatalf("RequestID = %q, want req-inline", got[0].RequestID)
}
if !got[0].IsError {
t.Fatalf("expected inline error log to mark invocation as failed: %+v", got[0])
}
if len(got[0].ErrorLines) != 1 {
t.Fatalf("ErrorLines = %v, want 1 inline error line", got[0].ErrorLines)
}
}