Skip to content

Commit fc2954c

Browse files
authored
Change BaggageKeyPredicate to filter baggage Members (#5813)
Provide greater flexibility for users when filtering baggage members by allowing them to inspect the full member.
1 parent 1b5af49 commit fc2954c

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
4848
- Update `go.opentelemetry.io/contrib/config` to latest released configuration schema which introduces breaking changes where `Attributes` is now a `map[string]interface{}`. (#5758)
4949
- Rename `BaggageKeyPredicate` to `Filter` in `go.opentelemetry.io/contrib/processors/baggage/baggagetrace`. (#5809)
5050
- Return `*SpanProcessor` from `"go.opentelemetry.io/contrib/processors/baggage/baggagetrace".New` instead of the `trace.SpanProcessor` interface. (#5810)
51+
- The `Filter` in `go.opentelemetry.io/contrib/processors/baggage/baggagetrace` now accepts a `baggage.Member` as a parameter instead of a string. (#5813)
52+
- Rename `AllowAllBaggageKeys` to `AllowAllMembers` in `go.opentelemetry.io/contrib/processors/baggage/baggagetrace`. (#5813)
5153

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

processors/baggage/baggagetrace/example_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ import (
88
"strings"
99

1010
"go.opentelemetry.io/contrib/processors/baggage/baggagetrace"
11+
"go.opentelemetry.io/otel/baggage"
1112
"go.opentelemetry.io/otel/sdk/trace"
1213
)
1314

1415
func ExampleNew_allKeys() {
1516
trace.NewTracerProvider(
16-
trace.WithSpanProcessor(baggagetrace.New(baggagetrace.AllowAllBaggageKeys)),
17+
trace.WithSpanProcessor(baggagetrace.New(baggagetrace.AllowAllMembers)),
1718
)
1819
}
1920

2021
func ExampleNew_keysWithPrefix() {
2122
trace.NewTracerProvider(
2223
trace.WithSpanProcessor(
2324
baggagetrace.New(
24-
func(baggageKey string) bool {
25-
return strings.HasPrefix(baggageKey, "my-key")
25+
func(m baggage.Member) bool {
26+
return strings.HasPrefix(m.Key(), "my-key")
2627
},
2728
),
2829
),
@@ -34,8 +35,8 @@ func ExampleNew_keysMatchingRegex() {
3435
trace.NewTracerProvider(
3536
trace.WithSpanProcessor(
3637
baggagetrace.New(
37-
func(baggageKey string) bool {
38-
return expr.MatchString(baggageKey)
38+
func(m baggage.Member) bool {
39+
return expr.MatchString(m.Key())
3940
},
4041
),
4142
),

processors/baggage/baggagetrace/processor.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ import (
1111
"go.opentelemetry.io/otel/sdk/trace"
1212
)
1313

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

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

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

49-
for _, entry := range baggage.FromContext(ctx).Members() {
50-
if filter(entry.Key()) {
51-
span.SetAttributes(attribute.String(entry.Key(), entry.Value()))
48+
for _, member := range baggage.FromContext(ctx).Members() {
49+
if filter(member) {
50+
span.SetAttributes(attribute.String(member.Key(), member.Value()))
5251
}
5352
}
5453
}

processors/baggage/baggagetrace/processor_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestSpanProcessorAppendsAllBaggageAttributes(t *testing.T) {
4444
// create trace provider with baggage processor and test exporter
4545
exporter := NewTestExporter()
4646
tp := trace.NewTracerProvider(
47-
trace.WithSpanProcessor(New(AllowAllBaggageKeys)),
47+
trace.WithSpanProcessor(New(AllowAllMembers)),
4848
trace.WithSpanProcessor(trace.NewSimpleSpanProcessor(exporter)),
4949
)
5050

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

68-
baggageKeyPredicate := func(key string) bool {
69-
return strings.HasPrefix(key, "baggage.")
68+
baggageKeyPredicate := func(m baggage.Member) bool {
69+
return strings.HasPrefix(m.Key(), "baggage.")
7070
}
7171

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

9696
expr := regexp.MustCompile(`^baggage\..*`)
97-
baggageKeyPredicate := func(key string) bool {
98-
return expr.MatchString(key)
97+
baggageKeyPredicate := func(m baggage.Member) bool {
98+
return expr.MatchString(m.Key())
9999
}
100100

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

126-
baggageKeyPredicate := func(key string) bool {
127-
return key == "baggage.test"
126+
baggageKeyPredicate := func(m baggage.Member) bool {
127+
return m.Key() == "baggage.test"
128128
}
129129

130130
// create trace provider with baggage processor and test exporter

0 commit comments

Comments
 (0)