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:
- logr.Info and logr.V(0) are transformed to log.SeverityInfo.
- logr.V(1) is transformed to log.SeverityDebug.
- logr.V(2) and higher are transformed to log.SeverityTrace.
KeysAndValues values are transformed based on their type. The following types are supported:
- [bool] are transformed to log.BoolValue.
- [string] are transformed to log.StringValue.
- [int], [int8], [int16], [int32], [int64] are transformed to log.Int64Value.
- [uint], [uint8], [uint16], [uint32], [uint64], [uintptr] are transformed to log.Int64Value or log.StringValue if the value is too large.
- [float32], [float64] are transformed to log.Float64Value.
- time.Duration are transformed to log.Int64Value with the nanoseconds.
- [complex64], [complex128] are transformed to log.MapValue with the keys "r" and "i" for the real and imaginary parts. The values are log.Float64Value.
- time.Time are transformed to log.Int64Value with the nanoseconds.
- [[]byte] are transformed to log.BytesValue.
- [error] are transformed to log.StringValue with the error message.
- [nil] are transformed to an empty log.Value.
- [struct] are transformed to log.StringValue with the struct fields.
- [slice], [array] are transformed to log.SliceValue with the elements.
- [map] are transformed to log.MapValue with the key-value pairs.
- [pointer], [interface] are transformed to the dereferenced value.
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 ¶
- type LogSink
- func (l *LogSink) Enabled(level int) bool
- func (l *LogSink) Error(err error, msg string, keysAndValues ...any)
- func (l *LogSink) Info(level int, msg string, keysAndValues ...any)
- func (l *LogSink) Init(logr.RuntimeInfo)
- func (l LogSink) WithName(name string) logr.LogSink
- func (l LogSink) WithValues(keysAndValues ...any) logr.LogSink
- type Option
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 ¶
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 ¶
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) Init ¶
func (l *LogSink) Init(logr.RuntimeInfo)
Init receives optional information about the logr library this implementation does not use it.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures a LogSink.
func WithLevelSeverity ¶
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:
- logr.Info and logr.V(0) are transformed to log.SeverityInfo.
- logr.V(1) is transformed to log.SeverityDebug.
- logr.V(2) and higher are transformed to log.SeverityTrace.
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 ¶
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 ¶
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.