Skip to content

Handler attaching OpenTelemetry trace and resource details to logs

License

Notifications You must be signed in to change notification settings

go-slog/otelslog

Repository files navigation

OpenTelemetry handler

GitHub Workflow Status go.dev reference Go Version built with nix

log/slog handler attaching OpenTelemetry trace details to logs.

Installation

go get github.com/go-slog/otelslog

Usage

Wrap an existing handler:

import(
    "log/slog"

    "github.com/go-slog/otelslog"
)

var handler slog.Handler

// Set up your handler
// handler = ...

// Wrap Handler
handler = otelslog.NewHandler(handler)

logger := slog.New(handler)

// Call logger with a context

logger.InfoContext(ctx, "hello world")

// Output: level=INFO msg="hello world" trace_id=74726163655f69645f74657374313233 span_id=7370616e5f696431

Use it as a middleware in slogmulti.Pipe:

import (
    "github.com/go-slog/otelslog"
    "github.com/samber/slog-multi"
)

handler = slogmulti.Pipe(otelslog.Middleware()).Handler(handler)

// Set p logger
// ...

Development

Run tests:

go test -race -v ./...

Run linter:

golangci-lint run

License

The project is licensed under the MIT License.