Skip to content
Draft
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
8 changes: 4 additions & 4 deletions baggage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ func GetBaggage(ctx context.Context) baggage.Baggage {

// AddBaggageItem adds a key-value pair to the baggage.
func AddBaggageItem(ctx context.Context, key, value string) context.Context {
b := baggage.FromContext(ctx)
m, _ := baggage.NewMember(key, value)
b, _ := baggage.New(m)
b, _ = b.SetMember(m)
return baggage.ContextWithBaggage(ctx, b)
}

// AddBaggageItems adds multiple key-value pairs to the baggage.
func AddBaggageItems(ctx context.Context, items map[string]string) context.Context {
var members []baggage.Member
b := baggage.FromContext(ctx)
for key, value := range items {
m, _ := baggage.NewMember(key, value)
members = append(members, m)
b, _ = b.SetMember(m)
Comment on lines 26 to +27
Copy link

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

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

Error from b.SetMember(m) is ignored. Consider handling the error or at least documenting why it's safe to ignore.

Copilot uses AI. Check for mistakes.
}
b, _ := baggage.New(members...)
return baggage.ContextWithBaggage(ctx, b)
Comment on lines 15 to 29
Copy link

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

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

Error from b.SetMember(m) is ignored. Consider handling the error or at least documenting why it's safe to ignore.

Copilot uses AI. Check for mistakes.
}

Expand Down
29 changes: 29 additions & 0 deletions baggage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package otelemetry

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestAddBaggageItem(t *testing.T) {
ctx := context.Background()
ctx = AddBaggageItem(ctx, "user", "alice")
ctx = AddBaggageItem(ctx, "tenant", "acme")

b := GetBaggage(ctx)
assert.Equal(t, "alice", b.Member("user").Value())
assert.Equal(t, "acme", b.Member("tenant").Value())
}

func TestAddBaggageItems(t *testing.T) {
ctx := context.Background()
ctx = AddBaggageItem(ctx, "user", "alice")
ctx = AddBaggageItems(ctx, map[string]string{"tenant": "acme", "role": "admin"})

b := GetBaggage(ctx)
assert.Equal(t, "alice", b.Member("user").Value())
assert.Equal(t, "acme", b.Member("tenant").Value())
assert.Equal(t, "admin", b.Member("role").Value())
}