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
Allow setting the Activity properties after Activity creation and before starting it when using ActivitySource #42784
The description and the Proposal edited by @tarekgh. The original description is moved down in the comment #42784 (comment)
ActivitySource provides the method StartActivity which will try to create and start the Activity object. There is a scenario that needs to add some more properties to the Activity object after it is created and before it gets started.
The reason is the caller wants to avoid creating the objects need to set to the Activity before ensuring the Activity object is created. It is important to have these properties set before starting the Activity so the listeners of the Start event will get the full set of the data.
Proposal
namespaceSystem.Diagnostics{publicsealedclassActivitySource:IDisposable{// Alternative proposed name is CreateUnstartedActivitypublicActivity?CreateActivity(stringname,ActivityKindkind,ActivityContextparentContext,IEnumerable<KeyValuePair<string,object?>>?tags=null,IEnumerable<ActivityLink>?links=null);publicActivity?CreateActivity(stringname,ActivityKindkind,stringparentContext,IEnumerable<KeyValuePair<string,object?>>?tags=null,IEnumerable<ActivityLink>?links=null);}}
Example
using(Activityactivity=activitySource.CreateActivity("SomeName",ActivityKind.Server,context)){activity?.AddBaggage("db.type","mongo");activity?.Start();// Do any necessary work }
I don't think it is intended to support all possible scenario in ActivitySource in first version. Your scenario can be considered in the future releases. You still have the option to use old way of creating the Activity and using DiagnosticSource. This still work.
For tags allocation, you can still avoid the allocation before creating the activity by setting ActivityCreationOptions.SamplingTags in the listener callbacks. This will still allocate at that time but for sure it will be determined the Activity is going to be allocated anyway.
It is too late to change any APIs in the 5.0 release.
You still have the option to use old way of creating the Activity and using DiagnosticSource. This still work.
From the perspective of a telemetry producer - yes. But a producer does not know whether this scenario is important for any actual consumer. And if a producer who followed the existing guidance wants to transition to the "new way" while enabling all previous scenarios, they cannot. I completely realize the current spot in the release cycle, but if this item was to be tracked for prioritization in the next cycles, then, for the record, I think that the workaround you mention does not apply. :)
For tags allocation, you can still avoid the allocation before creating the activity by setting ActivityCreationOptions.SamplingTags in the listener callbacks.
Thanks for pointing it out. I had missed the existence of that property. However, having had a look at it, I do not fully understand its use. Is there a doc or an example? What I mean above is that when invoking ActivitySource.StartActivity(..) you need to pass an IEnumerable object on the heap. So the allocation will occur when creating that object while the sampling decision is taken within the scope of the ActivitySource.StartActivity(..) method. So regardless of whether or not you decided to allocate the activity, you will have to allocate the tags collection object.
Please let me know if you guys consider these Activity-related submissions as a valuable input for forward-looking planning / for the next cycle. I do not want to create a distraction if it is not productive. :)
if a producer who followed the existing guidance wants to transition to the "new way" while enabling all previous scenarios, they cannot.
Agree. What i was trying to say is, this is not supported scenario yet with ActivitySource. I was pointing if someone blocked for now can consider use the old way.
For ActivityCreationOptions.SamplingTags, the idea is when you are calling ActivitySource.StartActivity you'll not pass any tags at that time but in the listener you'll have a chance to add the tags during then. I am not sure if this will fully satisfy your scenario. you may look at our test
If the allocation is very critical to your scenario, you may consider having IEnumerable thread local object (e.g. List<>) which you can use for all subsequent calls on same thread. The allocation would be once per thread instead to be ones per ActivityStart call.
Agree. What i was trying to say is, this is not supported scenario yet with ActivitySource. I was pointing if someone blocked for now can consider use the old way.
Gotcha, thanks!
For ActivityCreationOptions.SamplingTags, the idea is when you are calling ActivitySource.StartActivity you'll not pass any tags at that time but in the listener you'll have a chance to add the tags during then.
Ah, understood. This is a different scenario. I was talking about saving the allocation that is only required only to pass parameters to StartActivity(..)
If the allocation is very critical to your scenario, you may consider having IEnumerable thread local object (e.g. List<>) which you can use for all subsequent calls on same thread.
That might work, but the complexity of implementing such cache and the perf overhead required for cleaning it before each use make this feel concerning. Of course, .NET5 is done. I would love a performance-focused overload similar to what I mentioned above to be considered for the next release.
If the team decides that such an overload is beneficial, I'd be happy to contribute when the time comes. :)
StartActivity(..) overloads that have parameters like string tag1Key, string tag1Value, ... for the first few tags.
This sounds like a good improvement. In most cases the number of initial tags/attributes is very low (or none). More tags are added after actually creating the activity and knowing the sampling decision.
System.Diagnostics.ActivitySource.StartActivity(..) is missing an overload that takes Baggage items
StartActivity() was intended to take the minimum amount of data necessary for sampling. Adding more types of data trades off performance for more flexibility in sampling and richer data available on the start callback. Another direction is to create an API that returns an unstarted Activity allowing Start() to be decoupled from sampling. That option is trading off more complicated API usage for richer data available on the start callback. I'm not opposed to this in principle, but they first hurdle to clear would be to understand why do we need specific data attached to a Start callback if that same data will be available later in the Stop callback?
However, this will require that an enumerable is allocated before StartActivity(..) is called
We added the overload which took tags because OpenTelemetry defined that it must be possible to give tags to the sampling algorithm. My hope is that in practice it is a rare case that the sampling algorithm needs to consider tags because doing so implies you are paying perf costs to generate them and analyze them even for sampled-out activities. The higher performing route would be to determine in advance that the tags aren't relevant for sampling and provide them only after an Activity has been sampled in using Activity.SetTag(). If there is a need for higher performance scenarios where certain tags are available to the sampler or available in the Start() callback I'm not opposed to optimizations you suggested, but I'd like to be able to tie to some specific customer or use-case that the work is improving.
minor: The title of this issue can be confusing as the new API is about slightly different things. Could you create a new issue with the proposal, and link it from there for easy read by anyone new. :)
minor: The title of this issue can be confusing as the new API is about slightly different things. Could you create a new issue with the proposal, and link it from there for easy read by anyone new. :)
We can rename the title and possibly the description if @macrogreg not mind. I am not sure creating a new issue and link it to this one would help much as all discussions are listed here.
Moving the original description added by @macrogreg here for the record as we need to modify the description up to match what have discussed and decided on.
I think there is a scenario supported by the pre-.NET-5 Activity APIs based on DiagnosticSource that is not supported in the new ActivitySource based APIs:
System.Diagnostics.ActivitySource.StartActivity(..) is missing an overload that takes Baggage items.
In this scenario, a telemetry consumer who chooses to listen to "Http_In"-evens will receive Start and Stop events such that the Start events already contain information from the "Correlation-Context"-headers in the Baggage bag attached to the activity. (In addition, the whole HttpContext context object is also passed, but this is not the focus here.)
On contrary, in the new ActivitySource-based APIs, the listeners are notified within the scope of the ActivitySource.StartActivity(..) method. The creator of the activity only receives a reference to the new activity after the potentially registered listeners have been notified and finished processing the Start event. Only information that can be passed as arguments to the StartActivity(..) method can reach the listeners.
So the scenario that is used as the "model" scenario in .NET's own docs is not supported.
If we consider this to indeed be a problem worth addressing, we should not stop at Baggage and consider that it applies to all Activity properties that existed pre-.NET-5 and were settable on an Activity instance before is is passed to DiagnosticSource.StartActivity(..).
I realized how late to the final release the product is. There are several possible approaches:
I am missing something, and there is a workaround already (I hope for this to be the case :) ). Perhaps we can include it into some doc.
We decide that this is indeed a problem, but we are too late in the cycle to fix it. The we should still acknowledge and document it, and track for a future release.
We consider it fixing for .NET 5. If so, please let me know, I'd be happy to prepare a PR.
Thanks for considering this!
(Update an hour later) Just discovered an additional (related) issue, adding relevant info here:
The Tags of an Activity are not exactly affected by the above problem as ActivitySource.StartActivity(..) has overloads that take an IEnumerable<KeyValuePair<string, object>> tags parameter. However, this will require that an enumerable is allocated before StartActivity(..) is called. This is problematic in two ways. One of them is easier to address than the other:
If the activity would not be created due to sampling (i.e. StartActivity(..) returns null), then the allocation of the enumerable and all its members was for nothing. This is hard to address in the current model. At this stage all we can do is to document it and recommend a best way to minimize overhead.
Even if the Activity object is created, there is still an allocation if the IEnumerable just to pass parameters. The object will likely not survive beyond Gen0, but still: so much work was done to avoid allocations, that it seems weird. This could be addressed by providing StartActivity(..) overloads that have parameters like string tag1Key, string tag1Value, ... for the first few tags. This could help skipping that allocation for the common case where there is just a few tags. Notably, having tags and/or baggage is likely much more frequent than having links, events etc. So yes, the number of overloads would grow, but would not explode uncontrollably. :)
changed the title [-]ActivitySource.StartActivity(..) is missing an overload that takes Baggage Items[/-][+]Allow setting the Activity properties after Activity creation and before starting it when using ActivitySource[/+]on Feb 11, 2021
Discussed offline more the proposal with @dotnet/fxdc to ensure we are addressing the request in #43853 and decided we'll add the extra parameter ActivityIdFormat idFormat = ActivityIdFormat.Unknown to the last 2 overloads. the final approved proposal now is:
Activity
macrogreg commentedon Sep 26, 2020
Some tags to raise awareness for folks involved into relevant work streams:
@cijothomas @noahfalk
Thanks! :)
tarekgh commentedon Sep 27, 2020
I don't think it is intended to support all possible scenario in ActivitySource in first version. Your scenario can be considered in the future releases. You still have the option to use old way of creating the Activity and using DiagnosticSource. This still work.
For tags allocation, you can still avoid the allocation before creating the activity by setting
ActivityCreationOptions.SamplingTagsin the listener callbacks. This will still allocate at that time but for sure it will be determined the Activity is going to be allocated anyway.It is too late to change any APIs in the 5.0 release.
macrogreg commentedon Sep 28, 2020
From the perspective of a telemetry producer - yes. But a producer does not know whether this scenario is important for any actual consumer. And if a producer who followed the existing guidance wants to transition to the "new way" while enabling all previous scenarios, they cannot. I completely realize the current spot in the release cycle, but if this item was to be tracked for prioritization in the next cycles, then, for the record, I think that the workaround you mention does not apply. :)
Thanks for pointing it out. I had missed the existence of that property. However, having had a look at it, I do not fully understand its use. Is there a doc or an example? What I mean above is that when invoking
ActivitySource.StartActivity(..)you need to pass anIEnumerableobject on the heap. So the allocation will occur when creating that object while the sampling decision is taken within the scope of theActivitySource.StartActivity(..)method. So regardless of whether or not you decided to allocate the activity, you will have to allocate the tags collection object.Please let me know if you guys consider these Activity-related submissions as a valuable input for forward-looking planning / for the next cycle. I do not want to create a distraction if it is not productive. :)
Thanks!
tarekgh commentedon Sep 28, 2020
Agree. What i was trying to say is, this is not supported scenario yet with ActivitySource. I was pointing if someone blocked for now can consider use the old way.
For
ActivityCreationOptions.SamplingTags, the idea is when you are callingActivitySource.StartActivityyou'll not pass any tags at that time but in the listener you'll have a chance to add the tags during then. I am not sure if this will fully satisfy your scenario. you may look at our testruntime/src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivitySourceTests.cs
Line 605 in e2e43f4
If the allocation is very critical to your scenario, you may consider having IEnumerable thread local object (e.g. List<>) which you can use for all subsequent calls on same thread. The allocation would be once per thread instead to be ones per ActivityStart call.
macrogreg commentedon Sep 28, 2020
Gotcha, thanks!
Ah, understood. This is a different scenario. I was talking about saving the allocation that is only required only to pass parameters to StartActivity(..)
That might work, but the complexity of implementing such cache and the perf overhead required for cleaning it before each use make this feel concerning. Of course, .NET5 is done. I would love a performance-focused overload similar to what I mentioned above to be considered for the next release.
If the team decides that such an overload is beneficial, I'd be happy to contribute when the time comes. :)
Thanks!
cijothomas commentedon Sep 28, 2020
This sounds like a good improvement. In most cases the number of initial tags/attributes is very low (or none). More tags are added after actually creating the activity and knowing the sampling decision.
noahfalk commentedon Sep 28, 2020
Thanks @macrogreg!
StartActivity() was intended to take the minimum amount of data necessary for sampling. Adding more types of data trades off performance for more flexibility in sampling and richer data available on the start callback. Another direction is to create an API that returns an unstarted Activity allowing Start() to be decoupled from sampling. That option is trading off more complicated API usage for richer data available on the start callback. I'm not opposed to this in principle, but they first hurdle to clear would be to understand why do we need specific data attached to a Start callback if that same data will be available later in the Stop callback?
We added the overload which took tags because OpenTelemetry defined that it must be possible to give tags to the sampling algorithm. My hope is that in practice it is a rare case that the sampling algorithm needs to consider tags because doing so implies you are paying perf costs to generate them and analyze them even for sampled-out activities. The higher performing route would be to determine in advance that the tags aren't relevant for sampling and provide them only after an Activity has been sampled in using Activity.SetTag(). If there is a need for higher performance scenarios where certain tags are available to the sampler or available in the Start() callback I'm not opposed to optimizations you suggested, but I'd like to be able to tie to some specific customer or use-case that the work is improving.
27 remaining items
tarekgh commentedon Feb 10, 2021
I have added the proposal in the top. We should discuss the naming in the design meeting. Thanks allot for your feedback.
cijothomas commentedon Feb 10, 2021
minor: The title of this issue can be confusing as the new API is about slightly different things. Could you create a new issue with the proposal, and link it from there for easy read by anyone new. :)
tarekgh commentedon Feb 10, 2021
We can rename the title and possibly the description if @macrogreg not mind. I am not sure creating a new issue and link it to this one would help much as all discussions are listed here.
tarekgh commentedon Feb 11, 2021
Moving the original description added by @macrogreg here for the record as we need to modify the description up to match what have discussed and decided on.
@macrogreg original description
I think there is a scenario supported by the pre-.NET-5 Activity APIs based on
DiagnosticSourcethat is not supported in the newActivitySourcebased APIs:System.Diagnostics.ActivitySource.StartActivity(..)is missing an overload that takes Baggage items.Consider .NET's own guidance on using
ActivitywithDiagnosticSource:https://github.com/dotnet/runtime/blob/master/src/libraries/System.Diagnostics.DiagnosticSource/src/ActivityUserGuide.md#starting-and-stopping-activity
In this scenario, a telemetry consumer who chooses to listen to
"Http_In"-evens will receive Start and Stop events such that the Start events already contain information from the"Correlation-Context"-headers in theBaggagebag attached to the activity. (In addition, the wholeHttpContext contextobject is also passed, but this is not the focus here.)On contrary, in the new
ActivitySource-based APIs, the listeners are notified within the scope of theActivitySource.StartActivity(..)method. The creator of the activity only receives a reference to the new activity after the potentially registered listeners have been notified and finished processing the Start event. Only information that can be passed as arguments to theStartActivity(..)method can reach the listeners.So the scenario that is used as the "model" scenario in .NET's own docs is not supported.
If we consider this to indeed be a problem worth addressing, we should not stop at
Baggageand consider that it applies to all Activity properties that existed pre-.NET-5 and were settable on anActivityinstance before is is passed toDiagnosticSource.StartActivity(..).I realized how late to the final release the product is. There are several possible approaches:
I am missing something, and there is a workaround already (I hope for this to be the case :) ). Perhaps we can include it into some doc.
We decide that this is indeed a problem, but we are too late in the cycle to fix it. The we should still acknowledge and document it, and track for a future release.
We consider it fixing for .NET 5. If so, please let me know, I'd be happy to prepare a PR.
Thanks for considering this!
(Update an hour later) Just discovered an additional (related) issue, adding relevant info here:
For convenience, here is the current ActivitySource public surface.
The
Tagsof anActivityare not exactly affected by the above problem asActivitySource.StartActivity(..)has overloads that take anIEnumerable<KeyValuePair<string, object>> tagsparameter. However, this will require that an enumerable is allocated beforeStartActivity(..)is called. This is problematic in two ways. One of them is easier to address than the other:If the activity would not be created due to sampling (i.e.
StartActivity(..)returnsnull), then the allocation of the enumerable and all its members was for nothing. This is hard to address in the current model. At this stage all we can do is to document it and recommend a best way to minimize overhead.Even if the Activity object is created, there is still an allocation if the IEnumerable just to pass parameters. The object will likely not survive beyond Gen0, but still: so much work was done to avoid allocations, that it seems weird. This could be addressed by providing
StartActivity(..)overloads that have parameters likestring tag1Key,string tag1Value, ... for the first few tags. This could help skipping that allocation for the common case where there is just a few tags. Notably, having tags and/or baggage is likely much more frequent than having links, events etc. So yes, the number of overloads would grow, but would not explode uncontrollably. :)[-]ActivitySource.StartActivity(..) is missing an overload that takes Baggage Items[/-][+]Allow setting the Activity properties after Activity creation and before starting it when using ActivitySource[/+]terrajobst commentedon Feb 16, 2021
Video
tarekgh commentedon Feb 18, 2021
Discussed offline more the proposal with @dotnet/fxdc to ensure we are addressing the request in #43853 and decided we'll add the extra parameter
ActivityIdFormat idFormat = ActivityIdFormat.Unknownto the last 2 overloads. the final approved proposal now is: