Repository navigation
Expand file tree
/
Copy pathcontext_test.go
More file actions
167 lines (151 loc) · 3.72 KB
/
Copy pathcontext_test.go
File metadata and controls
167 lines (151 loc) · 3.72 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
package log
import (
"bytes"
"context"
"encoding/json"
"io"
"log/slog"
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace"
)
func TestNewSpanContextHandler(t *testing.T) {
jsonHandler := slog.NewJSONHandler(io.Discard, nil)
for _, testcase := range []struct {
name string
handler slog.Handler
withSpanID bool
wants slog.Handler
}{
{
name: "WithHandlerAndSpanID",
handler: jsonHandler,
withSpanID: true,
wants: &SpanContextHandler{
withSpanID: true,
handler: jsonHandler,
},
},
{
name: "WithNilHandler",
handler: nil,
wants: &SpanContextHandler{
handler: defaultHandler(),
},
},
{
name: "WithNilInterfaceHandler",
handler: slog.Handler(nil),
wants: &SpanContextHandler{
handler: defaultHandler(),
},
},
} {
t.Run(testcase.name, func(t *testing.T) {
handler := NewSpanContextHandler(testcase.handler, testcase.withSpanID)
require.Equal(t, testcase.wants, handler)
})
}
}
func TestSpanContextHandler_Handle(t *testing.T) {
buf := &bytes.Buffer{}
logger := slog.New(NewSpanContextHandler(slog.NewJSONHandler(buf, nil), true))
testTraceID := trace.TraceID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
testSpanID := trace.SpanID{1, 2, 3, 4, 5, 6, 7, 8}
for _, testcase := range []struct {
name string
message string
withSpanContext bool
}{
{
name: "WithoutTrace",
message: "test log event",
},
{
name: "WithTrace",
message: "test log event",
withSpanContext: true,
},
} {
t.Run(testcase.name, func(t *testing.T) {
buf.Reset()
ctx := context.Background()
if testcase.withSpanContext {
ctx = trace.ContextWithSpanContext(ctx, trace.NewSpanContext(trace.SpanContextConfig{
TraceID: testTraceID,
SpanID: testSpanID,
}))
}
logger.InfoContext(ctx, testcase.message)
var m map[string]any
require.NoError(t, json.Unmarshal(buf.Bytes(), &m))
if traceID, ok := m[traceIDKey]; ok == testcase.withSpanContext && ok {
require.Equal(t, testTraceID.String(), traceID)
}
if spanID, ok := m[spanIDKey]; ok == testcase.withSpanContext && ok {
require.Equal(t, testSpanID.String(), spanID)
}
})
}
}
func TestSpanContextHandler_WithAttrs(t *testing.T) {
handler1 := NewSpanContextHandler(slog.NewJSONHandler(io.Discard, nil), false)
handler2 := NewSpanContextHandler(slog.NewJSONHandler(io.Discard, nil), false)
handler2wants := NewSpanContextHandler(
slog.NewJSONHandler(io.Discard, nil).
WithAttrs([]slog.Attr{slog.String("key", "value"), slog.Int("num", 0)}),
false,
)
for _, testcase := range []struct {
name string
handler slog.Handler
attrs []slog.Attr
wants slog.Handler
}{
{
name: "NoAttrs",
handler: handler1,
wants: handler1,
},
{
name: "AddAttrs",
handler: handler2,
attrs: []slog.Attr{slog.String("key", "value"), slog.Int("num", 0)},
wants: handler2wants,
},
} {
t.Run(testcase.name, func(t *testing.T) {
require.Equal(t,
testcase.wants,
testcase.handler.WithAttrs(testcase.attrs),
)
})
}
}
func TestSpanContextHandler_WithGroup(t *testing.T) {
handler := NewSpanContextHandler(slog.NewJSONHandler(io.Discard, nil), false)
handlerWants := NewSpanContextHandler(
slog.NewJSONHandler(io.Discard, nil).WithGroup("test"),
false,
)
for _, testcase := range []struct {
name string
handler slog.Handler
group string
wants slog.Handler
}{
{
name: "AddGroup",
handler: handler,
group: "test",
wants: handlerWants,
},
} {
t.Run(testcase.name, func(t *testing.T) {
require.Equal(t,
testcase.wants,
testcase.handler.WithGroup(testcase.group),
)
})
}
}