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:
- slog.LevelDebug is transformed to log.SeverityDebug
- slog.LevelInfo is transformed to log.SeverityInfo
- slog.LevelWarn is transformed to log.SeverityWarn
- slog.LevelError is transformed to log.SeverityError
Attribute values are transformed based on their slog.Kind:
- slog.KindAny are transformed to log.StringValue. The value is encoded using fmt.Sprintf.
- slog.KindBool are transformed to log.BoolValue directly.
- slog.KindDuration are transformed to log.Int64Value as nanoseconds.
- slog.KindFloat64 are transformed to log.Float64Value directly.
- slog.KindInt64 are transformed to log.Int64Value directly.
- slog.KindString are transformed to log.StringValue directly.
- slog.KindTime are transformed to log.Int64Value as nanoseconds since the Unix epoch.
- slog.KindUint64 are transformed to log.Int64Value using int64 conversion.
- slog.KindGroup are transformed to log.MapValue using appropriate transforms for each group value.
- slog.KindLogValuer the value is resolved and then transformed.
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 ¶
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 ¶
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 ¶
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) WithAttrs ¶
WithAttrs returns a new slog.Handler based on h that will log using the passed attrs.
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.