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

log/logtest: add Record Factory #5263

Merged
merged 9 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
See our [versioning policy](VERSIONING.md) for more information about these stability guarantees. (#5240)
- The `go.opentelemetry.io/otel/semconv/v1.25.0` package.
The package contains semantic conventions from the `v1.25.0` version of the OpenTelemetry Semantic Conventions. (#5254)
- Add `RecordFactory` in `go.opentelemetry.io/otel/log/logtest` to facilitate testing the bridge implementations. (#5263)

### Changed

Expand Down
1 change: 1 addition & 0 deletions log/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-logr/logr v1.4.1
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel v1.25.0
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
)

require (
Expand Down
2 changes: 2 additions & 0 deletions log/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2nTQVIEhZIptnrVb1XY=
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
37 changes: 37 additions & 0 deletions log/logtest/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Package logtest is a testing helper package.
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
package logtest // import "go.opentelemetry.io/otel/log/logtest"

import (
"time"

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

// RecordFactory is used to facilitate unit testing bridge implementations that
// make use of a [go.opentelemetry.io/otel/log.Record]
//
// Do not use RecordFactory to create records in production code.
type RecordFactory struct {
Timestamp time.Time
ObservedTimestamp time.Time
Severity log.Severity
SeverityText string
Body log.Value
Attributes []log.KeyValue
}

// NewRecord returns a log record.
func (b RecordFactory) NewRecord() log.Record {
var record log.Record
record.SetTimestamp(b.Timestamp)
record.SetObservedTimestamp(b.ObservedTimestamp)
record.SetSeverity(b.Severity)
record.SetSeverityText(b.SeverityText)
record.SetBody(b.Body)
record.AddAttributes(b.Attributes...)

return record
}
80 changes: 80 additions & 0 deletions log/logtest/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package logtest

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"golang.org/x/exp/slices"
dmathieu marked this conversation as resolved.
Show resolved Hide resolved

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

func TestRecordFactory(t *testing.T) {
now := time.Now()
observed := now.Add(time.Second)
severity := log.SeverityDebug
severityText := "DBG"
body := log.StringValue("Message")
attrs := []log.KeyValue{
log.Int("int", 1),
log.String("str", "foo"),
log.Float64("flt", 3.14),
}

got := RecordFactory{
Timestamp: now,
ObservedTimestamp: observed,
Severity: severity,
SeverityText: severityText,
Body: body,
Attributes: attrs,
}.NewRecord()

assert.Equal(t, now, got.Timestamp())
assert.Equal(t, observed, got.ObservedTimestamp())
assert.Equal(t, severity, got.Severity())
assert.Equal(t, severityText, got.SeverityText())
assert.Equal(t, body, got.Body())
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
assertAttributes(t, attrs, got)
}

func TestRecordFactoryMultiple(t *testing.T) {
now := time.Now()
attrs := []log.KeyValue{
log.Int("int", 1),
log.String("str", "foo"),
log.Float64("flt", 3.14),
}

f := RecordFactory{
Timestamp: now,
Attributes: attrs,
}

record1 := f.NewRecord()
f.Attributes = append(f.Attributes, log.Bool("added", true))

record2 := f.NewRecord()
assert.Equal(t, now, record2.Timestamp())
assertAttributes(t, append(attrs, log.Bool("added", true)), record2)

// Previously returned record is unharmed by the builder changes.
assert.Equal(t, now, record1.Timestamp())
assertAttributes(t, attrs, record1)
}

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)
}
}
2 changes: 0 additions & 2 deletions log/logtest/recorder.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Package logtest is a testing helper package. Users can retrieve an in-memory
// logger to verify the behavior of their integrations.
package logtest // import "go.opentelemetry.io/otel/log/logtest"

import (
Expand Down