diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d5f5a432..243ecbc98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Added - 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 diff --git a/log/logtest/assert.go b/log/logtest/assert.go index dfd754425..3af206470 100644 --- a/log/logtest/assert.go +++ b/log/logtest/assert.go @@ -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...) diff --git a/log/logtest/assert_test.go b/log/logtest/assert_test.go index 50f680711..7fafdc355 100644 --- a/log/logtest/assert_test.go +++ b/log/logtest/assert_test.go @@ -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, diff --git a/log/logtest/recorder.go b/log/logtest/recorder.go index a6c509705..2d4e036aa 100644 --- a/log/logtest/recorder.go +++ b/log/logtest/recorder.go @@ -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, } diff --git a/log/logtest/recorder_test.go b/log/logtest/recorder_test.go index f9acae94a..d2ad1d090 100644 --- a/log/logtest/recorder_test.go +++ b/log/logtest/recorder_test.go @@ -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"),