Debian Codegraph @

log/logtest/recorder_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package logtest
5
6 import (
7 "context"
8 "sync"
9 "testing"
10 "time"
11
12 "github.com/stretchr/testify/assert"
13
14 "go.opentelemetry.io/otel/attribute"
15 "go.opentelemetry.io/otel/log"
16 )
17
18 func TestRecorderLogger(t *testing.T) {
19 for _, tt := range []struct {
20 name string
21 options []Option
22
23 loggerName string
24 loggerOptions []log.LoggerOption
25
26 want Recording
27 }{
28 {
29 name: "default scope",
30 want: Recording{
31 Scope{}: nil,
32 },
33 },
34 {
35 name: "configured scope",
36 loggerName: "test",
37 loggerOptions: []log.LoggerOption{
38 log.WithInstrumentationVersion("logtest v42"),
39 log.WithSchemaURL("https://example.com"),
40 log.WithInstrumentationAttributes(attribute.String("foo", "bar")),
41 },
42 want: Recording{
43 Scope{
44 Name: "test",
45 Version: "logtest v42",
46 SchemaURL: "https://example.com",
47 Attributes: attribute.NewSet(attribute.String("foo", "bar")),
48 }: nil,
49 },
50 },
51 } {
52 t.Run(tt.name, func(t *testing.T) {
53 rec := NewRecorder(tt.options...)
54 rec.Logger(tt.loggerName, tt.loggerOptions...)
55 got := rec.Result()
56 assert.Equal(t, tt.want, got)
57 })
58 }
59 }
60
61 func TestRecorderLoggerCreatesNewStruct(t *testing.T) {
62 r := &Recorder{}
63 assert.NotEqual(t, r, r.Logger("test"))
64 }
65
66 func TestLoggerEnabled(t *testing.T) {
67 for _, tt := range []struct {
68 name string
69 options []Option
70 ctx context.Context
71 enabledParams log.EnabledParameters
72 want bool
73 }{
74 {
75 name: "the default option enables every log entry",
76 ctx: t.Context(),
77 want: true,
78 },
79 {
80 name: "with everything disabled",
81 options: []Option{
82 WithEnabledFunc(func(context.Context, log.EnabledParameters) bool {
83 return false
84 }),
85 },
86 ctx: t.Context(),
87 want: false,
88 },
89 } {
90 t.Run(tt.name, func(t *testing.T) {
91 e := NewRecorder(tt.options...).Logger("test").Enabled(tt.ctx, tt.enabledParams)
92 assert.Equal(t, tt.want, e)
93 })
94 }
95 }
96
97 func TestLoggerEnabledFnUnset(t *testing.T) {
98 r := &logger{}
99 assert.True(t, r.Enabled(t.Context(), log.EnabledParameters{}))
100 }
101
102 func TestRecorderLoggerEmitAndReset(t *testing.T) {
103 rec := NewRecorder()
104 ts := time.Now()
105
106 l := rec.Logger(t.Name())
107 ctx := t.Context()
108 r := log.Record{}
109 r.SetTimestamp(ts)
110 r.SetSeverity(log.SeverityInfo)
111 r.SetBody(log.StringValue("Hello there"))
112 r.AddAttributes(log.Int("n", 1))
113 r.AddAttributes(log.String("foo", "bar"))
114 l.Emit(ctx, r)
115
116 l2 := rec.Logger(t.Name())
117 r2 := log.Record{}
118 r2.SetBody(log.StringValue("Logger with the same scope"))
119 l2.Emit(ctx, r2)
120
121 want := Recording{
122 Scope{Name: t.Name()}: []Record{
123 {
124 Context: ctx,
125 Timestamp: ts,
126 Severity: log.SeverityInfo,
127 Body: log.StringValue("Hello there"),
128 Attributes: []log.KeyValue{
129 log.Int("n", 1),
130 log.String("foo", "bar"),
131 },
132 },
133 {
134 Context: ctx,
135 Body: log.StringValue("Logger with the same scope"),
136 Attributes: []log.KeyValue{},
137 },
138 },
139 }
140 got := rec.Result()
141 assert.Equal(t, want, got)
142
143 rec.Reset()
144
145 want = Recording{
146 Scope{Name: t.Name()}: nil,
147 }
148 got = rec.Result()
149 assert.Equal(t, want, got)
150 }
151
152 func TestRecorderConcurrentSafe(t *testing.T) {
153 const goRoutineN = 10
154
155 var wg sync.WaitGroup
156 wg.Add(goRoutineN)
157
158 r := &Recorder{}
159
160 for range goRoutineN {
161 go func() {
162 defer wg.Done()
163
164 nr := r.Logger("test")
165 nr.Enabled(t.Context(), log.EnabledParameters{})
166 nr.Emit(t.Context(), log.Record{})
167
168 r.Result()
169 r.Reset()
170 }()
171 }
172
173 wg.Wait()
174 }