logtest
v0.23.0
go.opentelemetry.io/otel/sdk/log/logtest
Documentation
pkg.go.dev ↗Package logtest is a testing helper package.
Types
RecordFactory
type RecordFactory struct {
EventName string
Timestamp time.Time
ObservedTimestamp time.Time
Severity log.Severity
SeverityText string
Body attribute.Value
Attributes []attribute.KeyValue
TraceID trace.TraceID
SpanID trace.SpanID
TraceFlags trace.TraceFlags
Resource *resource.Resource
InstrumentationScope *instrumentation.Scope
DroppedAttributes int
}
RecordFactory is used to facilitate unit testing implementations of go.opentelemetry.io/otel/sdk/log.Exporter and go.opentelemetry.io/otel/sdk/log.Processor.
Do not use RecordFactory to create records in production code.
Example
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Package logtest is a testing helper package.
package logtest_test
import (
"context"
"fmt"
"io"
"os"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/log"
"go.opentelemetry.io/otel/sdk/log/logtest"
)
func ExampleRecordFactory() {
exp := exporter{os.Stdout}
rf := logtest.RecordFactory{
InstrumentationScope: &instrumentation.Scope{Name: "myapp"},
}
rf.Body = attribute.StringValue("foo")
r1 := rf.NewRecord()
rf.Body = attribute.StringValue("bar")
r2 := rf.NewRecord()
_ = exp.Export(context.Background(), []log.Record{r1, r2})
// Output:
// scope=myapp msg=foo
// scope=myapp msg=bar
}
// This is a compile-time check that exporter implements log.Exporter.
var _ log.Exporter = exporter{}
type exporter struct{ io.Writer }
func (e exporter) Export(_ context.Context, records []log.Record) error {
for i, r := range records {
if i != 0 {
if _, err := e.Write([]byte("\n")); err != nil {
return err
}
}
if _, err := fmt.Fprintf(e, "scope=%s msg=%s", r.InstrumentationScope().Name, r.Body().String()); err != nil {
return err
}
}
return nil
}
func (exporter) Shutdown(context.Context) error {
return nil
}
// ForceFlush returns nil.
func (exporter) ForceFlush(context.Context) error {
return nil
}
Output:
scope=myapp msg=foo scope=myapp msg=bar
RecordFactory.NewRecord
func (f RecordFactory) NewRecord() sdklog.Record
NewRecord returns a sdklog.Record configured from the values of f.