logtest

package module
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2026 License: Apache-2.0, BSD-3-Clause Imports: 8 Imported by: 2

README

Log Test SDK

PkgGoDev

Documentation

Overview

Package logtest is a testing helper package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RecordFactory

type RecordFactory struct {
	EventName         string
	Timestamp         time.Time
	ObservedTimestamp time.Time
	Severity          log.Severity
	SeverityText      string
	Body              log.Value
	Attributes        []log.KeyValue
	TraceID           trace.TraceID
	SpanID            trace.SpanID
	TraceFlags        trace.TraceFlags

	Resource             *resource.Resource
	InstrumentationScope *instrumentation.Scope

	DroppedAttributes         int
	AttributeValueLengthLimit int
	AttributeCountLimit       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
// Package logtest is a testing helper package.
package main

import (
	"context"
	"fmt"
	"io"
	"os"

	logapi "go.opentelemetry.io/otel/log"
	"go.opentelemetry.io/otel/sdk/instrumentation"
	"go.opentelemetry.io/otel/sdk/log"
	"go.opentelemetry.io/otel/sdk/log/logtest"
)

func main() {
	exp := exporter{os.Stdout}
	rf := logtest.RecordFactory{
		InstrumentationScope: &instrumentation.Scope{Name: "myapp"},
	}

	rf.Body = logapi.StringValue("foo")
	r1 := rf.NewRecord()

	rf.Body = logapi.StringValue("bar")
	r2 := rf.NewRecord()

	_ = exp.Export(context.Background(), []log.Record{r1, r2})

}

// Compile time check 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
}

// appropriate error should be returned in these situations.
func (exporter) ForceFlush(context.Context) error {
	return nil
}
Output:
scope=myapp msg=foo
scope=myapp msg=bar

func (RecordFactory) NewRecord

func (f RecordFactory) NewRecord() sdklog.Record

NewRecord returns a sdklog.Record configured from the values of f.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL