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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add `Values` method to `HeaderCarrier` to implement the new `ValuesGetter` interface in `go.opentelemetry.io/otel/propagation`. (#5973)
- Update `Baggage` in `go.opentelemetry.io/otel/propagation` to retrieve multiple values for a key when the carrier implements `ValuesGetter`. (#5973)
- Add `AssertEqual` function in `go.opentelemetry.io/otel/log/logtest`. (#6662)
- Add `Transform` option in `go.opentelemetry.io/otel/log/logtest`. (#6794)

### Removed

Expand Down
28 changes: 26 additions & 2 deletions log/logtest/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,24 @@ type testingT interface {
Errorf(format string, args ...any)
}

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

var cfg assertConfig
for _, opt := range opts {
cfg = opt.apply(cfg)
}

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.
}
cmpOpts = append(cmpOpts, cfg.cmpOpts...)

if diff := cmp.Diff(want, got, cmpOpts...); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
Expand All @@ -45,9 +51,27 @@ func assertEqual[T Recording | Record](t testingT, want, got T, _ ...AssertOptio
return true
}

type assertConfig struct{}
type assertConfig struct {
cmpOpts []cmp.Option
}

// AssertOption allows for fine grain control over how AssertEqual operates.
type AssertOption interface {
apply(cfg assertConfig) assertConfig
}

type fnOption func(cfg assertConfig) assertConfig

func (fn fnOption) apply(cfg assertConfig) assertConfig {
return fn(cfg)
}

// Transform applies a transformation f function that
// converts values of a certain type into that of another.
// f must not mutate A in any way.
func Transform[A, B any](f func(A) B) AssertOption {
return fnOption(func(cfg assertConfig) assertConfig {
cfg.cmpOpts = append(cfg.cmpOpts, cmp.Transformer("", f))
return cfg
})
}
16 changes: 16 additions & 0 deletions log/logtest/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ func TestAssertEqualRecord(t *testing.T) {
},
want: false,
},
{
name: "Transform to ignore timestamps",
a: Record{
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")},
},
b: Record{
Timestamp: y2k,
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)},
},
opts: []AssertOption{
Transform(func(time.Time) time.Time {
return time.Time{}
}),
},
want: true,
},
}

for _, tc := range tests {
Expand Down
55 changes: 55 additions & 0 deletions log/logtest/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package logtest_test

import (
"context"
"testing"
"time"

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

func Example() {
t := &testing.T{} // Provided by testing framework.

// Create a recorder.
rec := logtest.NewRecorder()

// Emit a log record (code under test).
l := rec.Logger("Example")
ctx := context.Background()
r := log.Record{}
r.SetTimestamp(time.Now())
r.SetSeverity(log.SeverityInfo)
r.SetBody(log.StringValue("Hello there"))
r.AddAttributes(log.String("foo", "bar"))
r.AddAttributes(log.Int("n", 1))
l.Emit(ctx, r)

// Verify that the expected and actual log records match.
want := logtest.Recording{
logtest.Scope{Name: "Example"}: []logtest.Record{
{
Context: context.Background(),
Severity: log.SeverityInfo,
Body: log.StringValue("Hello there"),
Attributes: []log.KeyValue{
log.Int("n", 1),
log.String("foo", "bar"),
},
},
},
}
got := rec.Result()
logtest.AssertEqual(t, want, got,
// Ignore Timestamps.
logtest.Transform(func(time.Time) time.Time {
return time.Time{}
}),
)
// Output:
//
}
Loading