otelslog

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: Apache-2.0 Imports: 13 Imported by: 63

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("my/pkg/name", otelslog.WithLoggerProvider(provider))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(name string, 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(name string, 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.

The provided name needs to uniquely identify the code being logged. This is most commonly the package name of the code. If name is empty, the log.Logger implementation may override this value with a default.

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 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.

func WithSchemaURL added in v0.2.0

func WithSchemaURL(schemaURL string) Option

WithSchemaURL returns an Option that configures the semantic convention schema URL of the log.Logger used by a Handler. The schemaURL should be the schema URL for the semantic conventions used in log records.

func WithSource added in v0.7.0

func WithSource(source bool) Option

WithSource returns an Option that configures the Handler to include the source location of the log record in log attributes.

func WithVersion added in v0.2.0

func WithVersion(version string) Option

WithVersion returns an Option that configures the version of the log.Logger used by a Handler. The version should be the version of the package that is being logged.

Jump to

Keyboard shortcuts

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