Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce logtest.AssertRecordEqual #5499

Merged
merged 6 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
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)
- `SimpleProcessor.OnEmit` in `go.opentelemetry.io/otel/sdk/log` no longer allocates a slice which makes it possible to have a zero-allocation log processing using `SimpleProcessor`. (#5493)
- The `AssertRecordEqual` method to `go.opentelemetry.io/otel/log/logtest` to allow comparison of two log records in tests. (#5499)

### Changed

Expand Down
70 changes: 70 additions & 0 deletions log/logtest/assertions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package logtest // import "go.opentelemetry.io/otel/log/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, 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

Check warning on line 20 in log/logtest/assertions.go

View check run for this annotation

Codecov / codecov/patch

log/logtest/assertions.go#L19-L20

Added lines #L19 - L20 were not covered by tests
}
if !want.ObservedTimestamp().Equal(got.ObservedTimestamp()) {
t.Errorf("ObservedTimestamp value is not equal:\nwant: %v\ngot: %v", want.ObservedTimestamp(), got.ObservedTimestamp())
return false

Check warning on line 24 in log/logtest/assertions.go

View check run for this annotation

Codecov / codecov/patch

log/logtest/assertions.go#L23-L24

Added lines #L23 - L24 were not covered by tests
}
if want.Severity() != got.Severity() {
t.Errorf("Severity value is not equal:\nwant: %v\ngot: %v", want.Severity(), got.Severity())
return false

Check warning on line 28 in log/logtest/assertions.go

View check run for this annotation

Codecov / codecov/patch

log/logtest/assertions.go#L27-L28

Added lines #L27 - L28 were not covered by tests
}
if want.SeverityText() != got.SeverityText() {
t.Errorf("SeverityText value is not equal:\nwant: %v\ngot: %v", want.SeverityText(), got.SeverityText())
return false

Check warning on line 32 in log/logtest/assertions.go

View check run for this annotation

Codecov / codecov/patch

log/logtest/assertions.go#L31-L32

Added lines #L31 - L32 were not covered by tests
}
if !assertBody(t, want.Body(), got) {
return false

Check warning on line 35 in log/logtest/assertions.go

View check run for this annotation

Codecov / codecov/patch

log/logtest/assertions.go#L35

Added line #L35 was not covered by tests
}

var attrs []log.KeyValue
want.WalkAttributes(func(kv log.KeyValue) bool {
attrs = append(attrs, kv)
return true
})
return assertAttributes(t, attrs, got)
}

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

Check warning on line 51 in log/logtest/assertions.go

View check run for this annotation

Codecov / codecov/patch

log/logtest/assertions.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}

return true
}

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 {
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)
return false

Check warning on line 66 in log/logtest/assertions.go

View check run for this annotation

Codecov / codecov/patch

log/logtest/assertions.go#L65-L66

Added lines #L65 - L66 were not covered by tests
}

return true
}
33 changes: 33 additions & 0 deletions log/logtest/assertions_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
46 changes: 0 additions & 46 deletions log/logtest/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package logtest

import (
"slices"
"testing"
"time"

Expand Down Expand Up @@ -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)
}
}
4 changes: 2 additions & 2 deletions log/logtest/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down