otelslog

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package otelslog provides Handler, an slog.Handler implementation, that can be used to bridge between the log/slog API and OpenTelemetry.

Record Conversion

The slog.Record are converted to OpenTelemetry log.Record in the following way:

  • Time is set as the Timestamp.
  • Message is set as the Body using a log.StringValue.
  • Level is transformed and set as the Severity. The SeverityText is not set.
  • PC is dropped.
  • Attr are transformed and set as the Attributes.

The Level is transformed by using the static offset to the OpenTelemetry Severity types. For example:

Attribute values are transformed based on their slog.Kind:

Example
package main

import (
	"go.opentelemetry.io/contrib/bridges/otelslog"
	"go.opentelemetry.io/otel/log/noop"
)

func main() {
	// Use a working LoggerProvider implementation instead e.g. using go.opentelemetry.io/otel/sdk/log.
	provider := noop.NewLoggerProvider()

	// Create an *slog.Logger and use it in your application.
	otelslog.NewLogger(otelslog.WithLoggerProvider(provider))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(options ...Option) *slog.Logger

NewLogger returns a new slog.Logger backed by a new Handler. See NewHandler for details on how the backing Handler is created.

Types

type Handler

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

Handler is an slog.Handler that sends all logging records it receives to OpenTelemetry. See package documentation for how conversions are made.

func NewHandler

func NewHandler(options ...Option) *Handler

NewHandler returns a new Handler to be used as an slog.Handler.

If WithLoggerProvider is not provided, the returned Handler will use the global LoggerProvider.

By default the returned Handler will use a log.Logger that is identified with this bridge package information. WithInstrumentationScope should be used to override this with details about the package or module the handler will instrument.

func (*Handler) Enabled

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

Enable returns true if the Handler is enabled to log for the provided context and Level. Otherwise, false is returned if it is not enabled.

func (*Handler) Handle

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

Handle handles the passed record.

func (*Handler) WithAttrs

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

WithAttrs returns a new slog.Handler based on h that will log using the passed attrs.

func (*Handler) WithGroup

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

WithGroup returns a new slog.Handler based on h that will log all messages and attributes within a group of the provided name.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures a Handler.

func WithInstrumentationScope

func WithInstrumentationScope(scope instrumentation.Scope) Option

WithInstrumentationScope returns an Option that configures the scope of the log.Logger used by a Handler.

By default if this Option is not provided, the Handler will use a default instrumentation scope describing this bridge package. It is recommended to provide this so log data can be associated with its source package or module.

func WithLoggerProvider

func WithLoggerProvider(provider log.LoggerProvider) Option

WithLoggerProvider returns an Option that configures log.LoggerProvider used by a Handler to create its log.Logger.

By default if this Option is not provided, the Handler will use the global LoggerProvider.

Jump to

Keyboard shortcuts

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