Skip to content
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add `WithHTTPClient` option to configure the `http.Client` used by `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`. (#6751)
- Add `WithHTTPClient` option to configure the `http.Client` used by `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`. (#6752)
- Add `WithHTTPClient` option to configure the `http.Client` used by `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#6688)
- Add `AssertEqual` function in `go.opentelemetry.io/otel/log/logtest`. (#6662)

### Removed

- Drop support for [Go 1.22]. (#6381, #6418)
- Remove `Resource` field from `EnabledParameters` in `go.opentelemetry.io/otel/sdk/log`. (#6494)
- Remove `RecordFactory` type from `go.opentelemetry.io/otel/log/logtest`. (#6492)
- Remove `RecordFactory` type from `go.opentelemetry.io/otel/log/logtest`. (#6492)
- Remove `ScopeRecords`, `EmittedRecord`, and `RecordFactory` types from `go.opentelemetry.io/otel/log/logtest`. (#6507)
- Remove `AssertRecordEqual` function in `go.opentelemetry.io/otel/log/logtest`, use `AssertEqual` instead. (#6662)

### Changed

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

package logtest // import "go.opentelemetry.io/otel/log/logtest"

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

"go.opentelemetry.io/otel/log"
)

// AssertEqual asserts that the two concrete data-types from the logtest package are equal.
func AssertEqual[T Recording | Record](t *testing.T, want, got T, opts ...AssertOption) bool {
t.Helper()
return assertEqual(t, want, got, opts...)
}

// testingT reports failure messages.
// *testing.T implements this interface.
type testingT interface {
Errorf(format string, args ...any)
}

func assertEqual[T Recording | Record](t testingT, want, got T, _ ...AssertOption) bool {
if h, ok := t.(interface{ Helper() }); ok {
h.Helper()
}

cmpOpts := []cmp.Option{
cmp.Comparer(func(x, y context.Context) bool { return x == y }), // Compare context.
cmpopts.SortSlices(
func(a, b log.KeyValue) bool { return a.Key < b.Key },
), // Unordered compare of the key values.
cmpopts.EquateEmpty(), // Empty and nil collections are equal.
}

if diff := cmp.Diff(want, got, cmpOpts...); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
return false
}
return true
}

type assertConfig struct{}

// AssertOption allows for fine grain control over how AssertEqual operates.
type AssertOption interface {
apply(cfg assertConfig) assertConfig
}
172 changes: 172 additions & 0 deletions log/logtest/assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package logtest

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/log"
)

var y2k = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)

type mockTestingT struct {
errors []string
}

func (m *mockTestingT) Errorf(format string, args ...any) {
m.errors = append(m.errors, format)
}

func TestAssertEqual(t *testing.T) {
a := Recording{
Scope{Name: t.Name()}: []Record{
{Body: log.StringValue("msg"), Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)}},
},
}
b := Recording{
Scope{Name: t.Name()}: []Record{
{Body: log.StringValue("msg"), Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")}},
},
}

got := AssertEqual(t, a, b)
assert.True(t, got, "expected recordings to be equal")
}

func TestAssertEqualRecording(t *testing.T) {
tests := []struct {
name string
a Recording
b Recording
opts []AssertOption
want bool
}{
{
name: "equal recordings",
a: Recording{
Scope{Name: t.Name()}: []Record{
{
Timestamp: y2k,
Context: context.Background(),
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")},
},
},
},
b: Recording{
Scope{Name: t.Name()}: []Record{
{
Timestamp: y2k,
Context: context.Background(),
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)},
},
},
},
want: true,
},
{
name: "different recordings",
a: Recording{
Scope{Name: t.Name()}: []Record{
{Attributes: []log.KeyValue{log.String("foo", "bar")}},
},
},
b: Recording{
Scope{Name: t.Name()}: []Record{
{Attributes: []log.KeyValue{log.Int("n", 1)}},
},
},
want: false,
},
{
name: "equal empty scopes",
a: Recording{
Scope{Name: t.Name()}: nil,
},
b: Recording{
Scope{Name: t.Name()}: []Record{},
},
want: true,
},
{
name: "equal empty attributes",
a: Recording{
Scope{Name: t.Name()}: []Record{
{Body: log.StringValue("msg"), Attributes: []log.KeyValue{}},
},
},
b: Recording{
Scope{Name: t.Name()}: []Record{
{Body: log.StringValue("msg"), Attributes: nil},
},
},
want: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mockT := &mockTestingT{}
result := assertEqual(mockT, tc.a, tc.b, tc.opts...)
if result != tc.want {
t.Errorf("AssertEqual() = %v, want %v", result, tc.want)
}
if !tc.want && len(mockT.errors) == 0 {
t.Errorf("expected Errorf call but got none")
}
})
}
}

func TestAssertEqualRecord(t *testing.T) {
tests := []struct {
name string
a Record
b Record
opts []AssertOption
want bool
}{
{
name: "equal records",
a: Record{
Timestamp: y2k,
Context: context.Background(),
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")},
},
b: Record{
Timestamp: y2k,
Context: context.Background(),
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)},
},
want: true,
},
{
name: "different records",
a: Record{
Attributes: []log.KeyValue{log.String("foo", "bar")},
},
b: Record{
Attributes: []log.KeyValue{log.Int("n", 1)},
},
want: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mockT := &mockTestingT{}
result := assertEqual(mockT, tc.a, tc.b, tc.opts...)
if result != tc.want {
t.Errorf("AssertEqual() = %v, want %v", result, tc.want)
}
if !tc.want && len(mockT.errors) == 0 {
t.Errorf("expected Errorf call but got none")
}
})
}
}
78 changes: 0 additions & 78 deletions log/logtest/assertions.go

This file was deleted.

34 changes: 0 additions & 34 deletions log/logtest/assertions_test.go

This file was deleted.

1 change: 1 addition & 0 deletions log/logtest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module go.opentelemetry.io/otel/log/logtest
go 1.23.0

require (
github.com/google/go-cmp v0.7.0
github.com/stretchr/testify v1.10.0
go.opentelemetry.io/otel v1.35.0
go.opentelemetry.io/otel/log v0.11.0
Expand Down
Loading