Skip to content

No way to update or remove item in Activity.Baggage #42596

@AndreyTretyak

Description

@AndreyTretyak

Problem description:

I need to update the value of an item in Activity.Baggage.
Initially, I've assumed that Activity.AddBaggage would replace the previous value, but it adds a second item with the same key and also does it in an inconsistent way, for example following code:

Console.WriteLine("Version={0}", Environment.Version);

var key = "Key1";
var activity = new Activity("Test").Start();
activity.AddBaggage(key, "1");
activity.AddBaggage(key, "2");
Console.WriteLine("Current value {0}={1}", key, activity.GetBaggageItem(key));

Console.WriteLine("All baggage values:");
var index = 0;
foreach (var pair in activity.Baggage)
{
    Console.WriteLine("[{0}] => {1}={2}", index++, pair.Key, pair.Value);
}

When you target netcoreapp3.1 will output:

Version=3.1.7
Current value Key1=2
All baggage values:
[0] => Key1=2
[1] => Key1=1

And for net5.0:

Version=5.0.0
Current value Key1=1
All baggage values:
[0] => Key1=1
[1] => Key1=2

In additional to that when you are passing Activity baggage as a header in web request to Kestrel, Activity inside server would have order reverced so value for the same key on client and server would be diffirent.

Suggested solution
From my point of view having two items with the same key is an incorrect state of the Activity, it creates confusion, lead to strange errors (since users don't expect that order is important) and also increase size or the Baggage(that should be kept as small as posible).

Ideally AddBaggage method should remove or replace previous value.
If you want to avoid adding performance overhead to AddBaggage method then it would be helpful to have some option of removing value from collection, for example RemoveBaggage(string key). Then users could remove old value before adding to avoid mentioned problems.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions