Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ sanitized. Baggage can contain tenant identifiers, tokens, or other sensitive
values; use `WithBaggageAllowList`, `WithBaggageDenyList`, or
`WithBaggageFilter` to keep log output intentional.

From `log/otel` v1.9.0 these options compose in the order given: a member is logged only when every filter keeps it, and each filter sees the value the previous one returned. Earlier versions kept only the last of them, and `WithBaggageFilter(nil)` cleared the others; it is now ignored. Setting the `BaggageFilter` field directly still replaces every filter.

## Verification

```bash
Expand Down
81 changes: 81 additions & 0 deletions otel/baggage_filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package otel

import (
"bytes"
"context"
"log/slog"
"strings"
"testing"

"go.opentelemetry.io/otel/baggage"
)

func TestBaggageFilterOptionsCompose(t *testing.T) {
redactTenant := WithBaggageFilter(func(key, value string) (string, bool) {
if key == "tenant" {
return "[REDACTED]", true
}
return value, true
})
tests := []struct {
name string
opts []OtelHandlerOpt
want []string
notWant []string
}{
{
name: "allow list then deny list",
opts: []OtelHandlerOpt{WithBaggageAllowList("request_id", "password"), WithBaggageDenyList("password")},
want: []string{`"request_id":"r1"`},
notWant: []string{"session_token", "password", "tenant"},
},
{
name: "deny list then allow list",
opts: []OtelHandlerOpt{WithBaggageDenyList("password"), WithBaggageAllowList("request_id", "password")},
want: []string{`"request_id":"r1"`},
notWant: []string{"session_token", "password", "tenant"},
},
{
name: "allow list then custom filter",
opts: []OtelHandlerOpt{WithBaggageAllowList("request_id", "tenant"), redactTenant},
want: []string{`"request_id":"r1"`, `"tenant":"[REDACTED]"`},
notWant: []string{"session_token", "acme"},
},
{
name: "nil filter keeps the allow list",
opts: []OtelHandlerOpt{WithBaggageAllowList("request_id"), WithBaggageFilter(nil)},
want: []string{`"request_id":"r1"`},
notWant: []string{"session_token", "password", "tenant"},
},
{
name: "custom filter then allow list",
opts: []OtelHandlerOpt{redactTenant, WithBaggageAllowList("request_id", "tenant")},
want: []string{`"request_id":"r1"`, `"tenant":"[REDACTED]"`},
notWant: []string{"session_token", "acme"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var out bytes.Buffer
opts := append([]OtelHandlerOpt{WithNoBaggage(false)}, tt.opts...)
logger := slog.New(New(slog.NewJSONHandler(&out, nil), opts...))
bag, err := baggage.Parse("request_id=r1,session_token=s3cr3t,password=p,tenant=acme")
if err != nil {
t.Fatalf("baggage.Parse() error = %v", err)
}

logger.InfoContext(baggage.ContextWithBaggage(context.Background(), bag), "x")

for _, want := range tt.want {
if !strings.Contains(out.String(), want) {
t.Errorf("output %s does not contain %s", out.String(), want)
}
}
for _, notWant := range tt.notWant {
if strings.Contains(out.String(), notWant) {
t.Errorf("output %s contains %s", out.String(), notWant)
}
}
})
}
}
33 changes: 29 additions & 4 deletions otel/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ type OtelHandler struct {
NoBaggage bool
// NoTraceEvents determines whether to record an event for every log on the active trace.
NoTraceEvents bool
// BaggageFilter can allow, drop, or redact baggage members before they are logged.
// BaggageFilter can allow, drop, or redact baggage members before they are
// logged. The baggage filter options add to it; setting the field directly
// replaces it.
BaggageFilter BaggageFilter

// groupPrefix is the dot-joined path of the groups opened with WithGroup.
Expand Down Expand Up @@ -118,14 +120,36 @@ func WithNoTraceEvents(noTraceEvents bool) OtelHandlerOpt {
}
}

// WithBaggageFilter configures a baggage filter for selective logging/redaction.
// WithBaggageFilter adds a baggage filter for selective logging/redaction.
// Baggage filter options (WithBaggageFilter, WithBaggageAllowList,
// WithBaggageDenyList) compose in the order given: a member is logged only
// when every filter keeps it, and each filter receives the value returned by
// the previous one. A nil filter is ignored.
func WithBaggageFilter(filter BaggageFilter) OtelHandlerOpt {
return func(handler *OtelHandler) {
handler.BaggageFilter = filter
handler.BaggageFilter = composeBaggageFilters(handler.BaggageFilter, filter)
}
}

// WithBaggageAllowList only logs baggage members whose keys are explicitly listed.
// composeBaggageFilters returns a filter that applies first and then second.
func composeBaggageFilters(first, second BaggageFilter) BaggageFilter {
if first == nil {
return second
}
if second == nil {
return first
}
return func(key, value string) (string, bool) {
value, ok := first(key, value)
if !ok {
return "", false
}
return second(key, value)
}
}

// WithBaggageAllowList only logs baggage members whose keys are explicitly
// listed. It composes with the other baggage filter options.
func WithBaggageAllowList(keys ...string) OtelHandlerOpt {
allowed := make(map[string]struct{}, len(keys))
for _, key := range keys {
Expand All @@ -139,6 +163,7 @@ func WithBaggageAllowList(keys ...string) OtelHandlerOpt {
}

// WithBaggageDenyList drops baggage members whose keys are explicitly listed.
// It composes with the other baggage filter options.
func WithBaggageDenyList(keys ...string) OtelHandlerOpt {
denied := make(map[string]struct{}, len(keys))
for _, key := range keys {
Expand Down
Loading