mirror of
https://github.com/hanzoai/o11y.git
synced 2026-10-30 14:56:15 +00:00
Replace the nested `slog.Source` group with flat OTel semantic convention keys by adding a Source log handler wrapper that extracts source info from record.PC. Remove AddSource from the JSON handler since the wrapper handles it directly.
29 lines
560 B
Go
29 lines
560 B
Go
package loghandler
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"runtime"
|
|
)
|
|
|
|
type source struct{}
|
|
|
|
func NewSource() *source {
|
|
return &source{}
|
|
}
|
|
|
|
func (h *source) Wrap(next LogHandler) LogHandler {
|
|
return LogHandlerFunc(func(ctx context.Context, record slog.Record) error {
|
|
if record.PC != 0 {
|
|
frame, _ := runtime.CallersFrames([]uintptr{record.PC}).Next()
|
|
record.AddAttrs(
|
|
slog.String("code.filepath", frame.File),
|
|
slog.String("code.function", frame.Function),
|
|
slog.Int("code.lineno", frame.Line),
|
|
)
|
|
}
|
|
|
|
return next.Handle(ctx, record)
|
|
})
|
|
}
|