Documentation
¶
Overview ¶
Package logtest is a testing helper package.
Example ¶
package main
import (
"testing"
"time"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/logtest"
)
func main() {
t := &testing.T{} // Provided by testing framework.
// Create a recorder.
rec := logtest.NewRecorder()
// Emit a log record (code under test).
l := rec.Logger("Example")
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(t.Context(), r)
// Verify that the expected and actual log records match.
want := logtest.Recording{
logtest.Scope{Name: "Example"}: []logtest.Record{
{
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,
logtest.Transform(func(r logtest.Record) logtest.Record {
r = r.Clone()
r.Context = nil // Ignore context.
r.Timestamp = time.Time{} // Ignore timestamp.
return r
}),
)
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertEqual ¶
func AssertEqual[T Recording | Record](t TestingT, want, got T, opts ...AssertOption) bool
AssertEqual asserts that the two concrete data-types from the logtest package are equal.
Types ¶
type AssertOption ¶
type AssertOption interface {
// contains filtered or unexported methods
}
AssertOption allows for fine grain control over how AssertEqual operates.
func Desc ¶
func Desc(text string, args ...any) AssertOption
Desc prepends the given text to an assertion failure message. The text is formatted with the args using fmt.Sprintf.
func Transform ¶
func Transform[A, B any](f func(A) B) AssertOption
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.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures a Recorder.
func WithEnabledFunc ¶
WithEnabledFunc allows configuring whether the Recorder is enabled for specific log entries or not.
By default, the Recorder is enabled for every log entry.
type Record ¶
type Record struct {
Context context.Context
EventName string
Timestamp time.Time
ObservedTimestamp time.Time
Severity log.Severity
SeverityText string
Body log.Value
Error error
Attributes []log.KeyValue
// contains filtered or unexported fields
}
Record represents the record alongside its context.
type Recorder ¶
type Recorder struct {
embedded.LoggerProvider
// contains filtered or unexported fields
}
Recorder stores all received log records in-memory. Recorder implements log.LoggerProvider.
func NewRecorder ¶
NewRecorder returns a new Recorder.
func (*Recorder) Logger ¶
Logger returns a copy of Recorder as a log.Logger with the provided scope information.
type Scope ¶
type Scope struct {
// Name is the name of the instrumentation scope. This should be the
// Go package name of that scope.
Name string
// Version is the version of the instrumentation scope.
Version string
// SchemaURL of the telemetry emitted by the scope.
SchemaURL string
// Attributes of the telemetry emitted by the scope.
Attributes attribute.Set
}
Scope represents the instrumentation scope.