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
@@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Added ### Added
- Add `ByteSlice` and `ByteSliceValue` functions for new `BYTESLICE` attribute type in `go.opentelemetry.io/otel/attribute`. (#7948) - Add `ByteSlice` and `ByteSliceValue` functions for new `BYTESLICE` attribute type in `go.opentelemetry.io/otel/attribute`. (#7948)
- Add `Error` field on `Record` type in `go.opentelemetry.io/otel/log/logtest`. (#8148)
### Changed ### Changed
+1
View File
@@ -34,6 +34,7 @@ func AssertEqual[T Recording | Record](t TestingT, want, got T, opts ...AssertOp
cmpopts.SortSlices( cmpopts.SortSlices(
func(a, b log.KeyValue) bool { return a.Key < b.Key }, func(a, b log.KeyValue) bool { return a.Key < b.Key },
), // Unordered compare of the key values. ), // Unordered compare of the key values.
cmpopts.EquateErrors(),
cmpopts.EquateEmpty(), // Empty and nil collections are equal. cmpopts.EquateEmpty(), // Empty and nil collections are equal.
} }
cmpOpts = append(cmpOpts, cfg.cmpOpts...) cmpOpts = append(cmpOpts, cfg.cmpOpts...)
+9 -1
View File
@@ -4,6 +4,7 @@
package logtest package logtest
import ( import (
"errors"
"fmt" "fmt"
"testing" "testing"
"time" "time"
@@ -14,7 +15,10 @@ import (
"go.opentelemetry.io/otel/log" "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. // Compile-time check to ensure testing structs implement TestingT.
var ( var (
@@ -63,6 +67,7 @@ func TestAssertEqualRecording(t *testing.T) {
{ {
Timestamp: y2k, Timestamp: y2k,
Context: t.Context(), Context: t.Context(),
Error: errBoom,
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")}, Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")},
}, },
}, },
@@ -72,6 +77,7 @@ func TestAssertEqualRecording(t *testing.T) {
{ {
Timestamp: y2k, Timestamp: y2k,
Context: t.Context(), Context: t.Context(),
Error: errBoom,
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)}, Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)},
}, },
}, },
@@ -145,11 +151,13 @@ func TestAssertEqualRecord(t *testing.T) {
a: Record{ a: Record{
Timestamp: y2k, Timestamp: y2k,
Context: t.Context(), Context: t.Context(),
Error: errBoom,
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")}, Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")},
}, },
b: Record{ b: Record{
Timestamp: y2k, Timestamp: y2k,
Context: t.Context(), Context: t.Context(),
Error: errBoom,
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)}, Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)},
}, },
want: true, want: true,
+2
View File
@@ -88,6 +88,7 @@ type Record struct {
Severity log.Severity Severity log.Severity
SeverityText string SeverityText string
Body log.Value Body log.Value
Error error
Attributes []log.KeyValue Attributes []log.KeyValue
} }
@@ -219,6 +220,7 @@ func (l *logger) Emit(ctx context.Context, record log.Record) {
Severity: record.Severity(), Severity: record.Severity(),
SeverityText: record.SeverityText(), SeverityText: record.SeverityText(),
Body: record.Body(), Body: record.Body(),
Error: record.Err(),
Attributes: attrs, Attributes: attrs,
} }
+4
View File
@@ -5,6 +5,7 @@ package logtest
import ( import (
"context" "context"
"errors"
"sync" "sync"
"testing" "testing"
"time" "time"
@@ -102,6 +103,7 @@ func TestLoggerEnabledFnUnset(t *testing.T) {
func TestRecorderLoggerEmitAndReset(t *testing.T) { func TestRecorderLoggerEmitAndReset(t *testing.T) {
rec := NewRecorder() rec := NewRecorder()
ts := time.Now() ts := time.Now()
errBoom := errors.New("boom")
l := rec.Logger(t.Name()) l := rec.Logger(t.Name())
ctx := t.Context() ctx := t.Context()
@@ -109,6 +111,7 @@ func TestRecorderLoggerEmitAndReset(t *testing.T) {
r.SetTimestamp(ts) r.SetTimestamp(ts)
r.SetSeverity(log.SeverityInfo) r.SetSeverity(log.SeverityInfo)
r.SetBody(log.StringValue("Hello there")) r.SetBody(log.StringValue("Hello there"))
r.SetErr(errBoom)
r.AddAttributes(log.Int("n", 1)) r.AddAttributes(log.Int("n", 1))
r.AddAttributes(log.String("foo", "bar")) r.AddAttributes(log.String("foo", "bar"))
l.Emit(ctx, r) l.Emit(ctx, r)
@@ -125,6 +128,7 @@ func TestRecorderLoggerEmitAndReset(t *testing.T) {
Timestamp: ts, Timestamp: ts,
Severity: log.SeverityInfo, Severity: log.SeverityInfo,
Body: log.StringValue("Hello there"), Body: log.StringValue("Hello there"),
Error: errBoom,
Attributes: []log.KeyValue{ Attributes: []log.KeyValue{
log.Int("n", 1), log.Int("n", 1),
log.String("foo", "bar"), log.String("foo", "bar"),