1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00

log/logtest: add Error field to Record type (#8148)

Fixes https://github.com/open-telemetry/opentelemetry-go/issues/8147
This commit is contained in:
Robert Pająk
2026-04-08 09:41:26 +02:00
committed by GitHub
parent 3b18b21580
commit 99b2206d11
5 changed files with 17 additions and 1 deletions
+1
View File
@@ -34,6 +34,7 @@ func AssertEqual[T Recording | Record](t TestingT, want, got T, opts ...AssertOp
cmpopts.SortSlices(
func(a, b log.KeyValue) bool { return a.Key < b.Key },
), // Unordered compare of the key values.
cmpopts.EquateErrors(),
cmpopts.EquateEmpty(), // Empty and nil collections are equal.
}
cmpOpts = append(cmpOpts, cfg.cmpOpts...)
+9 -1
View File
@@ -4,6 +4,7 @@
package logtest
import (
"errors"
"fmt"
"testing"
"time"
@@ -14,7 +15,10 @@ import (
"go.opentelemetry.io/otel/log"
)
var y2k = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
var (
y2k = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
errBoom = errors.New("boom")
)
// Compile-time check to ensure testing structs implement TestingT.
var (
@@ -63,6 +67,7 @@ func TestAssertEqualRecording(t *testing.T) {
{
Timestamp: y2k,
Context: t.Context(),
Error: errBoom,
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")},
},
},
@@ -72,6 +77,7 @@ func TestAssertEqualRecording(t *testing.T) {
{
Timestamp: y2k,
Context: t.Context(),
Error: errBoom,
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)},
},
},
@@ -145,11 +151,13 @@ func TestAssertEqualRecord(t *testing.T) {
a: Record{
Timestamp: y2k,
Context: t.Context(),
Error: errBoom,
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")},
},
b: Record{
Timestamp: y2k,
Context: t.Context(),
Error: errBoom,
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)},
},
want: true,
+2
View File
@@ -88,6 +88,7 @@ type Record struct {
Severity log.Severity
SeverityText string
Body log.Value
Error error
Attributes []log.KeyValue
}
@@ -219,6 +220,7 @@ func (l *logger) Emit(ctx context.Context, record log.Record) {
Severity: record.Severity(),
SeverityText: record.SeverityText(),
Body: record.Body(),
Error: record.Err(),
Attributes: attrs,
}
+4
View File
@@ -5,6 +5,7 @@ package logtest
import (
"context"
"errors"
"sync"
"testing"
"time"
@@ -102,6 +103,7 @@ func TestLoggerEnabledFnUnset(t *testing.T) {
func TestRecorderLoggerEmitAndReset(t *testing.T) {
rec := NewRecorder()
ts := time.Now()
errBoom := errors.New("boom")
l := rec.Logger(t.Name())
ctx := t.Context()
@@ -109,6 +111,7 @@ func TestRecorderLoggerEmitAndReset(t *testing.T) {
r.SetTimestamp(ts)
r.SetSeverity(log.SeverityInfo)
r.SetBody(log.StringValue("Hello there"))
r.SetErr(errBoom)
r.AddAttributes(log.Int("n", 1))
r.AddAttributes(log.String("foo", "bar"))
l.Emit(ctx, r)
@@ -125,6 +128,7 @@ func TestRecorderLoggerEmitAndReset(t *testing.T) {
Timestamp: ts,
Severity: log.SeverityInfo,
Body: log.StringValue("Hello there"),
Error: errBoom,
Attributes: []log.KeyValue{
log.Int("n", 1),
log.String("foo", "bar"),