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 values are transformed based on their type or into a string value encoded using fmt.Sprintf if there is no matching type.
- 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("my/pkg/name", 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.
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 ¶
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 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
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
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
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.