Skip to content

Commit

Permalink
Change BaggageKeyPredicate to filter baggage Members (#5813)
Browse files Browse the repository at this point in the history
Provide greater flexibility for users when filtering baggage members by
allowing them to inspect the full member.
  • Loading branch information
MrAlias authored Jun 27, 2024
1 parent 1b5af49 commit fc2954c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Update `go.opentelemetry.io/contrib/config` to latest released configuration schema which introduces breaking changes where `Attributes` is now a `map[string]interface{}`. (#5758)
- Rename `BaggageKeyPredicate` to `Filter` in `go.opentelemetry.io/contrib/processors/baggage/baggagetrace`. (#5809)
- Return `*SpanProcessor` from `"go.opentelemetry.io/contrib/processors/baggage/baggagetrace".New` instead of the `trace.SpanProcessor` interface. (#5810)
- The `Filter` in `go.opentelemetry.io/contrib/processors/baggage/baggagetrace` now accepts a `baggage.Member` as a parameter instead of a string. (#5813)
- Rename `AllowAllBaggageKeys` to `AllowAllMembers` in `go.opentelemetry.io/contrib/processors/baggage/baggagetrace`. (#5813)

## [1.27.0/0.52.0/0.21.0/0.7.0/0.2.0] - 2024-05-21

Expand Down
11 changes: 6 additions & 5 deletions processors/baggage/baggagetrace/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ import (
"strings"

"go.opentelemetry.io/contrib/processors/baggage/baggagetrace"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/sdk/trace"
)

func ExampleNew_allKeys() {
trace.NewTracerProvider(
trace.WithSpanProcessor(baggagetrace.New(baggagetrace.AllowAllBaggageKeys)),
trace.WithSpanProcessor(baggagetrace.New(baggagetrace.AllowAllMembers)),
)
}

func ExampleNew_keysWithPrefix() {
trace.NewTracerProvider(
trace.WithSpanProcessor(
baggagetrace.New(
func(baggageKey string) bool {
return strings.HasPrefix(baggageKey, "my-key")
func(m baggage.Member) bool {
return strings.HasPrefix(m.Key(), "my-key")
},
),
),
Expand All @@ -34,8 +35,8 @@ func ExampleNew_keysMatchingRegex() {
trace.NewTracerProvider(
trace.WithSpanProcessor(
baggagetrace.New(
func(baggageKey string) bool {
return expr.MatchString(baggageKey)
func(m baggage.Member) bool {
return expr.MatchString(m.Key())
},
),
),
Expand Down
17 changes: 8 additions & 9 deletions processors/baggage/baggagetrace/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import (
"go.opentelemetry.io/otel/sdk/trace"
)

// Filter returns true if the baggage member with key should be added to a
// span.
type Filter func(key string) bool
// Filter returns true if the baggage member should be added to a span.
type Filter func(member baggage.Member) bool

// AllowAllBaggageKeys allows all baggage members to be added to a span.
var AllowAllBaggageKeys Filter = func(string) bool { return true }
// AllowAllMembers allows all baggage members to be added to a span.
var AllowAllMembers Filter = func(baggage.Member) bool { return true }

// SpanProcessor is a [trace.SpanProcessor] implementation that adds baggage
// members onto a span as attributes.
Expand All @@ -43,12 +42,12 @@ func New(filter Filter) *SpanProcessor {
func (processor SpanProcessor) OnStart(ctx context.Context, span trace.ReadWriteSpan) {
filter := processor.filter
if filter == nil {
filter = AllowAllBaggageKeys
filter = AllowAllMembers
}

for _, entry := range baggage.FromContext(ctx).Members() {
if filter(entry.Key()) {
span.SetAttributes(attribute.String(entry.Key(), entry.Value()))
for _, member := range baggage.FromContext(ctx).Members() {
if filter(member) {
span.SetAttributes(attribute.String(member.Key(), member.Value()))
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions processors/baggage/baggagetrace/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestSpanProcessorAppendsAllBaggageAttributes(t *testing.T) {
// create trace provider with baggage processor and test exporter
exporter := NewTestExporter()
tp := trace.NewTracerProvider(
trace.WithSpanProcessor(New(AllowAllBaggageKeys)),
trace.WithSpanProcessor(New(AllowAllMembers)),
trace.WithSpanProcessor(trace.NewSimpleSpanProcessor(exporter)),
)

Expand All @@ -65,8 +65,8 @@ func TestSpanProcessorAppendsBaggageAttributesWithHaPrefixPredicate(t *testing.T
b = addEntryToBaggage(t, b, "baggage.test", "baggage value")
ctx := baggage.ContextWithBaggage(context.Background(), b)

baggageKeyPredicate := func(key string) bool {
return strings.HasPrefix(key, "baggage.")
baggageKeyPredicate := func(m baggage.Member) bool {
return strings.HasPrefix(m.Key(), "baggage.")
}

// create trace provider with baggage processor and test exporter
Expand Down Expand Up @@ -94,8 +94,8 @@ func TestSpanProcessorAppendsBaggageAttributesWithRegexPredicate(t *testing.T) {
ctx := baggage.ContextWithBaggage(context.Background(), b)

expr := regexp.MustCompile(`^baggage\..*`)
baggageKeyPredicate := func(key string) bool {
return expr.MatchString(key)
baggageKeyPredicate := func(m baggage.Member) bool {
return expr.MatchString(m.Key())
}

// create trace provider with baggage processor and test exporter
Expand Down Expand Up @@ -123,8 +123,8 @@ func TestOnlyAddsBaggageEntriesThatMatchPredicate(t *testing.T) {
b = addEntryToBaggage(t, b, "foo", "bar")
ctx := baggage.ContextWithBaggage(context.Background(), b)

baggageKeyPredicate := func(key string) bool {
return key == "baggage.test"
baggageKeyPredicate := func(m baggage.Member) bool {
return m.Key() == "baggage.test"
}

// create trace provider with baggage processor and test exporter
Expand Down

0 comments on commit fc2954c

Please sign in to comment.