-
Notifications
You must be signed in to change notification settings - Fork 542
/
handler_test.go
601 lines (539 loc) · 14.4 KB
/
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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package otelslog
import (
"context"
"fmt"
"log/slog"
"reflect"
"runtime"
"testing"
"testing/slogtest"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/embedded"
"go.opentelemetry.io/otel/log/global"
"go.opentelemetry.io/otel/sdk/instrumentation"
)
var now = time.Now()
func TestNewLogger(t *testing.T) {
assert.IsType(t, &Handler{}, NewLogger().Handler())
}
// embeddedLogger is a type alias so the embedded.Logger type doesn't conflict
// with the Logger method of the recorder when it is embedded.
type embeddedLogger = embedded.Logger // nolint:unused // Used below.
// recorder records all [log.Record]s it is ased to emit.
type recorder struct {
embedded.LoggerProvider
embeddedLogger // nolint:unused // Used to embed embedded.Logger.
// Records are the records emitted.
Records []log.Record
// Scope is the Logger scope recorder received when Logger was called.
Scope instrumentation.Scope
// MinSeverity is the minimum severity the recorder will return true for
// when Enabled is called (unless enableKey is set).
MinSeverity log.Severity
}
func (r *recorder) Logger(name string, opts ...log.LoggerOption) log.Logger {
cfg := log.NewLoggerConfig(opts...)
r.Scope = instrumentation.Scope{
Name: name,
Version: cfg.InstrumentationVersion(),
SchemaURL: cfg.SchemaURL(),
}
return r
}
type enablerKey uint
var enableKey enablerKey
func (r *recorder) Enabled(ctx context.Context, record log.Record) bool {
return ctx.Value(enableKey) != nil || record.Severity() >= r.MinSeverity
}
func (r *recorder) Emit(_ context.Context, record log.Record) {
r.Records = append(r.Records, record)
}
func (r *recorder) Results() []map[string]any {
out := make([]map[string]any, len(r.Records))
for i := range out {
r := r.Records[i]
m := make(map[string]any)
if tStamp := r.Timestamp(); !tStamp.IsZero() {
m[slog.TimeKey] = tStamp
}
if lvl := r.Severity(); lvl != 0 {
m[slog.LevelKey] = lvl - 9
}
if body := r.Body(); body.Kind() != log.KindEmpty {
m[slog.MessageKey] = value2Result(body)
}
r.WalkAttributes(func(kv log.KeyValue) bool {
m[kv.Key] = value2Result(kv.Value)
return true
})
out[i] = m
}
return out
}
func value2Result(v log.Value) any {
switch v.Kind() {
case log.KindBool:
return v.AsBool()
case log.KindFloat64:
return v.AsFloat64()
case log.KindInt64:
return v.AsInt64()
case log.KindString:
return v.AsString()
case log.KindBytes:
return v.AsBytes()
case log.KindSlice:
return v.AsSlice()
case log.KindMap:
m := make(map[string]any)
for _, val := range v.AsMap() {
m[val.Key] = value2Result(val.Value)
}
return m
}
return nil
}
// testCase represents a complete setup/run/check of an slog handler to test.
// It is based on the testCase from "testing/slogtest" (1.22.1).
type testCase struct {
// Subtest name.
name string
// If non-empty, explanation explains the violated constraint.
explanation string
// f executes a single log event using its argument logger.
// So that mkdescs.sh can generate the right description,
// the body of f must appear on a single line whose first
// non-whitespace characters are "l.".
f func(*slog.Logger)
// If mod is not nil, it is called to modify the Record
// generated by the Logger before it is passed to the Handler.
mod func(*slog.Record)
// checks is a list of checks to run on the result. Each item is a slice of
// checks that will be evaluated for the corresponding record emitted.
checks [][]check
}
// copied from slogtest (1.22.1).
type check func(map[string]any) string
// copied from slogtest (1.22.1).
func hasKey(key string) check {
return func(m map[string]any) string {
if _, ok := m[key]; !ok {
return fmt.Sprintf("missing key %q", key)
}
return ""
}
}
// copied from slogtest (1.22.1).
func missingKey(key string) check {
return func(m map[string]any) string {
if _, ok := m[key]; ok {
return fmt.Sprintf("unexpected key %q", key)
}
return ""
}
}
// copied from slogtest (1.22.1).
func hasAttr(key string, wantVal any) check {
return func(m map[string]any) string {
if s := hasKey(key)(m); s != "" {
return s
}
gotVal := m[key]
if !reflect.DeepEqual(gotVal, wantVal) {
return fmt.Sprintf("%q: got %#v, want %#v", key, gotVal, wantVal)
}
return ""
}
}
// copied from slogtest (1.22.1).
func inGroup(name string, c check) check {
return func(m map[string]any) string {
v, ok := m[name]
if !ok {
return fmt.Sprintf("missing group %q", name)
}
g, ok := v.(map[string]any)
if !ok {
return fmt.Sprintf("value for group %q is not map[string]any", name)
}
return c(g)
}
}
// copied from slogtest (1.22.1).
func withSource(s string) string {
_, file, line, ok := runtime.Caller(1)
if !ok {
panic("runtime.Caller failed")
}
return fmt.Sprintf("%s (%s:%d)", s, file, line)
}
// copied from slogtest (1.22.1).
type wrapper struct {
slog.Handler
mod func(*slog.Record)
}
// copied from slogtest (1.22.1).
func (h *wrapper) Handle(ctx context.Context, r slog.Record) error {
h.mod(&r)
return h.Handler.Handle(ctx, r)
}
func TestSLogHandler(t *testing.T) {
cases := []testCase{
{
name: "Values",
explanation: withSource("all slog Values need to be supported"),
f: func(l *slog.Logger) {
l.Info(
"msg",
"any", struct{ data int64 }{data: 1},
"bool", true,
"duration", time.Minute,
"float64", 3.14159,
"int64", -2,
"string", "str",
"time", now,
"uint64", uint64(3),
// KindGroup and KindLogValuer are left for slogtest.TestHandler.
)
},
checks: [][]check{{
hasKey(slog.TimeKey),
hasKey(slog.LevelKey),
hasAttr("any", "{data:1}"),
hasAttr("bool", true),
hasAttr("duration", int64(time.Minute)),
hasAttr("float64", 3.14159),
hasAttr("int64", int64(-2)),
hasAttr("string", "str"),
hasAttr("time", now.UnixNano()),
hasAttr("uint64", int64(3)),
}},
},
{
name: "multi-messages",
explanation: withSource("this test expects multiple independent messages"),
f: func(l *slog.Logger) {
l.Info("one")
l.Info("two")
},
checks: [][]check{{
hasKey(slog.TimeKey),
hasKey(slog.LevelKey),
hasAttr(slog.MessageKey, "one"),
}, {
hasKey(slog.TimeKey),
hasKey(slog.LevelKey),
hasAttr(slog.MessageKey, "two"),
}},
},
{
name: "multi-attrs",
explanation: withSource("attributes from one message do not affect another"),
f: func(l *slog.Logger) {
l.Info("one", "k", "v")
l.Info("two")
},
checks: [][]check{{
hasAttr("k", "v"),
}, {
missingKey("k"),
}},
},
{
name: "independent-WithAttrs",
explanation: withSource("a Handler should only include attributes from its own WithAttr origin"),
f: func(l *slog.Logger) {
l1 := l.With("a", "b")
l2 := l1.With("c", "d")
l3 := l1.With("e", "f")
l3.Info("msg", "k", "v")
l2.Info("msg", "k", "v")
l1.Info("msg", "k", "v")
l.Info("msg", "k", "v")
},
checks: [][]check{{
hasAttr("a", "b"),
hasAttr("e", "f"),
hasAttr("k", "v"),
}, {
hasAttr("a", "b"),
hasAttr("c", "d"),
hasAttr("k", "v"),
missingKey("e"),
}, {
hasAttr("a", "b"),
hasAttr("k", "v"),
missingKey("c"),
missingKey("e"),
}, {
hasAttr("k", "v"),
missingKey("a"),
missingKey("c"),
missingKey("e"),
}},
},
{
name: "independent-WithGroup",
explanation: withSource("a Handler should only include attributes from its own WithGroup origin"),
f: func(l *slog.Logger) {
l1 := l.WithGroup("G").With("a", "b")
l2 := l1.WithGroup("H").With("c", "d")
l3 := l1.WithGroup("I").With("e", "f")
l3.Info("msg", "k", "v")
l2.Info("msg", "k", "v")
l1.Info("msg", "k", "v")
l.Info("msg", "k", "v")
},
checks: [][]check{{
hasKey(slog.TimeKey),
hasKey(slog.LevelKey),
hasAttr(slog.MessageKey, "msg"),
missingKey("a"),
missingKey("c"),
missingKey("H"),
inGroup("G", hasAttr("a", "b")),
inGroup("G", inGroup("I", hasAttr("e", "f"))),
inGroup("G", inGroup("I", hasAttr("k", "v"))),
}, {
hasKey(slog.TimeKey),
hasKey(slog.LevelKey),
hasAttr(slog.MessageKey, "msg"),
missingKey("a"),
missingKey("c"),
inGroup("G", hasAttr("a", "b")),
inGroup("G", inGroup("H", hasAttr("c", "d"))),
inGroup("G", inGroup("H", hasAttr("k", "v"))),
}, {
hasKey(slog.TimeKey),
hasKey(slog.LevelKey),
hasAttr(slog.MessageKey, "msg"),
missingKey("a"),
missingKey("c"),
missingKey("H"),
inGroup("G", hasAttr("a", "b")),
inGroup("G", hasAttr("k", "v")),
}, {
hasKey(slog.TimeKey),
hasKey(slog.LevelKey),
hasAttr("k", "v"),
hasAttr(slog.MessageKey, "msg"),
missingKey("a"),
missingKey("c"),
missingKey("G"),
missingKey("H"),
}},
},
{
name: "independent-WithGroup.WithAttrs",
explanation: withSource("a Handler should only include group attributes from its own WithAttr origin"),
f: func(l *slog.Logger) {
l = l.WithGroup("G")
l.With("a", "b").Info("msg", "k", "v")
l.With("c", "d").Info("msg", "k", "v")
},
checks: [][]check{{
inGroup("G", hasAttr("a", "b")),
inGroup("G", hasAttr("k", "v")),
inGroup("G", missingKey("c")),
}, {
inGroup("G", hasAttr("c", "d")),
inGroup("G", hasAttr("k", "v")),
inGroup("G", missingKey("a")),
}},
},
}
// Based on slogtest.Run.
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
r := new(recorder)
var h slog.Handler = NewHandler(WithLoggerProvider(r))
if c.mod != nil {
h = &wrapper{h, c.mod}
}
l := slog.New(h)
c.f(l)
got := r.Results()
if len(got) != len(c.checks) {
t.Fatalf("missing record checks: %d records, %d checks", len(got), len(c.checks))
}
for i, checks := range c.checks {
for _, check := range checks {
if p := check(got[i]); p != "" {
t.Errorf("%s: %s", p, c.explanation)
}
}
}
})
}
t.Run("slogtest.TestHandler", func(t *testing.T) {
r := new(recorder)
h := NewHandler(WithLoggerProvider(r))
// TODO: use slogtest.Run when Go 1.21 is no longer supported.
err := slogtest.TestHandler(h, r.Results)
if err != nil {
t.Fatal(err)
}
})
}
func TestNewHandlerConfiguration(t *testing.T) {
t.Run("Default", func(t *testing.T) {
r := new(recorder)
global.SetLoggerProvider(r)
var h *Handler
assert.NotPanics(t, func() { h = NewHandler() })
assert.NotNil(t, h.logger)
require.IsType(t, &recorder{}, h.logger)
l := h.logger.(*recorder)
want := instrumentation.Scope{Name: bridgeName, Version: version}
assert.Equal(t, want, l.Scope)
})
t.Run("Options", func(t *testing.T) {
r := new(recorder)
scope := instrumentation.Scope{Name: "name", Version: "ver", SchemaURL: "url"}
var h *Handler
assert.NotPanics(t, func() {
h = NewHandler(
WithLoggerProvider(r),
WithInstrumentationScope(scope),
)
})
assert.NotNil(t, h.logger)
require.IsType(t, &recorder{}, h.logger)
l := h.logger.(*recorder)
assert.Equal(t, scope, l.Scope)
})
}
func TestHandlerEnabled(t *testing.T) {
r := new(recorder)
r.MinSeverity = log.SeverityInfo
h := NewHandler(WithLoggerProvider(r))
ctx := context.Background()
assert.False(t, h.Enabled(ctx, slog.LevelDebug), "level conversion: permissive")
assert.True(t, h.Enabled(ctx, slog.LevelInfo), "level conversion: restrictive")
ctx = context.WithValue(ctx, enableKey, true)
assert.True(t, h.Enabled(ctx, slog.LevelDebug), "context not passed")
}
func BenchmarkHandler(b *testing.B) {
var (
h slog.Handler
err error
)
attrs10 := []slog.Attr{
slog.String("1", "1"),
slog.Int64("2", 2),
slog.Int("3", 3),
slog.Uint64("4", 4),
slog.Float64("5", 5.),
slog.Bool("6", true),
slog.Time("7", time.Now()),
slog.Duration("8", time.Second),
slog.Any("9", 9),
slog.Any("10", "10"),
}
attrs5 := attrs10[:5]
record := slog.NewRecord(time.Now(), slog.LevelInfo, "body", 0)
ctx := context.Background()
b.Run("Handle", func(b *testing.B) {
handlers := make([]*Handler, b.N)
for i := range handlers {
handlers[i] = NewHandler()
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
err = handlers[n].Handle(ctx, record)
}
})
b.Run("WithAttrs", func(b *testing.B) {
b.Run("5", func(b *testing.B) {
handlers := make([]*Handler, b.N)
for i := range handlers {
handlers[i] = NewHandler()
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
h = handlers[n].WithAttrs(attrs5)
}
})
b.Run("10", func(b *testing.B) {
handlers := make([]*Handler, b.N)
for i := range handlers {
handlers[i] = NewHandler()
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
h = handlers[n].WithAttrs(attrs10)
}
})
})
b.Run("WithGroup", func(b *testing.B) {
handlers := make([]*Handler, b.N)
for i := range handlers {
handlers[i] = NewHandler()
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
h = handlers[n].WithGroup("group")
}
})
b.Run("WithGroup.WithAttrs", func(b *testing.B) {
b.Run("5", func(b *testing.B) {
handlers := make([]*Handler, b.N)
for i := range handlers {
handlers[i] = NewHandler()
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
h = handlers[n].WithGroup("group").WithAttrs(attrs5)
}
})
b.Run("10", func(b *testing.B) {
handlers := make([]*Handler, b.N)
for i := range handlers {
handlers[i] = NewHandler()
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
h = handlers[n].WithGroup("group").WithAttrs(attrs10)
}
})
})
b.Run("(WithGroup.WithAttrs).Handle", func(b *testing.B) {
b.Run("5", func(b *testing.B) {
handlers := make([]slog.Handler, b.N)
for i := range handlers {
handlers[i] = NewHandler().WithGroup("group").WithAttrs(attrs5)
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
err = handlers[n].Handle(ctx, record)
}
})
b.Run("10", func(b *testing.B) {
handlers := make([]slog.Handler, b.N)
for i := range handlers {
handlers[i] = NewHandler().WithGroup("group").WithAttrs(attrs10)
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
err = handlers[n].Handle(ctx, record)
}
})
})
_, _ = h, err
}