Documentation
¶
Index ¶
Constants ¶
const ( // TraceIDKey is the key used by the Otel handler // to inject the trace ID in the log record. TraceIDKey = "trace_id" // SpanIDKey is the key used by the Otel handler // to inject the span ID in the log record. SpanIDKey = "span_id" // SpanEventKey is the prefix key used by the Otel handler // to inject the log record in the recording span, as a span event. SpanEventKey = "log" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HandlerFn ¶
HandlerFn defines the handler used by slog.Handler as return value.
func NewOtelHandler ¶
func NewOtelHandler(opts ...OtelHandlerOpt) HandlerFn
NewOtelHandler creates and returns a new HandlerFn, which wraps a handler with OtelHandler to use with log/slog.
type OtelHandler ¶
type OtelHandler struct {
// Next represents the next handler in the chain.
Next slog.Handler
// NoBaggage determines whether to add context baggage members to the log record.
NoBaggage bool
// NoTraceEvents determines whether to record an event for every log on the active trace.
NoTraceEvents bool
}
OtelHandler is an implementation of slog's Handler interface. Its role is to ensure correlation between logs and OTel spans by:
1. Adding otel span and trace IDs to the log record. 2. Adding otel context baggage members to the log record. 3. Setting slog record as otel span event. 4. Adding slog record attributes to the otel span event. 5. Setting span status based on slog record level (only if >= slog.LevelError).
func New ¶
func New(next slog.Handler, opts ...OtelHandlerOpt) *OtelHandler
New creates a new OtelHandler to use with log/slog
func (OtelHandler) Enabled ¶
Enabled reports whether the logger emits log records at the given context and level. Note: We handover the decision down to the next handler.
func (OtelHandler) Handle ¶
Handle handles the provided log record and adds correlation between a slog record and an Open-Telemetry span.
type OtelHandlerOpt ¶
type OtelHandlerOpt func(handler *OtelHandler)
func WithNoBaggage ¶
func WithNoBaggage(noBaggage bool) OtelHandlerOpt
WithNoBaggage returns an OtelHandlerOpt, which sets the NoBaggage flag
func WithNoTraceEvents ¶
func WithNoTraceEvents(noTraceEvents bool) OtelHandlerOpt
WithNoTraceEvents returns an OtelHandlerOpt, which sets the NoTraceEvents flag