From b3a11c150fdc966834adc8effdb69e8b39745d7c Mon Sep 17 00:00:00 2001 From: dmathieu Date: Mon, 10 Jun 2024 09:44:30 +0200 Subject: [PATCH 1/5] introduce logtest.AssertRecordEqual --- log/logtest/assertions.go | 58 ++++++++++++++++++++++++++++++++++ log/logtest/assertions_test.go | 33 +++++++++++++++++++ log/logtest/factory_test.go | 46 --------------------------- log/logtest/recorder_test.go | 4 +-- 4 files changed, 93 insertions(+), 48 deletions(-) create mode 100644 log/logtest/assertions.go create mode 100644 log/logtest/assertions_test.go diff --git a/log/logtest/assertions.go b/log/logtest/assertions.go new file mode 100644 index 00000000000..d6c0cffae51 --- /dev/null +++ b/log/logtest/assertions.go @@ -0,0 +1,58 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package logtest + +import ( + "slices" + "testing" + + "go.opentelemetry.io/otel/log" +) + +// AssertRecordEqual compares two log records, and fails the test if they are +// not equal. +func AssertRecordEqual(t testing.TB, want log.Record, got log.Record) { + t.Helper() + + if !want.Timestamp().Equal(got.Timestamp()) { + t.Errorf("Timestamp value is not equal:\nwant: %v\ngot: %v", want.Timestamp(), got.Timestamp()) + } + if !want.ObservedTimestamp().Equal(got.ObservedTimestamp()) { + t.Errorf("ObservedTimestamp value is not equal:\nwant: %v\ngot: %v", want.ObservedTimestamp(), got.ObservedTimestamp()) + } + if want.Severity() != got.Severity() { + t.Errorf("Severity value is not equal:\nwant: %v\ngot: %v", want.Severity(), got.Severity()) + } + if want.SeverityText() != got.SeverityText() { + t.Errorf("SeverityText value is not equal:\nwant: %v\ngot: %v", want.SeverityText(), got.SeverityText()) + } + assertBody(t, want.Body(), got) + + var attrs []log.KeyValue + want.WalkAttributes(func(kv log.KeyValue) bool { + attrs = append(attrs, kv) + return true + }) + assertAttributes(t, attrs, got) +} + +func assertBody(t testing.TB, want log.Value, r log.Record) { + t.Helper() + got := r.Body() + if !got.Equal(want) { + t.Errorf("Body value is not equal:\nwant: %v\ngot: %v", want, got) + } +} + +func assertAttributes(t testing.TB, want []log.KeyValue, r log.Record) { + t.Helper() + var got []log.KeyValue + r.WalkAttributes(func(kv log.KeyValue) bool { + got = append(got, kv) + return true + }) + if !slices.EqualFunc(want, got, log.KeyValue.Equal) { + t.Errorf("Attributes are not equal:\nwant: %v\ngot: %v", want, got) + } +} diff --git a/log/logtest/assertions_test.go b/log/logtest/assertions_test.go new file mode 100644 index 00000000000..7f4e91b675e --- /dev/null +++ b/log/logtest/assertions_test.go @@ -0,0 +1,33 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package logtest + +import ( + "testing" + "time" + + "go.opentelemetry.io/otel/log" +) + +func TestAssertRecord(t *testing.T) { + r1 := log.Record{} + r2 := log.Record{} + now := time.Now() + + AssertRecordEqual(t, r1, r2) + + r1.SetTimestamp(now) + r2.SetTimestamp(now) + r1.SetObservedTimestamp(now) + r2.SetObservedTimestamp(now) + r1.SetSeverity(log.SeverityTrace1) + r2.SetSeverity(log.SeverityTrace1) + r1.SetSeverityText("trace") + r2.SetSeverityText("trace") + r1.SetBody(log.StringValue("log body")) + r2.SetBody(log.StringValue("log body")) + r1.AddAttributes(log.Bool("attr", true)) + r2.AddAttributes(log.Bool("attr", true)) + AssertRecordEqual(t, r1, r2) +} diff --git a/log/logtest/factory_test.go b/log/logtest/factory_test.go index 6fa61049aae..8cfae272081 100644 --- a/log/logtest/factory_test.go +++ b/log/logtest/factory_test.go @@ -4,7 +4,6 @@ package logtest import ( - "slices" "testing" "time" @@ -66,48 +65,3 @@ func TestRecordFactoryMultiple(t *testing.T) { assert.Equal(t, now, record1.Timestamp()) assertAttributes(t, attrs, record1) } - -func assertRecord(t *testing.T, want log.Record, got log.Record) { - t.Helper() - - if !want.Timestamp().Equal(got.Timestamp()) { - t.Errorf("Timestamp value is not equal:\nwant: %v\ngot: %v", want.Timestamp(), got.Timestamp()) - } - if !want.ObservedTimestamp().Equal(got.ObservedTimestamp()) { - t.Errorf("ObservedTimestamp value is not equal:\nwant: %v\ngot: %v", want.ObservedTimestamp(), got.ObservedTimestamp()) - } - if want.Severity() != got.Severity() { - t.Errorf("Severity value is not equal:\nwant: %v\ngot: %v", want.Severity(), got.Severity()) - } - if want.SeverityText() != got.SeverityText() { - t.Errorf("SeverityText value is not equal:\nwant: %v\ngot: %v", want.SeverityText(), got.SeverityText()) - } - assertBody(t, want.Body(), got) - - var attrs []log.KeyValue - want.WalkAttributes(func(kv log.KeyValue) bool { - attrs = append(attrs, kv) - return true - }) - assertAttributes(t, attrs, got) -} - -func assertBody(t *testing.T, want log.Value, r log.Record) { - t.Helper() - got := r.Body() - if !got.Equal(want) { - t.Errorf("Body value is not equal:\nwant: %v\ngot: %v", want, got) - } -} - -func assertAttributes(t *testing.T, want []log.KeyValue, r log.Record) { - t.Helper() - var got []log.KeyValue - r.WalkAttributes(func(kv log.KeyValue) bool { - got = append(got, kv) - return true - }) - if !slices.EqualFunc(want, got, log.KeyValue.Equal) { - t.Errorf("Attributes are not equal:\nwant: %v\ngot: %v", want, got) - } -} diff --git a/log/logtest/recorder_test.go b/log/logtest/recorder_test.go index a66ebe96cf1..15f67d0e6de 100644 --- a/log/logtest/recorder_test.go +++ b/log/logtest/recorder_test.go @@ -133,11 +133,11 @@ func TestRecorderEmitAndReset(t *testing.T) { nl.Emit(ctx2, r2) assert.Len(t, r.Result()[0].Records, 1) - assertRecord(t, r.Result()[0].Records[0].Record, r1) + AssertRecordEqual(t, r.Result()[0].Records[0].Record, r1) assert.Equal(t, r.Result()[0].Records[0].Context(), ctx) assert.Len(t, r.Result()[1].Records, 1) - assertRecord(t, r.Result()[1].Records[0].Record, r2) + AssertRecordEqual(t, r.Result()[1].Records[0].Record, r2) assert.Equal(t, r.Result()[1].Records[0].Context(), ctx2) r.Reset() From 3c53f0ef9cefa07914fa8642f3cd82f490ba8297 Mon Sep 17 00:00:00 2001 From: dmathieu Date: Mon, 10 Jun 2024 09:47:04 +0200 Subject: [PATCH 2/5] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f28a537f062..83f2ea7793d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The `IsEmpty` method is added to the `Instrument` type in `go.opentelemetry.io/otel/sdk/metric`. This method is used to check if an `Instrument` instance is a zero-value. (#5431) - Store and provide the emitted `context.Context` in `ScopeRecords` of `go.opentelemetry.io/otel/sdk/log/logtest`. (#5468) +- The `AssertRecordEqual` method to `go.opentelemetry.io/otel/log/logtest` to allow comparison of two log records in tests. (#5499) ### Changed From fc2dd1ab37988d08b2dd6b6b91a398818f37d59b Mon Sep 17 00:00:00 2001 From: dmathieu Date: Mon, 10 Jun 2024 10:07:47 +0200 Subject: [PATCH 3/5] fix vanity import --- log/logtest/assertions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log/logtest/assertions.go b/log/logtest/assertions.go index d6c0cffae51..cc48ffe4641 100644 --- a/log/logtest/assertions.go +++ b/log/logtest/assertions.go @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -package logtest +package logtest // import "go.opentelemetry.io/otel/log/logtest" import ( "slices" From 5862e5044353c1c2a7bd8e843c8c773718943380 Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Wed, 12 Jun 2024 08:56:45 +0200 Subject: [PATCH 4/5] Update log/logtest/assertions.go Co-authored-by: Tyler Yahn --- log/logtest/assertions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log/logtest/assertions.go b/log/logtest/assertions.go index cc48ffe4641..51f16d045d2 100644 --- a/log/logtest/assertions.go +++ b/log/logtest/assertions.go @@ -12,7 +12,7 @@ import ( // AssertRecordEqual compares two log records, and fails the test if they are // not equal. -func AssertRecordEqual(t testing.TB, want log.Record, got log.Record) { +func AssertRecordEqual(t testing.TB, want, got log.Record) { t.Helper() if !want.Timestamp().Equal(got.Timestamp()) { From 3d301840a57e0be66ca743ff29b0f41f4f49bc56 Mon Sep 17 00:00:00 2001 From: dmathieu Date: Wed, 12 Jun 2024 09:04:52 +0200 Subject: [PATCH 5/5] return a bool with the assertion result --- log/logtest/assertions.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/log/logtest/assertions.go b/log/logtest/assertions.go index 51f16d045d2..8fb72e65ea2 100644 --- a/log/logtest/assertions.go +++ b/log/logtest/assertions.go @@ -12,40 +12,49 @@ import ( // AssertRecordEqual compares two log records, and fails the test if they are // not equal. -func AssertRecordEqual(t testing.TB, want, got log.Record) { +func AssertRecordEqual(t testing.TB, want, got log.Record) bool { t.Helper() if !want.Timestamp().Equal(got.Timestamp()) { t.Errorf("Timestamp value is not equal:\nwant: %v\ngot: %v", want.Timestamp(), got.Timestamp()) + return false } if !want.ObservedTimestamp().Equal(got.ObservedTimestamp()) { t.Errorf("ObservedTimestamp value is not equal:\nwant: %v\ngot: %v", want.ObservedTimestamp(), got.ObservedTimestamp()) + return false } if want.Severity() != got.Severity() { t.Errorf("Severity value is not equal:\nwant: %v\ngot: %v", want.Severity(), got.Severity()) + return false } if want.SeverityText() != got.SeverityText() { t.Errorf("SeverityText value is not equal:\nwant: %v\ngot: %v", want.SeverityText(), got.SeverityText()) + return false + } + if !assertBody(t, want.Body(), got) { + return false } - assertBody(t, want.Body(), got) var attrs []log.KeyValue want.WalkAttributes(func(kv log.KeyValue) bool { attrs = append(attrs, kv) return true }) - assertAttributes(t, attrs, got) + return assertAttributes(t, attrs, got) } -func assertBody(t testing.TB, want log.Value, r log.Record) { +func assertBody(t testing.TB, want log.Value, r log.Record) bool { t.Helper() got := r.Body() if !got.Equal(want) { t.Errorf("Body value is not equal:\nwant: %v\ngot: %v", want, got) + return false } + + return true } -func assertAttributes(t testing.TB, want []log.KeyValue, r log.Record) { +func assertAttributes(t testing.TB, want []log.KeyValue, r log.Record) bool { t.Helper() var got []log.KeyValue r.WalkAttributes(func(kv log.KeyValue) bool { @@ -54,5 +63,8 @@ func assertAttributes(t testing.TB, want []log.KeyValue, r log.Record) { }) if !slices.EqualFunc(want, got, log.KeyValue.Equal) { t.Errorf("Attributes are not equal:\nwant: %v\ngot: %v", want, got) + return false } + + return true }