baggagecopy – go.opentelemetry.io/contrib/processors/baggagecopy Index | Examples | Files

package baggagecopy

import "go.opentelemetry.io/contrib/processors/baggagecopy"

Package baggagecopy is an OpenTelemetry Span Processor and Log Record Processor that reads key/values stored in Baggage in context provided to copy onto the span or log.

The SpanProcessor retrieves Baggage from the starting span's parent context and adds them as attributes to the span.

Keys and values added to Baggage will appear on all subsequent child spans for a trace within this service and will be propagated to external services via propagation headers. If the external services also have a Baggage span processor, the keys and values will appear in those child spans as well.

The LogProcessor retrieves Baggage from the the context provided when emitting the log and adds them as attributes to the log. Baggage may be propagated to external services via propagation headers. and be used to add context to logs if the service also has a Baggage log processor.

Do not put sensitive information in Baggage.

Usage

Add the span processor when configuring the tracer provider.

Add the log processor when configuring the logger provider.

The convenience function [AllowAllBaggageKeys] is provided to allow all baggage keys to be copied. Alternatively, you can provide a custom baggage key predicate to select which baggage keys you want to copy.

Index

Examples

Types

type Filter

type Filter func(member baggage.Member) bool

Filter returns true if the baggage member should be added to a span.

var AllowAllMembers Filter = func(baggage.Member) bool { return true }

AllowAllMembers allows all baggage members to be added to a span.

type LogProcessor

type LogProcessor struct {
	// contains filtered or unexported fields
}

LogProcessor is a log.Processor implementation that adds baggage members onto a log as attributes.

func NewLogProcessor

func NewLogProcessor(filter Filter) *LogProcessor

NewLogProcessor returns a new LogProcessor.

The Baggage log processor adds attributes to a log record that are found in Baggage in the parent context at the moment the log is emitted. The passed filter determines which baggage members are added to the span.

If filter is nil, all baggage members will be added.

Example (AllKeys)

Code:play 

package main

import (
	"go.opentelemetry.io/contrib/processors/baggagecopy"
	"go.opentelemetry.io/otel/sdk/log"
)

func main() {
	log.NewLoggerProvider(
		log.WithProcessor(baggagecopy.NewLogProcessor(baggagecopy.AllowAllMembers)),
	)
}
Example (KeysMatchingRegex)

Code:play 

package main

import (
	"regexp"

	"go.opentelemetry.io/contrib/processors/baggagecopy"
	"go.opentelemetry.io/otel/baggage"
	"go.opentelemetry.io/otel/sdk/log"
)

func main() {
	expr := regexp.MustCompile(`^key.+`)
	log.NewLoggerProvider(
		log.WithProcessor(
			baggagecopy.NewLogProcessor(
				func(m baggage.Member) bool {
					return expr.MatchString(m.Key())
				},
			),
		),
	)
}
Example (KeysWithPrefix)

Code:play 

package main

import (
	"strings"

	"go.opentelemetry.io/contrib/processors/baggagecopy"
	"go.opentelemetry.io/otel/baggage"
	"go.opentelemetry.io/otel/sdk/log"
)

func main() {
	log.NewLoggerProvider(
		log.WithProcessor(
			baggagecopy.NewLogProcessor(
				func(m baggage.Member) bool {
					return strings.HasPrefix(m.Key(), "my-key")
				},
			),
		),
	)
}

func (LogProcessor) ForceFlush

func (processor LogProcessor) ForceFlush(context.Context) error

ForceFlush is called to ensure all logs are flushed to the output and is a no-op for this processor.

func (LogProcessor) OnEmit

func (processor LogProcessor) OnEmit(ctx context.Context, record *log.Record) error

OnEmit adds Baggage member to a log record as attributes that are pulled from the Baggage found in ctx. Baggage members are filtered by the filter passed to NewLogProcessor.

func (LogProcessor) Shutdown

func (processor LogProcessor) Shutdown(context.Context) error

Shutdown is called when the log.Processor is shutting down and is a no-op for this processor.

type SpanProcessor

type SpanProcessor struct {
	// contains filtered or unexported fields
}

SpanProcessor is a trace.SpanProcessor implementation that adds baggage members onto a span as attributes.

func NewSpanProcessor

func NewSpanProcessor(filter Filter) *SpanProcessor

NewSpanProcessor returns a new SpanProcessor.

The Baggage span processor duplicates onto a span the attributes found in Baggage in the parent context at the moment the span is started. The passed filter determines which baggage members are added to the span.

If filter is nil, all baggage members will be added.

Example (AllKeys)

Code:play 

package main

import (
	"go.opentelemetry.io/contrib/processors/baggagecopy"
	"go.opentelemetry.io/otel/sdk/trace"
)

func main() {
	trace.NewTracerProvider(
		trace.WithSpanProcessor(baggagecopy.NewSpanProcessor(baggagecopy.AllowAllMembers)),
	)
}
Example (KeysMatchingRegex)

Code:play 

package main

import (
	"regexp"

	"go.opentelemetry.io/contrib/processors/baggagecopy"
	"go.opentelemetry.io/otel/baggage"
	"go.opentelemetry.io/otel/sdk/trace"
)

func main() {
	expr := regexp.MustCompile(`^key.+`)
	trace.NewTracerProvider(
		trace.WithSpanProcessor(
			baggagecopy.NewSpanProcessor(
				func(m baggage.Member) bool {
					return expr.MatchString(m.Key())
				},
			),
		),
	)
}
Example (KeysWithPrefix)

Code:play 

package main

import (
	"strings"

	"go.opentelemetry.io/contrib/processors/baggagecopy"
	"go.opentelemetry.io/otel/baggage"
	"go.opentelemetry.io/otel/sdk/trace"
)

func main() {
	trace.NewTracerProvider(
		trace.WithSpanProcessor(
			baggagecopy.NewSpanProcessor(
				func(m baggage.Member) bool {
					return strings.HasPrefix(m.Key(), "my-key")
				},
			),
		),
	)
}

func (SpanProcessor) ForceFlush

func (processor SpanProcessor) ForceFlush(context.Context) error

ForceFlush exports all ended spans to the configured Exporter that have not yet been exported and is a no-op for this processor.

func (SpanProcessor) OnEnd

func (processor SpanProcessor) OnEnd(s trace.ReadOnlySpan)

OnEnd is called when span is finished and is a no-op for this processor.

func (SpanProcessor) OnStart

func (processor SpanProcessor) OnStart(ctx context.Context, span trace.ReadWriteSpan)

OnStart is called when a span is started and adds span attributes for baggage contents.

func (SpanProcessor) Shutdown

func (processor SpanProcessor) Shutdown(context.Context) error

Shutdown is called when the SDK shuts down and is a no-op for this processor.

Source Files

doc.go log_processor.go processor.go

Version
v0.9.0 (latest)
Published
May 22, 2025
Platform
js/wasm
Imports
6 packages
Last checked
now

Tools for package owners.