Documentation ¶
Overview ¶
Package otelslog provides an slog.Handler that attaches OpenTelemetry trace details to logs.
Package otelslog provides an slog.Handler that attaches OpenTelemetry trace details to logs.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
Middleware returns a Middleware for an slogmulti.Pipe handler.
func NewHandler ¶
NewHandler returns a new Handler.
func ResourceMiddleware ¶ added in v0.3.0
ResourceMiddleware returns a Middleware for an slogmulti.Pipe handler attaching resource attributes to an slog.Handler.
func WithResource ¶ added in v0.1.0
WithResource returns an slog.Handler with all attributes attached from a resource.Resource.
Example ¶
// Set up handler handler := testHandler() res, _ := resource.New( context.Background(), resource.WithAttributes( attribute.String("service.name", "my-service"), attribute.String("service.version", "1.0.0"), attribute.Bool("debug", false), attribute.Float64("sampling", 0.6), ), ) // Attach resource details to handler handler = otelslog.WithResource(handler, res) // Set up logger logger := slog.New(handler) // Call logger logger.Info("hello world")
Output: time=0001-01-01T00:00:00.000Z level=INFO msg="hello world" debug=false sampling=0.6 service.name=my-service service.version=1.0.0
Types ¶
type Handler ¶
Handler attaches details from an OpenTelemetry trace to each log record.
Example ¶
package main import ( "context" "log/slog" "os" "time" "go.opentelemetry.io/otel/trace" "github.com/go-slog/otelslog" ) func testHandler() slog.Handler { return slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ AddSource: false, Level: nil, ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr { if a.Key == slog.TimeKey { a.Value = slog.TimeValue(time.Time{}) } return a }, }) } func testContext() context.Context { ctx := context.Background() spanContext := trace.NewSpanContext(trace.SpanContextConfig{ TraceID: [16]byte{116, 114, 97, 99, 101, 95, 105, 100, 95, 116, 101, 115, 116, 49, 50, 51}, SpanID: [8]byte{115, 112, 97, 110, 95, 105, 100, 49}, }) ctx = trace.ContextWithSpanContext(ctx, spanContext) return ctx } func main() { var handler slog.Handler // Set up handler handler = testHandler() // Wrap handler handler = otelslog.NewHandler(handler) // Set up logger logger := slog.New(handler) // later in some trace context (eg. http request) ctx := testContext() // Call logger with a context logger.InfoContext(ctx, "hello world") }
Output: time=0001-01-01T00:00:00.000Z level=INFO msg="hello world" trace_id=74726163655f69645f74657374313233 span_id=7370616e5f696431
func (Handler) Handle ¶
Handle implements slog.Handler.
func (Handler) WithAttrs ¶
WithAttrs implements slog.Handler.
Click to show internal directories.
Click to hide internal directories.