otelslog

package module
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

README

otelslog

Otel tracing support for golang's log/slog package

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AKVContext added in v0.1.7

type AKVContext struct {
	// Key is the log field key the user used for the value.
	Key string
	// HandlerGroup is the list of group set by [slog.Logger.WithGroup]. handlerGroup is nil if the key is not
	// under any handler group.
	HandlerGroup []string
	// ElementGroup is the list of group names that the key is under.
	// ElementGroup does not include group set by [slog.Logger.WithGroup].
	//
	// `elementGroup` is nil if the key is not
	// under any group.
	ElementGroup []string
}

type AttributeKeyValuer added in v0.1.6

type AttributeKeyValuer interface {
	// AttributeKeyValue returns an OpenTelemetry attribute key and value.
	//
	// `key` is the log field key the user used for the value.
	//
	// `handlerGroup` is the list of group set by [slog.Logger.WithGroup]. handlerGroup is nil if the key is not
	// under any handler group.
	//
	// `elementGroup` is the list of group names that the key is under. `elementGroup` is nil if the key is not
	// under any group.
	AttributeKeyValue(ctx AKVContext) []attribute.KeyValue
}

type DurationValuerFunc

type DurationValuerFunc = func(d time.Duration) attribute.Value

type Handler

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

func New

func New(opts ...Option) *Handler

New returns a new Handler that wraps the default slog handler.

func NewWithHandler

func NewWithHandler(handler slog.Handler, opts ...Option) *Handler

NewWithHandler returns a new Handler that wraps the given handler.

func (*Handler) Enabled

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

Enabled implements the slog.Handler interface.

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, record slog.Record) error

Handle implements the slog.Handler interface.

func (*Handler) Handler

func (h *Handler) Handler() slog.Handler

Handler returns the wrapped handler.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements the slog.Handler interface.

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup implements the slog.Handler interface.

WithGroup adds prefix to the event name, NOT the log elements themselves when they are added to the span.

Group attribute for log elements will be added to the span as is.

Does not effect how the wrapped handler handles the log elements.

type LevelValuerFunc added in v0.1.1

type LevelValuerFunc = func(l slog.Leveler) attribute.Value

type Option

type Option func(h *Handler)

func WithDelimiter added in v0.1.6

func WithDelimiter(delim string) Option

WithDelimiter sets the delimiter to use when joining the log group.

func WithDurationSecondsValuer

func WithDurationSecondsValuer() Option

WithDurationSecondsValuer sets duration types to be formatted as seconds when being set as span's log event attribute.

func WithDurationValuer

func WithDurationValuer(f DurationValuerFunc) Option

WithDurationValuer sets how the duration is formatted when being set as span attribute.

Use this option if you want to use a custom duration format and none of the provided duration valuer suits your needs.

func WithLevelValuer added in v0.1.2

func WithLevelValuer(f LevelValuerFunc) Option

WithLevelValuer extracts the log level and set it as event attribute.

func WithLogEventName

func WithLogEventName(name string) Option

WithLogEventName sets the name of the log event attribute. Default is "log".

func WithSource added in v0.1.2

func WithSource(b bool) Option

WithSource enables the extraction of the caller location and set it as event attribute.

For formatting the output, see WithSourceKeyValuer.

func WithSourceKeyValuer added in v0.1.2

func WithSourceKeyValuer(f SourceKeyValuerFunc) Option

WithSourceKeyValuer extracts the caller location and set it as event attribute.

func WithStringDurationValuer

func WithStringDurationValuer() Option

WithStringDurationValuer sets duration types to be formatted as string when being set as span's log event attribute.

func WithTimeRFC3339NanoValuer

func WithTimeRFC3339NanoValuer() Option

WithTimeRFC3339NanoValuer sets time types to be formatted as RFC3339Nano when being set as span attribute.

func WithTimeRFC3339Valuer

func WithTimeRFC3339Valuer() Option

WithTimeRFC3339Valuer sets time types to be formatted as RFC3339 when being set as span attribute.

func WithTimeUnixNanoValuer

func WithTimeUnixNanoValuer() Option

WithTimeUnixNanoValuer sets time types to be formatted as Unix Nano Seconds when being set as span attribute.

func WithTimeUnixValuer

func WithTimeUnixValuer() Option

WithTimeUnixValuer sets time types to be formatted as Unix Seconds when being set as span attribute.

func WithTimeValuer

func WithTimeValuer(f TimeValuerFunc) Option

WithTimeValuer sets how the time is formatted when being set as span's log event attribute.

Use this option if you want to use a custom time format and none of the provided time formatters suits your needs.

func WithTraceIDKey added in v0.1.1

func WithTraceIDKey(key string) Option

WithTraceIDKey sets the key of the trace ID attribute to include in logs. Default is "trace_id".

type SourceKeyValuerFunc added in v0.1.2

type SourceKeyValuerFunc = func(source *slog.Source) []attribute.KeyValue

type TimeValuerFunc

type TimeValuerFunc = func(t time.Time) attribute.Value

Jump to

Keyboard shortcuts

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