Skip to content

Commit

Permalink
refactor log (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong authored Apr 28, 2024
1 parent f80400a commit 9ef9791
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions log/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,23 @@ func New(opts ...Option) *slog.Logger {
return slog.Default()
}

var traceOpt []otel.Option
var handler slog.Handler = rate.New(option.handler)

if option.asTraceEvent {
// If the logger is configured to log as trace event, it disables sampling.
// However, sampling handler still can buffer and logs if there is a error log,
// or there is no valid trace context.
option.sampler = func(ctx context.Context) bool { return !trace.SpanContextFromContext(ctx).IsValid() }
traceOpt = append(traceOpt, otel.WithRecordEvent(true))
handler = sampling.New(handler, func(ctx context.Context) bool {
return !trace.SpanContextFromContext(ctx).IsValid()
})
handler = otel.New(handler, otel.WithRecordEvent(true))

return slog.New(handler)
}

var handler slog.Handler
handler = rate.New(option.handler)
if option.sampler != nil {
handler = sampling.New(handler, option.sampler)
}
handler = otel.New(handler, traceOpt...)

return slog.New(handler)
}

0 comments on commit 9ef9791

Please sign in to comment.