otellogr

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package otellogr provides a LogSink, a logr.LogSink implementation that can be used to bridge between the logr API and OpenTelemetry.

Record Conversion

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

  • Message is set as the Body using a log.StringValue.
  • Level is transformed and set as the Severity. The SeverityText is not set.
  • KeyAndValues are transformed and set as Attributes.
  • Error is always logged as an additional attribute with the key "exception.message" and with the severity log.SeverityError.
  • The context.Context value in KeyAndValues is propagated to OpenTelemetry log record. All non-nested context.Context values are ignored and not added as attributes. If there are multiple context.Context the last one is used.

The V-level is transformed by using the WithLevelSeverity option. If option is not provided then V-level is transformed in the following way:

KeysAndValues values are transformed based on their type. The following types are supported:

Example
package main

import (
	"github.com/go-logr/logr"

	"go.opentelemetry.io/contrib/bridges/otellogr"
	"go.opentelemetry.io/otel/log"
	"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 logr.Logger with *otellogr.LogSink and use it in your application.
	logr.New(otellogr.NewLogSink(
		"my/pkg/name",
		otellogr.WithLoggerProvider(provider),
		// Optionally, set the log level severity mapping.
		otellogr.WithLevelSeverity(func(level int) log.Severity {
			switch level {
			case 0:
				return log.SeverityInfo
			case 1:
				return log.SeverityDebug
			default:
				return log.SeverityTrace
			}
		}),
	))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LogSink

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

LogSink is a logr.LogSink that sends all logging records it receives to OpenTelemetry. See package documentation for how conversions are made.

func NewLogSink

func NewLogSink(name string, options ...Option) *LogSink

NewLogSink returns a new LogSink to be used as a logr.LogSink.

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

func (*LogSink) Enabled

func (l *LogSink) Enabled(level int) bool

Enabled tests whether this LogSink is enabled at the specified V-level. For example, commandline flags might be used to set the logging verbosity and disable some info logs.

func (*LogSink) Error

func (l *LogSink) Error(err error, msg string, keysAndValues ...any)

Error logs an error, with the given message and key/value pairs.

func (*LogSink) Info

func (l *LogSink) Info(level int, msg string, keysAndValues ...any)

Info logs a non-error message with the given key/value pairs.

func (*LogSink) Init

func (l *LogSink) Init(logr.RuntimeInfo)

Init receives optional information about the logr library this implementation does not use it.

func (LogSink) WithName

func (l LogSink) WithName(name string) logr.LogSink

WithName returns a new LogSink with the specified name appended.

func (LogSink) WithValues

func (l LogSink) WithValues(keysAndValues ...any) logr.LogSink

WithValues returns a new LogSink with additional key/value pairs.

type Option

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

Option configures a LogSink.

func WithLevelSeverity

func WithLevelSeverity(f func(int) log.Severity) Option

WithLevelSeverity returns an Option that configures the function used to convert logr levels to OpenTelemetry log severities.

By default if this Option is not provided, the LogSink will use a default conversion function that transforms in the following way:

func WithLoggerProvider

func WithLoggerProvider(provider log.LoggerProvider) Option

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

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

func WithSchemaURL

func WithSchemaURL(schemaURL string) Option

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

func WithVersion

func WithVersion(version string) Option

WithVersion returns an Option that configures the version of the log.Logger used by a LogSink. 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