You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
No way to update or remove item in Activity.Baggage #42596
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);varkey="Key1";varactivity=newActivity("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:");varindex=0;foreach(varpairinactivity.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.
I understand that Add should only add values, but what's the point of adding items that won't be returned from GetBaggageItem and just increase collection.
Is it possible to add SetBaggage in the future?
I think it's even more needed for Baggage then for Tags, since Baggage should be small and it's transferred in headers where order could be changed.
If it's intended behavior maybe you should specify that order is part of the contract (then we could request asp .net core and other implementations that parse header into baggage to preserve order).
Also will diffidence in ordering would be fixed in net5.0? (In netcoreapp3.1 it added to the beginning and in net5.0 to the end)
but what's the point of adding items that won't be returned from GetBaggageItem and just increase collection.
You can get the whole list from Activity.Baggage property. So, it is possible to reach any item there.
Is it possible to add SetBaggage in the future?
Yes, it is possible. we just need to track that request. you can notice I have added the api-suggestion label to this issue.
I think it's even more needed for Baggage then for Tags, since Baggage should be small and it's transferred in headers where order could be changed.
Could you please add more details about your scenario? how you are using the Baggage and why you need to update or delete any item there?
If it's intended behavior maybe you should specify that order is part of the contract (then we could request asp .net core and other implementations that parse header into baggage to preserve order).
We learned from other projects (e.g. OpenTelemetry) that order is important in sme scenarios. So we are maintaining the order as the items get added. The reason is the consumer or us in the future can put some limits to the number of items can be added to the list.
We may consider add somethign to the doc to clarify that.
Also will diffidence in ordering would be fixed in net5.0?
It is too late for .NET 5.0, and I think moving forward we'll keep the current order behavior. But we can consider adding SetBaggage() if we see some demand on it.
You can get the whole list from Activity.Baggage property. So, it is possible to reach any item there.
Yes, but with different ordering between versions, it's complicated to understand what item was added last. It would probably be better in the future since you've agreed on a single ordering but between 3 and 5 it would be close to imposible.
It is too late for .NET 5.0, and I think moving forward we'll keep the current behavior.
I assume the behavior of GetBaggageItem won't be changed in the future also? netcoreapp3.1 behaved more logical to my taste since GetBaggageItem returned the last added item and after net5.0 it's always the first added item ignoring subsequent adds.
Could you please add more details about your scenario? how you are using the Baggage and why you need to update or delete any item there?
We are migrating from the custom correlation tracking system and adding it's correlation id as a baggage item. Unfortunately, it's done in a way that id assigned initially could be overridden by another value (provided as a header from incoming request for example). In a chain with multiple services calling each other baggage quickly becoming huge getting additional correlation id on each subsequent request. I understand that it's not in any way ideal architecture, but we need some period of our curstom correlation and activity working simultaneously since we could not migrate all service at once.
In general I think any scenario that allows multiple calls of AddBaggageItem could hit this problem.
Thank you for the suggestion @noahfalk . In our case AddBaggageItem() and GetBaggageItem() mostly happening in separate processes.
Because of this, the fact that Kestrel reverses the order of baggage in net core 3.1 complicates things. From what I've seen in code it should be fine for net 5.0 since you've changed the addition order (probably that was the reason for order change).
Problem with using SetCustomProperty() is that it won't be transferred (as far as I understand) to nested activities or outgoing requests and that's the main reason for using baggage over tags.
Custom properties would be a nice replacement of Tags after we'll move to net5.0 (or we could use SetTag then), since we have a similar scenario of adding a state (success/failure) to activity tags that could be later overridden (ex. in case of exception).
Curently for the scenario with tags we could probably do some kind of logic that would return the last tag pair with required key (althougth I dislike the idea of having huge list of useles values in tags), and then reverse this logic to return the first tag after moving to net5.0. But with baggage, it's more complicated since I don't see a way of understanding right order if services use diffirent frameworks, like Servise1(net core 3.1) -> Service2 (net5.0) -> Service3(net472), only adding indexes to keys :), but so many duplicates would explode the size of the baggage quite quick.
So it looks hopeless, but if you have any suggestions about making simular scenario work on net core 3.1, they would be grately appreciated. I'm very tempted to use reflection for the removal of old pair :)
@AndreyTretyak I am looking if we can fix the behavior of GetBaggageItem in 5.0 to return the last added entry instead of the first. I'll let you know how this will go. I have 2 questions for you now:
If we fixed GetBaggageItem, would that be enough for you to fix your scenarios?
regarding Servise1(net core 3.1) -> Service2 (net5.0) -> Service3(net472), do you have control over such services to upgrade to the latest System.Diagnostics.DiagnosticSource package version? I am asking because the version we ship with 5.0, can be used in net core 3.1 and net472 too. if you can upgrade the package version, you'll always have very consistent behavior at that time.
@tarekgh thank you for looking into this. I think fixing the behavior of GetBaggageItem should cover most of our scenarios. There would be still an issue of increasing baggage with each add, but that's a much smaller problem and it's kind of our fault for misunderstanding the contract. Althought, it would be very useful if in some future version you could provide ability to remove or replace baggage items.
In our case, we are controling the version of the packages in all of them so it would be great if NuGet package would containe updated behaivour. I've assumed there would be no packages after net5.0 so it's great that you would continue publishing them, thank you.
@AndreyTretyak I have opened the PR #42659 (this is against 6.0) to go back to old behavior of Baggages. After we merge this one, I'll try to get this change to 5.0 too if it get approved.
Thanks for the heads-up, @tarekgh. From what I can tell, this wouldn't block RC2 but would be considered for GA/RTM. Let me know if you were thinking otherwise.
Activity
tarekgh commentedon Sep 22, 2020
CC @noahfalk
This is the original design for AddBaggage, it always add. The only workaround for that is to clone the Activity object with updated Baggages.
In 5.0 we have allowed the requested functionality for Tags through SetTag().
AndreyTretyak commentedon Sep 22, 2020
I understand that Add should only add values, but what's the point of adding items that won't be returned from GetBaggageItem and just increase collection.
Is it possible to add SetBaggage in the future?
I think it's even more needed for Baggage then for Tags, since Baggage should be small and it's transferred in headers where order could be changed.
If it's intended behavior maybe you should specify that order is part of the contract (then we could request asp .net core and other implementations that parse header into baggage to preserve order).
Also will diffidence in ordering would be fixed in net5.0? (In netcoreapp3.1 it added to the beginning and in net5.0 to the end)
tarekgh commentedon Sep 22, 2020
You can get the whole list from
Activity.Baggageproperty. So, it is possible to reach any item there.Yes, it is possible. we just need to track that request. you can notice I have added the
api-suggestionlabel to this issue.Could you please add more details about your scenario? how you are using the Baggage and why you need to update or delete any item there?
We learned from other projects (e.g.
OpenTelemetry) that order is important in sme scenarios. So we are maintaining the order as the items get added. The reason is the consumer or us in the future can put some limits to the number of items can be added to the list.We may consider add somethign to the doc to clarify that.
It is too late for .NET 5.0, and I think moving forward we'll keep the current order behavior. But we can consider adding
SetBaggage()if we see some demand on it.AndreyTretyak commentedon Sep 22, 2020
Yes, but with different ordering between versions, it's complicated to understand what item was added last. It would probably be better in the future since you've agreed on a single ordering but between 3 and 5 it would be close to imposible.
I assume the behavior of
GetBaggageItemwon't be changed in the future also?netcoreapp3.1behaved more logical to my taste sinceGetBaggageItemreturned the last added item and afternet5.0it's always the first added item ignoring subsequent adds.We are migrating from the custom correlation tracking system and adding it's correlation id as a baggage item. Unfortunately, it's done in a way that id assigned initially could be overridden by another value (provided as a header from incoming request for example). In a chain with multiple services calling each other baggage quickly becoming huge getting additional correlation id on each subsequent request. I understand that it's not in any way ideal architecture, but we need some period of our curstom correlation and activity working simultaneously since we could not migrate all service at once.
In general I think any scenario that allows multiple calls of
AddBaggageItemcould hit this problem.1 remaining item
AndreyTretyak commentedon Sep 23, 2020
Thank you for the suggestion @noahfalk . In our case AddBaggageItem() and GetBaggageItem() mostly happening in separate processes.
Because of this, the fact that Kestrel reverses the order of baggage in net core 3.1 complicates things. From what I've seen in code it should be fine for net 5.0 since you've changed the addition order (probably that was the reason for order change).
Problem with using
SetCustomProperty()is that it won't be transferred (as far as I understand) to nested activities or outgoing requests and that's the main reason for using baggage over tags.Custom properties would be a nice replacement of Tags after we'll move to net5.0 (or we could use
SetTagthen), since we have a similar scenario of adding a state (success/failure) to activity tags that could be later overridden (ex. in case of exception).Curently for the scenario with tags we could probably do some kind of logic that would return the last tag pair with required key (althougth I dislike the idea of having huge list of useles values in tags), and then reverse this logic to return the first tag after moving to net5.0. But with baggage, it's more complicated since I don't see a way of understanding right order if services use diffirent frameworks, like Servise1(net core 3.1) -> Service2 (net5.0) -> Service3(net472), only adding indexes to keys :), but so many duplicates would explode the size of the baggage quite quick.
So it looks hopeless, but if you have any suggestions about making simular scenario work on net core 3.1, they would be grately appreciated. I'm very tempted to use reflection for the removal of old pair :)
tarekgh commentedon Sep 23, 2020
@AndreyTretyak I am looking if we can fix the behavior of
GetBaggageItemin 5.0 to return the last added entry instead of the first. I'll let you know how this will go. I have 2 questions for you now:GetBaggageItem, would that be enough for you to fix your scenarios?Servise1(net core 3.1) -> Service2 (net5.0) -> Service3(net472), do you have control over such services to upgrade to the latestSystem.Diagnostics.DiagnosticSourcepackage version? I am asking because the version we ship with 5.0, can be used in net core 3.1 and net472 too. if you can upgrade the package version, you'll always have very consistent behavior at that time.AndreyTretyak commentedon Sep 23, 2020
@tarekgh thank you for looking into this. I think fixing the behavior of
GetBaggageItemshould cover most of our scenarios. There would be still an issue of increasing baggage with each add, but that's a much smaller problem and it's kind of our fault for misunderstanding the contract. Althought, it would be very useful if in some future version you could provide ability to remove or replace baggage items.In our case, we are controling the version of the packages in all of them so it would be great if NuGet package would containe updated behaivour. I've assumed there would be no packages after net5.0 so it's great that you would continue publishing them, thank you.
tarekgh commentedon Sep 24, 2020
@AndreyTretyak I have opened the PR #42659 (this is against 6.0) to go back to old behavior of Baggages. After we merge this one, I'll try to get this change to 5.0 too if it get approved.
tarekgh commentedon Sep 24, 2020
@ericstj @jeffhandley this is another candidate fix to port to 5.0 release. I'll CC you on the porting PR after I process this one.
jeffhandley commentedon Sep 24, 2020
Thanks for the heads-up, @tarekgh. From what I can tell, this wouldn't block RC2 but would be considered for GA/RTM. Let me know if you were thinking otherwise.
tarekgh commentedon Sep 24, 2020
@jeffhandley having it for GA/RTM will be fine.