-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_test.go
69 lines (51 loc) · 1.46 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
package otelslog_test
import (
"context"
"io"
"log/slog"
"os"
"testing"
"time"
"go.opentelemetry.io/otel/trace"
"github.com/go-slog/otelslog"
)
func testHandler() slog.Handler {
return slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
AddSource: false,
Level: nil,
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
a.Value = slog.TimeValue(time.Time{})
}
return a
},
})
}
func testContext() context.Context {
ctx := context.Background()
spanContext := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: [16]byte{116, 114, 97, 99, 101, 95, 105, 100, 95, 116, 101, 115, 116, 49, 50, 51},
SpanID: [8]byte{115, 112, 97, 110, 95, 105, 100, 49},
})
ctx = trace.ContextWithSpanContext(ctx, spanContext)
return ctx
}
func ExampleHandler() {
var handler slog.Handler
// Set up handler
handler = testHandler()
// Wrap handler
handler = otelslog.NewHandler(handler)
// Set up logger
logger := slog.New(handler)
// later in some trace context (eg. http request)
ctx := testContext()
// Call logger with a context
logger.InfoContext(ctx, "hello world")
// Output: time=0001-01-01T00:00:00.000Z level=INFO msg="hello world" trace_id=74726163655f69645f74657374313233 span_id=7370616e5f696431
}
func TestHandler_Handle_NoSpan(_ *testing.T) {
handler := otelslog.NewHandler(slog.NewJSONHandler(io.Discard, nil))
logger := slog.New(handler)
logger.InfoContext(context.Background(), "hello world")
}