Preserve baggage items when appending new context data#6
Draft
Preserve baggage items when appending new context data#6
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes baggage handling functions to preserve existing baggage items when adding new ones, instead of overwriting the entire baggage. The changes ensure that AddBaggageItem and AddBaggageItems accumulate baggage data rather than replacing it.
- Modified
AddBaggageItemto retrieve existing baggage and useSetMemberinstead of creating new baggage - Updated
AddBaggageItemsto iterate and set members on existing baggage rather than creating from scratch - Added comprehensive tests to verify baggage accumulation behavior
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| baggage.go | Fixed functions to merge with existing baggage instead of overwriting |
| baggage_test.go | Added tests to verify baggage accumulation works correctly |
Comment on lines
15
to
29
| 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) | ||
| } | ||
| b, _ := baggage.New(members...) | ||
| return baggage.ContextWithBaggage(ctx, b) |
There was a problem hiding this comment.
Error from b.SetMember(m) is ignored. Consider handling the error or at least documenting why it's safe to ignore.
Comment on lines
26
to
+27
| m, _ := baggage.NewMember(key, value) | ||
| members = append(members, m) | ||
| b, _ = b.SetMember(m) |
There was a problem hiding this comment.
Error from b.SetMember(m) is ignored. Consider handling the error or at least documenting why it's safe to ignore.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AddBaggageItemandAddBaggageItemsmerge with existing baggage instead of overwritingTesting
GOPROXY=direct go test ./...(fails: unrecognized import paths / network 403 errors)https://chatgpt.com/codex/tasks/task_e_6899848cc4f08326bfe88bc0e6e837d2