Documentation ¶
Overview ¶
Package otelzerolog provides a function to extend structured logs using zerolog with the Open Telemetry trace related context.
The github.com/rs/zerolog zerolog.Event is decorated with standard metadata extracted from the trace.SpanContext, a traceID, spanID and additional information is injected into a log.
The initialization uses file level configuration to set defaults for the function to use. SetLogOptions can overwrite the defaults.
When the configuration is done AddTracingContext and AddTracingContextWithAttributes decorate zerolog logs with data from the trace context. A zeroLog.Event can be passed by using log.Info().Func(AddTracingContext(span)).Msg("") for example.
Functions
func SetLogOptions(options ...LogOption) func WithTraceID(traceID string) LogOption func WithSpanID(spanID string) LogOption func WithServiceName(serviceName string) LogOption func WithAttributePrefix(prefix string) LogOption func WithAttributes(attributes ...attribute.KeyValue) LogOption func AddTracingContext(span trace.Span, err ...error) func(event *zerolog.Event) func AddTracingContextWithAttributes(span trace.Span, attributes []attribute.KeyValue, err ...error) func(event *zerolog.Event)
Types
type LogOption func(*logConfig)
Structs
type logConfig struct { attributes []attribute.KeyValue serviceName string traceId string spanId string attributePrefix string }
import "github.com/vincentfree/opentelemetry/otelzerolog"
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddTracingContext ¶
AddTracingContext lets you add the trace context to a structured log
Example ¶
package main import ( "context" "errors" "github.com/rs/zerolog/log" "github.com/vincentfree/opentelemetry/otelzerolog" "go.opentelemetry.io/otel" ) func main() { tracer := otel.Tracer("otelzerolog/example") _, span := tracer.Start(context.Background(), "example-span") log.Info().Func(otelzerolog.AddTracingContext(span)).Msg("in case of a success") // or in the case of an error err := errors.New("example error") log.Error().Func(otelzerolog.AddTracingContext(span, err)).Msg("in case of a failure") }
Output:
func AddTracingContextWithAttributes ¶
func AddTracingContextWithAttributes(span trace.Span, attributes []attribute.KeyValue, err ...error) func(event *zerolog.Event)
AddTracingContextWithAttributes lets you add the trace context to a structured log, including attribute.KeyValue's to extend the log
Example ¶
package main import ( "context" "errors" "github.com/rs/zerolog/log" "github.com/vincentfree/opentelemetry/otelzerolog" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" ) func main() { tracer := otel.Tracer("otelzerolog/example") _, span := tracer.Start(context.Background(), "example-span") attributes := []attribute.KeyValue{ attribute.String("exampleKey", "exampleValue"), attribute.Bool("isValid", true), } log.Info().Func(otelzerolog.AddTracingContextWithAttributes(span, attributes)).Msg("in case of a success") // or in the case of an error err := errors.New("example error") log.Error().Func(otelzerolog.AddTracingContextWithAttributes(span, attributes, err)).Msg("in case of a failure") }
Output:
func SetLogOptions ¶
func SetLogOptions(options ...LogOption)
SetLogOptions takes LogOption's and overwrites library defaults
Example ¶
package main import ( "context" "errors" "github.com/rs/zerolog/log" "github.com/vincentfree/opentelemetry/otelzerolog" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" ) func main() { option := otelzerolog.WithAttributes(attribute.String("test", "value"), attribute.Bool("isValid", true)) // use of SetLogOptions otelzerolog.SetLogOptions(option) // set up tracer tracer := otel.Tracer("otelzerolog/example") _, span := tracer.Start(context.Background(), "example-span") // pass span to AddTracingContext log.Info().Func(otelzerolog.AddTracingContext(span)).Msg("in case of a success") // or in the case of an error err := errors.New("example error") log.Error().Func(otelzerolog.AddTracingContext(span, err)).Msg("in case of a failure") }
Output:
Types ¶
type LogOption ¶
type LogOption func(*logConfig)
LogOption takes a logConfig struct and applies changes. It can be passed to the SetLogOptions function to configure a logConfig struct.
func WithAttributePrefix ¶
WithAttributePrefix updates the default 'trace.attribute' attribute prefix
Example ¶
package main import ( "github.com/vincentfree/opentelemetry/otelzerolog" ) func main() { otelzerolog.SetLogOptions(otelzerolog.WithAttributePrefix("prefix")) // use AddTracingContext or AddTracingContextWithAttributes }
Output:
func WithAttributes ¶
WithAttributes adds global attributes that will be added to all structured logs. attributes have a prefix followed by the key of the attribute.
Example: if the attribute is of type string and the key is: 'http.method' then in the log it uses the default(but over-writable) 'trace.attribute' followed by 'http.method' so the end result is: 'trace.attribute.http.method'
Example ¶
package main import ( "context" "errors" "github.com/rs/zerolog/log" "github.com/vincentfree/opentelemetry/otelzerolog" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" ) func main() { option := otelzerolog.WithAttributes(attribute.String("test", "value"), attribute.Bool("isValid", true)) otelzerolog.SetLogOptions(option) tracer := otel.Tracer("otelzerolog/example") _, span := tracer.Start(context.Background(), "example-span") log.Info().Func(otelzerolog.AddTracingContext(span)).Msg("in case of a success") // or in the case of an error err := errors.New("example error") log.Error().Func(otelzerolog.AddTracingContext(span, err)).Msg("in case of a failure") }
Output:
func WithServiceName ¶
WithServiceName adds a service name to the field 'service.name' in your structured logs
Example ¶
package main import ( "github.com/vincentfree/opentelemetry/otelzerolog" ) func main() { otelzerolog.SetLogOptions(otelzerolog.WithServiceName("example-service")) // use AddTracingContext or AddTracingContextWithAttributes }
Output:
func WithSpanID ¶
WithSpanID overwrites the default 'spanID' field in the structured logs with your own key
Example ¶
package main import ( "github.com/vincentfree/opentelemetry/otelzerolog" ) func main() { otelzerolog.SetLogOptions(otelzerolog.WithSpanID("span-id")) // use AddTracingContext or AddTracingContextWithAttributes }
Output:
func WithTraceID ¶
WithTraceID overwrites the default 'traceID' field in the structured logs with your own key
Example ¶
package main import ( "github.com/vincentfree/opentelemetry/otelzerolog" ) func main() { otelzerolog.SetLogOptions(otelzerolog.WithTraceID("trace-id")) // use AddTracingContext or AddTracingContextWithAttributes }
Output: