2024-04-09 15:45:25 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
|
|
package logtest
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-04-08 09:41:26 +02:00
|
|
|
"errors"
|
2024-04-09 15:45:25 +02:00
|
|
|
"sync"
|
|
|
|
|
"testing"
|
2025-04-12 17:07:29 +02:00
|
|
|
"time"
|
2024-04-09 15:45:25 +02:00
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
2024-10-30 06:40:14 +01:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2024-04-09 15:45:25 +02:00
|
|
|
"go.opentelemetry.io/otel/log"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestRecorderLogger(t *testing.T) {
|
|
|
|
|
for _, tt := range []struct {
|
|
|
|
|
name string
|
|
|
|
|
options []Option
|
|
|
|
|
|
|
|
|
|
loggerName string
|
|
|
|
|
loggerOptions []log.LoggerOption
|
|
|
|
|
|
2025-04-12 17:07:29 +02:00
|
|
|
want Recording
|
2024-04-09 15:45:25 +02:00
|
|
|
}{
|
|
|
|
|
{
|
2025-04-12 17:07:29 +02:00
|
|
|
name: "default scope",
|
|
|
|
|
want: Recording{
|
|
|
|
|
Scope{}: nil,
|
2024-04-09 15:45:25 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-04-12 17:07:29 +02:00
|
|
|
name: "configured scope",
|
2024-04-09 15:45:25 +02:00
|
|
|
loggerName: "test",
|
|
|
|
|
loggerOptions: []log.LoggerOption{
|
|
|
|
|
log.WithInstrumentationVersion("logtest v42"),
|
|
|
|
|
log.WithSchemaURL("https://example.com"),
|
2024-10-30 06:40:14 +01:00
|
|
|
log.WithInstrumentationAttributes(attribute.String("foo", "bar")),
|
2024-04-09 15:45:25 +02:00
|
|
|
},
|
2025-04-12 17:07:29 +02:00
|
|
|
want: Recording{
|
|
|
|
|
Scope{
|
2024-10-30 06:40:14 +01:00
|
|
|
Name: "test",
|
|
|
|
|
Version: "logtest v42",
|
|
|
|
|
SchemaURL: "https://example.com",
|
|
|
|
|
Attributes: attribute.NewSet(attribute.String("foo", "bar")),
|
2025-04-12 17:07:29 +02:00
|
|
|
}: nil,
|
2024-04-09 15:45:25 +02:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
} {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2025-04-12 17:07:29 +02:00
|
|
|
rec := NewRecorder(tt.options...)
|
|
|
|
|
rec.Logger(tt.loggerName, tt.loggerOptions...)
|
|
|
|
|
got := rec.Result()
|
|
|
|
|
assert.Equal(t, tt.want, got)
|
2024-04-09 15:45:25 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRecorderLoggerCreatesNewStruct(t *testing.T) {
|
|
|
|
|
r := &Recorder{}
|
|
|
|
|
assert.NotEqual(t, r, r.Logger("test"))
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-17 16:28:40 +02:00
|
|
|
func TestLoggerEnabled(t *testing.T) {
|
2024-04-09 15:45:25 +02:00
|
|
|
for _, tt := range []struct {
|
2024-12-03 18:36:49 +01:00
|
|
|
name string
|
|
|
|
|
options []Option
|
|
|
|
|
ctx context.Context
|
|
|
|
|
enabledParams log.EnabledParameters
|
|
|
|
|
want bool
|
2024-04-09 15:45:25 +02:00
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "the default option enables every log entry",
|
2025-09-23 15:52:45 +08:00
|
|
|
ctx: t.Context(),
|
2024-12-03 18:36:49 +01:00
|
|
|
want: true,
|
2024-04-09 15:45:25 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "with everything disabled",
|
|
|
|
|
options: []Option{
|
2024-09-13 06:35:01 +02:00
|
|
|
WithEnabledFunc(func(context.Context, log.EnabledParameters) bool {
|
2024-04-09 15:45:25 +02:00
|
|
|
return false
|
|
|
|
|
}),
|
|
|
|
|
},
|
2025-09-23 15:52:45 +08:00
|
|
|
ctx: t.Context(),
|
2024-12-03 18:36:49 +01:00
|
|
|
want: false,
|
2024-04-09 15:45:25 +02:00
|
|
|
},
|
|
|
|
|
} {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-12-03 18:36:49 +01:00
|
|
|
e := NewRecorder(tt.options...).Logger("test").Enabled(tt.ctx, tt.enabledParams)
|
|
|
|
|
assert.Equal(t, tt.want, e)
|
2024-04-09 15:45:25 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-17 16:28:40 +02:00
|
|
|
func TestLoggerEnabledFnUnset(t *testing.T) {
|
|
|
|
|
r := &logger{}
|
2025-09-23 15:52:45 +08:00
|
|
|
assert.True(t, r.Enabled(t.Context(), log.EnabledParameters{}))
|
2024-04-09 15:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-12 17:07:29 +02:00
|
|
|
func TestRecorderLoggerEmitAndReset(t *testing.T) {
|
|
|
|
|
rec := NewRecorder()
|
|
|
|
|
ts := time.Now()
|
2026-04-08 09:41:26 +02:00
|
|
|
errBoom := errors.New("boom")
|
2024-04-09 15:45:25 +02:00
|
|
|
|
2025-04-12 17:07:29 +02:00
|
|
|
l := rec.Logger(t.Name())
|
2025-09-23 15:52:45 +08:00
|
|
|
ctx := t.Context()
|
2025-04-12 17:07:29 +02:00
|
|
|
r := log.Record{}
|
|
|
|
|
r.SetTimestamp(ts)
|
|
|
|
|
r.SetSeverity(log.SeverityInfo)
|
|
|
|
|
r.SetBody(log.StringValue("Hello there"))
|
2026-04-08 09:41:26 +02:00
|
|
|
r.SetErr(errBoom)
|
2025-04-12 17:07:29 +02:00
|
|
|
r.AddAttributes(log.Int("n", 1))
|
|
|
|
|
r.AddAttributes(log.String("foo", "bar"))
|
|
|
|
|
l.Emit(ctx, r)
|
|
|
|
|
|
|
|
|
|
l2 := rec.Logger(t.Name())
|
|
|
|
|
r2 := log.Record{}
|
|
|
|
|
r2.SetBody(log.StringValue("Logger with the same scope"))
|
|
|
|
|
l2.Emit(ctx, r2)
|
|
|
|
|
|
|
|
|
|
want := Recording{
|
|
|
|
|
Scope{Name: t.Name()}: []Record{
|
|
|
|
|
{
|
|
|
|
|
Context: ctx,
|
|
|
|
|
Timestamp: ts,
|
|
|
|
|
Severity: log.SeverityInfo,
|
|
|
|
|
Body: log.StringValue("Hello there"),
|
2026-04-08 09:41:26 +02:00
|
|
|
Error: errBoom,
|
2025-04-12 17:07:29 +02:00
|
|
|
Attributes: []log.KeyValue{
|
|
|
|
|
log.Int("n", 1),
|
|
|
|
|
log.String("foo", "bar"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Context: ctx,
|
|
|
|
|
Body: log.StringValue("Logger with the same scope"),
|
|
|
|
|
Attributes: []log.KeyValue{},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
got := rec.Result()
|
|
|
|
|
assert.Equal(t, want, got)
|
2024-06-06 17:28:28 +02:00
|
|
|
|
2025-04-12 17:07:29 +02:00
|
|
|
rec.Reset()
|
2024-04-09 15:45:25 +02:00
|
|
|
|
2025-04-12 17:07:29 +02:00
|
|
|
want = Recording{
|
|
|
|
|
Scope{Name: t.Name()}: nil,
|
|
|
|
|
}
|
|
|
|
|
got = rec.Result()
|
|
|
|
|
assert.Equal(t, want, got)
|
2024-04-09 15:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-23 15:52:45 +08:00
|
|
|
func TestRecorderConcurrentSafe(t *testing.T) {
|
2024-04-09 15:45:25 +02:00
|
|
|
const goRoutineN = 10
|
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(goRoutineN)
|
|
|
|
|
|
|
|
|
|
r := &Recorder{}
|
|
|
|
|
|
2025-07-29 18:19:11 +10:00
|
|
|
for range goRoutineN {
|
2024-04-09 15:45:25 +02:00
|
|
|
go func() {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
|
|
nr := r.Logger("test")
|
2025-09-23 15:52:45 +08:00
|
|
|
nr.Enabled(t.Context(), log.EnabledParameters{})
|
|
|
|
|
nr.Emit(t.Context(), log.Record{})
|
2024-04-09 15:45:25 +02:00
|
|
|
|
|
|
|
|
r.Result()
|
|
|
|
|
r.Reset()
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
}
|