Skip to content

Commit

Permalink
Merge pull request #6 from stephenafamo/no-baggage
Browse files Browse the repository at this point in the history
Add option to disable baggage members
  • Loading branch information
remychantenay authored Oct 19, 2023
2 parents bffcd3a + fd13079 commit 7212890
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Go package that provides an implementation of `log/slog`'s [Handler interface](https://pkg.go.dev/log/slog#Handler) that ensures a strong correlation between log records and [Open-Telemetry spans](https://opentelemetry.io/docs/concepts/signals/traces/#spans) by...

1. Adding [span and trace IDs](https://opentelemetry.io/docs/concepts/signals/traces/#span-context) to the log record.
2. Adding context [baggage](https://opentelemetry.io/docs/concepts/signals/baggage/) members to the log record.
2. Adding context [baggage](https://opentelemetry.io/docs/concepts/signals/baggage/) members to the log record (can be disabled).
3. Adding log record as [span event](https://opentelemetry.io/docs/concepts/signals/traces/#span-events).
4. Adding log record attributes to the span event.
5. Setting [span status](https://opentelemetry.io/docs/concepts/signals/traces/#span-status) based on slog record level (only if >= slog.LevelError).
Expand Down
15 changes: 9 additions & 6 deletions slog_otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package slogotel
import (
"context"
"fmt"
"time"

"log/slog"
"time"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/baggage"
Expand All @@ -25,6 +24,8 @@ import (
type OtelHandler struct {
// Next represents the next handler in the chain.
Next slog.Handler
// NoBaggage determines whether to add context baggage members to the log record.
NoBaggage bool
}

// HandlerFn defines the handler used by slog.Handler as return value.
Expand All @@ -45,10 +46,12 @@ func (h OtelHandler) Handle(ctx context.Context, record slog.Record) error {
return h.Next.Handle(ctx, record)
}

// Adding context baggage members to log record.
b := baggage.FromContext(ctx)
for _, m := range b.Members() {
record.AddAttrs(slog.String(m.Key(), m.Value()))
if !h.NoBaggage {
// Adding context baggage members to log record.
b := baggage.FromContext(ctx)
for _, m := range b.Members() {
record.AddAttrs(slog.String(m.Key(), m.Value()))
}
}

span := trace.SpanFromContext(ctx)
Expand Down

0 comments on commit 7212890

Please sign in to comment.