Skip to content

Commit

Permalink
Use pkg name from source as scope by default
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Mar 12, 2024
1 parent 20f6c0e commit 40b9b00
Showing 1 changed file with 48 additions and 11 deletions.
59 changes: 48 additions & 11 deletions bridges/sloghandler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"context"
"fmt"
"log/slog"
"runtime"
"slices"
"strings"
"sync"

"go.opentelemetry.io/otel/log"
Expand All @@ -22,7 +24,18 @@ const (
bridgeVersion = "0.0.1-alpha"
)

type config struct{}
type config struct {
// TODO: add option for users to configure this.
loggerFunc func(context.Context, slog.Record) (name string, options []log.LoggerOption)
}

func newConfig(options []Option) config {
var c config
for _, opt := range options {
c = opt.apply(c)

Check warning on line 35 in bridges/sloghandler/handler.go

View check run for this annotation

Codecov / codecov/patch

bridges/sloghandler/handler.go#L35

Added line #L35 was not covered by tests
}
return c
}

// Option configures a [Handler].
type Option interface {
Expand All @@ -35,9 +48,9 @@ type Handler struct {
// Ensure forward compatibility by explicitly making this not comparable.
noCmp [0]func() //nolint: unused // This is indeed used.

attrs *kvBuffer
group *group
logger log.Logger
attrs *kvBuffer
group *group
logFn func(context.Context, slog.Record) log.Logger
}

// Compile-time check *Handler implements slog.Handler.
Expand All @@ -46,22 +59,46 @@ var _ slog.Handler = (*Handler)(nil)
// New returns a new [Handler] to be used as an [slog.Handler].
//
// If lp is nil, [noop.LoggerProvider] will be used as the default.
func New(lp log.LoggerProvider, _ ...Option) *Handler {
func New(lp log.LoggerProvider, options ...Option) *Handler {
if lp == nil {
// Do not panic.
lp = noop.NewLoggerProvider()
}
return &Handler{
logger: lp.Logger(
bridgeName,
log.WithInstrumentationVersion(bridgeVersion),
),

c := newConfig(options)

var logFn func(context.Context, slog.Record) log.Logger
if c.loggerFunc != nil {
logFn = func(ctx context.Context, r slog.Record) log.Logger {
name, opts := c.loggerFunc(ctx, r)
return lp.Logger(name, opts...)

Check warning on line 74 in bridges/sloghandler/handler.go

View check run for this annotation

Codecov / codecov/patch

bridges/sloghandler/handler.go#L72-L74

Added lines #L72 - L74 were not covered by tests
}
} else {
logFn = defaultLogFn(lp)
}

return &Handler{logFn: logFn}
}

func defaultLogFn(lp log.LoggerProvider) func(context.Context, slog.Record) log.Logger {
return func(_ context.Context, r slog.Record) log.Logger {
fs := runtime.CallersFrames([]uintptr{r.PC})
f, _ := fs.Next()
return lp.Logger(parsePkg(f.Function, "log/slog"))
}
}

func parsePkg(fName, fallback string) string {
pkg, _, found := strings.Cut(fName, ".")
if !found {
return fallback
}
return pkg
}

// Handle handles the passed record.
func (h *Handler) Handle(ctx context.Context, record slog.Record) error {
h.logger.Emit(ctx, h.convertRecord(record))
h.logFn(ctx, record).Emit(ctx, h.convertRecord(record))
return nil
}

Expand Down

0 comments on commit 40b9b00

Please sign in to comment.