Skip to content

Commit

Permalink
Copy slogtest testing harness and expand
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Mar 11, 2024
1 parent cfac742 commit 0a666c0
Showing 1 changed file with 345 additions and 6 deletions.
351 changes: 345 additions & 6 deletions bridges/sloghandler/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
// 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 sloghandler_test

import (
"context"
"fmt"
"log/slog"
"reflect"
"runtime"
"strings"
"testing"
"testing/slogtest"
"time"

"go.opentelemetry.io/contrib/bridges/sloghandler"
Expand All @@ -18,14 +23,348 @@ import (
"go.opentelemetry.io/otel/log/noop"
)

// testCase represents a complete setup/run/check of an slog handler to test.
// It is copied 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
}

var cases = []testCase{
// Test cases copied from "testing/slogtest" (1.22.1).
// #######################################################################

{
name: "built-ins",
explanation: withSource("this test expects slog.TimeKey, slog.LevelKey and slog.MessageKey"),
f: func(l *slog.Logger) {
l.Info("message")
},
checks: [][]check{{
hasKey(slog.TimeKey),
hasKey(slog.LevelKey),
hasAttr(slog.MessageKey, "message"),
}},
},
{
name: "attrs",
explanation: withSource("a Handler should output attributes passed to the logging function"),
f: func(l *slog.Logger) {
l.Info("message", "k", "v")
},
checks: [][]check{{
hasAttr("k", "v"),
}},
},
{
name: "empty-attr",
explanation: withSource("a Handler should ignore an empty Attr"),
f: func(l *slog.Logger) {
l.Info("msg", "a", "b", "", nil, "c", "d")
},
checks: [][]check{{
hasAttr("a", "b"),
missingKey(""),
hasAttr("c", "d"),
}},
},
{
name: "zero-time",
explanation: withSource("a Handler should ignore a zero Record.Time"),
f: func(l *slog.Logger) {
l.Info("msg", "k", "v")
},
mod: func(r *slog.Record) { r.Time = time.Time{} },
checks: [][]check{{
missingKey(slog.TimeKey),
}},
},
{
name: "WithAttrs",
explanation: withSource("a Handler should include the attributes from the WithAttrs method"),
f: func(l *slog.Logger) {
l.With("a", "b").Info("msg", "k", "v")
},
checks: [][]check{{
hasAttr("a", "b"),
hasAttr("k", "v"),
}},
},
{
name: "groups",
explanation: withSource("a Handler should handle Group attributes"),
f: func(l *slog.Logger) {
l.Info("msg", "a", "b", slog.Group("G", slog.String("c", "d")), "e", "f")
},
checks: [][]check{{
hasAttr("a", "b"),
inGroup("G", hasAttr("c", "d")),
hasAttr("e", "f"),
}},
},
{
name: "empty-group",
explanation: withSource("a Handler should ignore an empty group"),
f: func(l *slog.Logger) {
l.Info("msg", "a", "b", slog.Group("G"), "e", "f")
},
checks: [][]check{{
hasAttr("a", "b"),
missingKey("G"),
hasAttr("e", "f"),
}},
},
{
name: "inline-group",
explanation: withSource("a Handler should inline the Attrs of a group with an empty key"),
f: func(l *slog.Logger) {
l.Info("msg", "a", "b", slog.Group("", slog.String("c", "d")), "e", "f")
},
checks: [][]check{{
hasAttr("a", "b"),
hasAttr("c", "d"),
hasAttr("e", "f"),
}},
},
{
name: "WithGroup",
explanation: withSource("a Handler should handle the WithGroup method"),
f: func(l *slog.Logger) {
l.WithGroup("G").Info("msg", "a", "b")
},
checks: [][]check{{
hasKey(slog.TimeKey),
hasKey(slog.LevelKey),
hasAttr(slog.MessageKey, "msg"),
missingKey("a"),
inGroup("G", hasAttr("a", "b")),
}},
},
{
name: "multi-With",
explanation: withSource("a Handler should handle multiple WithGroup and WithAttr calls"),
f: func(l *slog.Logger) {
l.With("a", "b").WithGroup("G").With("c", "d").WithGroup("H").Info("msg", "e", "f")
},
checks: [][]check{{
hasKey(slog.TimeKey),
hasKey(slog.LevelKey),
hasAttr(slog.MessageKey, "msg"),
hasAttr("a", "b"),
inGroup("G", hasAttr("c", "d")),
inGroup("G", inGroup("H", hasAttr("e", "f"))),
}},
},
{
name: "empty-group-record",
explanation: withSource("a Handler should not output groups if there are no attributes"),
f: func(l *slog.Logger) {
l.With("a", "b").WithGroup("G").With("c", "d").WithGroup("H").Info("msg")
},
checks: [][]check{{
hasKey(slog.TimeKey),
hasKey(slog.LevelKey),
hasAttr(slog.MessageKey, "msg"),
hasAttr("a", "b"),
inGroup("G", hasAttr("c", "d")),
inGroup("G", missingKey("H")),
}},
},
{
name: "resolve",
explanation: withSource("a Handler should call Resolve on attribute values"),
f: func(l *slog.Logger) {
l.Info("msg", "k", &replace{"replaced"})
},
checks: [][]check{{hasAttr("k", "replaced")}},
},
{
name: "resolve-groups",
explanation: withSource("a Handler should call Resolve on attribute values in groups"),
f: func(l *slog.Logger) {
l.Info("msg",
slog.Group("G",
slog.String("a", "v1"),
slog.Any("b", &replace{"v2"})))
},
checks: [][]check{{
inGroup("G", hasAttr("a", "v1")),
inGroup("G", hasAttr("b", "v2")),
}},
},
{
name: "resolve-WithAttrs",
explanation: withSource("a Handler should call Resolve on attribute values from WithAttrs"),
f: func(l *slog.Logger) {
l = l.With("k", &replace{"replaced"})
l.Info("msg")
},
checks: [][]check{{hasAttr("k", "replaced")}},
},
{
name: "resolve-WithAttrs-groups",
explanation: withSource("a Handler should call Resolve on attribute values in groups from WithAttrs"),
f: func(l *slog.Logger) {
l = l.With(slog.Group("G",
slog.String("a", "v1"),
slog.Any("b", &replace{"v2"})))
l.Info("msg")
},
checks: [][]check{{
inGroup("G", hasAttr("a", "v1")),
inGroup("G", hasAttr("b", "v2")),
}},
},
{
name: "empty-PC",
explanation: withSource("a Handler should not output SourceKey if the PC is zero"),
f: func(l *slog.Logger) {
l.Info("message")
},
mod: func(r *slog.Record) { r.PC = 0 },
checks: [][]check{{
missingKey(slog.SourceKey),
}},
},

// #######################################################################

// OTel specific test cases.
// #######################################################################

{
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"),
}},
},

// #######################################################################
}

func TestSLogHandler(t *testing.T) {
r := new(recorder)
// Based on slogtest.Run.
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
r := new(recorder)
var h slog.Handler = sloghandler.New(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)
}
}
}
})
}
}

type check func(map[string]any) string

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 ""
}
}

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 ""
}
}

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 ""
}
}

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)
}
}

// TODO: Use slogtest.Run when we drop support for Go 1.21.
err := slogtest.TestHandler(sloghandler.New(r), r.Results)
if err != nil {
t.Fatal(err)
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)
}

type wrapper struct {
slog.Handler
mod func(*slog.Record)
}

func (h *wrapper) Handle(ctx context.Context, r slog.Record) error {
h.mod(&r)
return h.Handler.Handle(ctx, r)
}

type replace struct {
v any
}

func (r *replace) LogValue() slog.Value { return slog.AnyValue(r.v) }

func (r *replace) String() string {
return fmt.Sprintf("<replace(%v)>", r.v)
}

// embeddedLogger is a type alias so the embedded.Logger type doesn't conflict
Expand Down

0 comments on commit 0a666c0

Please sign in to comment.