-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
log/logtest: add Record Factory (#5263)
- Loading branch information
Showing
5 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters