mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-03-29 21:47:00 +02:00
log/logtest: add Record Factory (#5263)
This commit is contained in:
parent
df455db04d
commit
f33d40886d
@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
||||
|
||||
### Added
|
||||
|
||||
- Add `RecordFactory` in `go.opentelemetry.io/otel/log/logtest` to facilitate testing the bridge implementations. (#5263)
|
||||
- Add `RecordFactory` in `go.opentelemetry.io/otel/sdk/log/logtest` to facilitate testing the exporter and processor implementations. (#5258)
|
||||
|
||||
## [1.26.0/0.48.0/0.2.0-alpha] 2024-04-24
|
||||
|
5
log/logtest/doc.go
Normal file
5
log/logtest/doc.go
Normal file
@ -0,0 +1,5 @@
|
||||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package logtest is a testing helper package.
|
||||
package logtest // import "go.opentelemetry.io/otel/log/logtest"
|
36
log/logtest/factory.go
Normal file
36
log/logtest/factory.go
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
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
|
||||
}
|
88
log/logtest/factory_test.go
Normal file
88
log/logtest/factory_test.go
Normal file
@ -0,0 +1,88 @@
|
||||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package logtest
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"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())
|
||||
assertBody(t, body, got)
|
||||
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 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)
|
||||
}
|
||||
}
|
@ -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 (
|
||||
|
Loading…
x
Reference in New Issue
Block a user