Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bridge/opentracing: fix baggage item key is canonicalized #4776

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Fix `Parse` in `go.opentelemetry.io/otel/baggage` to validate member value before percent-decoding. (#4755)
- Fix whitespace encoding of `Member.String` in `go.opentelemetry.io/otel/baggage`. (#4756)
- Fix baggage item key so that it is not canonicalized in `go.opentelemetry.io/otel/bridge/opentracing`. (#4776)

## [1.21.0/0.44.0] 2023-11-16

Expand Down
7 changes: 2 additions & 5 deletions bridge/opentracing/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import (
"context"
"fmt"
"net/http"
"strings"
"sync"

Expand Down Expand Up @@ -74,17 +73,15 @@
}

func (c *bridgeSpanContext) setBaggageItem(restrictedKey, value string) {
crk := http.CanonicalHeaderKey(restrictedKey)
m, err := baggage.NewMember(crk, value)
m, err := baggage.NewMember(restrictedKey, value)
if err != nil {
return
}
c.bag, _ = c.bag.SetMember(m)
}

func (c *bridgeSpanContext) baggageItem(restrictedKey string) baggage.Member {
crk := http.CanonicalHeaderKey(restrictedKey)
return c.bag.Member(crk)
return c.bag.Member(restrictedKey)

Check warning on line 84 in bridge/opentracing/bridge.go

View check run for this annotation

Codecov / codecov/patch

bridge/opentracing/bridge.go#L84

Added line #L84 was not covered by tests
}

type bridgeSpan struct {
Expand Down
84 changes: 84 additions & 0 deletions bridge/opentracing/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,87 @@ func TestBridgeSpanContextPromotedMethods(t *testing.T) {
assert.True(t, spanContext.(spanContextProvider).HasTraceID())
})
}

func TestBridgeCarrierBaggagePropagation(t *testing.T) {
carriers := []struct {
name string
carrier interface{}
format ot.BuiltinFormat
}{
{
name: "TextMapCarrier",
carrier: ot.TextMapCarrier{},
format: ot.TextMap,
},
{
name: "HTTPHeadersCarrier",
carrier: ot.HTTPHeadersCarrier{},
format: ot.HTTPHeaders,
},
}

testCases := []struct {
name string
baggageItems []bipBaggage
}{
{
name: "single baggage item",
baggageItems: []bipBaggage{
{
key: "foo",
value: "bar",
},
},
},
{
name: "multiple baggage items",
baggageItems: []bipBaggage{
{
key: "foo",
value: "bar",
},
{
key: "foo2",
value: "bar2",
},
},
},
}

for _, c := range carriers {
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s %s", c.name, tc.name), func(t *testing.T) {
mockOtelTracer := internal.NewMockTracer()
b, _ := NewTracerPair(mockOtelTracer)
b.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{}), // Required for baggage propagation.
)

// Set baggage items.
span := b.StartSpan("test")
for _, bi := range tc.baggageItems {
span.SetBaggageItem(bi.key, bi.value)
}
defer span.Finish()

err := b.Inject(span.Context(), c.format, c.carrier)
assert.NoError(t, err)

spanContext, err := b.Extract(c.format, c.carrier)
assert.NoError(t, err)

// Check baggage items.
bsc, ok := spanContext.(*bridgeSpanContext)
assert.True(t, ok)

var got []bipBaggage
for _, m := range bsc.bag.Members() {
got = append(got, bipBaggage{m.Key(), m.Value()})
}

assert.ElementsMatch(t, tc.baggageItems, got)
})
}
}
}
4 changes: 2 additions & 2 deletions bridge/opentracing/mix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func newBaggageItemsPreservationTest() *baggageItemsPreservationTest {
value: "two",
},
{
key: "Third",
key: "third",
value: "three",
},
},
Expand Down Expand Up @@ -427,7 +427,7 @@ func newBaggageInteroperationTest() *baggageInteroperationTest {
value: "two",
},
{
key: "Third",
key: "third",
value: "three",
},
},
Expand Down