Skip to content

Latest commit

 

History

History
75 lines (48 loc) · 1.59 KB

README.md

File metadata and controls

75 lines (48 loc) · 1.59 KB

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.