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

Delete a baggage item when value is blank #562

Merged
merged 1 commit into from Mar 29, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions span_context.go
Expand Up @@ -314,6 +314,7 @@ func (c *SpanContext) CopyFrom(ctx *SpanContext) {
}

// WithBaggageItem creates a new context with an extra baggage item.
// Delete a baggage item if provided blank value.
//
// The SpanContext is designed to be immutable and passed by value. As such,
// it cannot contain any locks, and should only hold immutable data, including baggage.
Expand All @@ -324,6 +325,18 @@ func (c *SpanContext) CopyFrom(ctx *SpanContext) {
// a trace), or it needs to do a copy-on-write, which is the approach taken here.
func (c SpanContext) WithBaggageItem(key, value string) SpanContext {
var newBaggage map[string]string
// unset baggage item
if value == "" {
if _, ok := c.baggage[key]; !ok {
return c
}
newBaggage = make(map[string]string, len(c.baggage))
for k, v := range c.baggage {
newBaggage[k] = v
}
delete(newBaggage, key)
return SpanContext{c.traceID, c.spanID, c.parentID, newBaggage, "", c.samplingState, c.remote}
}
if c.baggage == nil {
newBaggage = map[string]string{key: value}
} else {
Expand Down
13 changes: 13 additions & 0 deletions span_context_test.go
Expand Up @@ -79,6 +79,19 @@ func TestSpanContext_WithBaggageItem(t *testing.T) {
assert.Equal(t, map[string]string{"some-KEY": "Some-Other-Value"}, ctx.baggage)
}

func TestSpanContext_WithBaggageItem_Delete(t *testing.T) {
var ctx SpanContext
ctx = ctx.WithBaggageItem("some-KEY", "")
assert.Nil(t, ctx.baggage)
ctx = ctx.WithBaggageItem("some-KEY", "Some-Value")
assert.Equal(t, map[string]string{"some-KEY": "Some-Value"}, ctx.baggage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.Equal(t, map[string]string{"some-KEY": "Some-Value"}, ctx.baggage)
assert.Equal(t, map[string]string{"some-KEY": "Some-Value"}, ctx.baggage)
ctx = ctx.WithBaggageItem("another-KEY", "")
assert.Equal(t, map[string]string{"some-KEY": "Some-Value"}, ctx.baggage)

ctx = ctx.WithBaggageItem("another-KEY", "")
assert.Equal(t, map[string]string{"some-KEY": "Some-Value"}, ctx.baggage)
ctx2 := ctx.WithBaggageItem("some-KEY", "")
assert.Equal(t, map[string]string{"some-KEY": "Some-Value"}, ctx.baggage, "parent unchanged")
assert.Equal(t, map[string]string{}, ctx2.baggage)
}

func TestSpanContext_Flags(t *testing.T) {

var tests = map[string]struct {
Expand Down