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
Activity's SetParentId() API doesn't set parent span id as told to #46706
Currently the runtime provides SetParentId() API to override the parent id and trace id of the activity. We noticed that if we're calling this API to a root activity, which has no parent span id, and set the parent span id (the second parameter) as default, its parent span id will eventually be converted to 0000000000000000, instead of default or null as we expected. In this case, is it better for this API to keep consistence with what it's told to be set and is it more reasonable for a root activity to have a null parent span id after applying this API?
Would you consider updating the current code similar as the following to fit this case?
public Activity SetParentId(ActivityTraceId traceId, ActivitySpanId spanId, ActivityTraceFlags activityTraceFlags = ActivityTraceFlags.None)
{
if (Parent != null)
{
NotifyError(new InvalidOperationException(SR.SetParentIdOnActivityWithParent));
}
else if (ParentId != null || _parentSpanId != null)
{
NotifyError(new InvalidOperationException(SR.ParentIdAlreadySet));
}
else
{
_traceId = traceId.ToHexString(); // The child will share the parent's traceId.
_parentSpanId = spanId == default ? null : spanId.ToHexString(); // line of change
ActivityTraceFlags = activityTraceFlags;
}
return this;
}
@lupengamzn I think this is reasonable. The only concern is this can affect the current value we return from Activity.ParentId which I am not sure if this will be high risk for the app compatibility.
Could you please elaborate more on why you need this change? what scenario you think current behavior would be problematic?
We're planing to override the trace id of a root activity which is created by .NET runtime and we found out that there is no public API for us to do so. The only API available for us to touch the trace id is through SetParentId() so we use it and pass new trace id, default parent span id, and the original trace flags of activity hoping to get an activity with the new trace id, but parent span id and original trace flags the same as previous. We found out that, if we pass default as parent span id to this API, the activity's parent span id is no longer null, but overridden to 0000000000000000 instead, which will give us 00-new trace id-0000000000000000-00 when we call activity.ParentId, instead of null as we expected.
We're thinking if it's more suitable for this API to keep the parent span id as null when we pass default as the parent span id as the second parameter. After that, when we call this API again and only passing the new trace id and keep the second and third parameters the same as before, only the trace id of activity will be overridden and the activity.ParentId will return null still.
I'd like to find out why the broader scenario needs the dev to have direct control of the trace id for the Activity? I'm only aware of two typical scenarios for activities and neither of them needs that ability:
root -> create a new random id
not a root -> there is a parent and the id flows from the parent
I'm not inherently opposed to supporting a 3rd option where the developer specifies the id but there are some costs/consequences for us to do that so its important to understand what is the benefit for .NET devs. One consequence is that we have to resolve what is supposed to happen if anyone combines this with setting Activity.DefaultFormat = ActivityFormat.Hierarchical and Activity.ForceDefaultFormat = true. A 2nd is that the new ActivitySource.StartActivity() code path wouldn't support it so either we need some more changes there or we need to decide why it doesn't matter in that case.
Fwiw if we do this I think we should consider adding a new SetTraceId() API rather than modifying SetParentId(). It feels counter-intuitive to me that after calling SetParentId(x,y) it might still be the case that ParentId == null. I get that is the goal in this case, but I don't think it is what anyone else looking at this API would expect to have happen. Anyone else who wanted the same behavior would probably not be able to discover how to do it.
Changing the trace id of a root activity by calling SetParentId() turns out counter-intuitive for us too, so adding a new API such as SetTraceId() would be the most ideal way as currently there seems no direct APIs for us to touch the trace id of an activity. We're working with OTel .NET SDK team and as OTel .NET SDK adopts trace context from .NET runtime, it would be great that you can provide OTel .NET developers an API to override the original random trace id as other OTel SDKs already support such feature. The use case of such API so far is to override the original trace id with a non-random one for a root activity so that it (and its child activities) can be further recognized and processed by specific backend vendor (like AWS X-Ray).
I opened an issue discussing this and the workaround on it would be much appreciated if it's decided to target .NET 6.
We're using activity listener to detect every activity and we're able to know if it's a root activity by checking if it's parentId is null or not. If it's root activity, we're calling SetParentId() to it to override the trace id. It seems a little bit tricky but is so far the only way we're able to come up with based on the APIs we can utilize from activity class.
And yes, sure, we can continue discussing on #46704 as they're ultimately aiming the same target.
Activity
tarekgh commentedon Jan 7, 2021
@lupengamzn I think this is reasonable. The only concern is this can affect the current value we return from Activity.ParentId which I am not sure if this will be high risk for the app compatibility.
Could you please elaborate more on why you need this change? what scenario you think current behavior would be problematic?
CC @noahfalk
lupengamzn commentedon Jan 7, 2021
Hey @tarekgh ,
Thanks for the quick response!
We're planing to override the trace id of a root activity which is created by .NET runtime and we found out that there is no public API for us to do so. The only API available for us to touch the trace id is through
SetParentId()so we use it and pass new trace id, default parent span id, and the original trace flags of activity hoping to get an activity with the new trace id, but parent span id and original trace flags the same as previous. We found out that, if we passdefaultas parent span id to this API, the activity's parent span id is no longernull, but overridden to0000000000000000instead, which will give us00-new trace id-0000000000000000-00when we call activity.ParentId, instead ofnullas we expected.We're thinking if it's more suitable for this API to keep the parent span id as
nullwhen we passdefaultas the parent span id as the second parameter. After that, when we call this API again and only passing the new trace id and keep the second and third parameters the same as before, only the trace id of activity will be overridden and the activity.ParentId will returnnullstill.tarekgh commentedon Jan 7, 2021
@lupengamzn thanks for clarifying that.
@noahfalk what you think about this change? I think the app compat would be minimal and the change looks desired.
noahfalk commentedon Jan 8, 2021
I'd like to find out why the broader scenario needs the dev to have direct control of the trace id for the Activity? I'm only aware of two typical scenarios for activities and neither of them needs that ability:
I'm not inherently opposed to supporting a 3rd option where the developer specifies the id but there are some costs/consequences for us to do that so its important to understand what is the benefit for .NET devs. One consequence is that we have to resolve what is supposed to happen if anyone combines this with setting Activity.DefaultFormat = ActivityFormat.Hierarchical and Activity.ForceDefaultFormat = true. A 2nd is that the new ActivitySource.StartActivity() code path wouldn't support it so either we need some more changes there or we need to decide why it doesn't matter in that case.
Fwiw if we do this I think we should consider adding a new SetTraceId() API rather than modifying SetParentId(). It feels counter-intuitive to me that after calling SetParentId(x,y) it might still be the case that ParentId == null. I get that is the goal in this case, but I don't think it is what anyone else looking at this API would expect to have happen. Anyone else who wanted the same behavior would probably not be able to discover how to do it.
lupengamzn commentedon Jan 8, 2021
Hey @noahfalk ,
Thanks for the response!
Changing the trace id of a root activity by calling
SetParentId()turns out counter-intuitive for us too, so adding a new API such asSetTraceId()would be the most ideal way as currently there seems no direct APIs for us to touch the trace id of an activity. We're working with OTel .NET SDK team and as OTel .NET SDK adopts trace context from .NET runtime, it would be great that you can provide OTel .NET developers an API to override the original random trace id as other OTel SDKs already support such feature. The use case of such API so far is to override the original trace id with a non-random one for a root activity so that it (and its child activities) can be further recognized and processed by specific backend vendor (like AWS X-Ray).I opened an issue discussing this and the workaround on it would be much appreciated if it's decided to target .NET 6.
tarekgh commentedon Jan 8, 2021
@lupengamzn do you control the root activity creation? I mean how to decide which Activity is the root activity?
Can we close this issue and continue the discussion in #46704 to not have teared discussion?
lupengamzn commentedon Jan 8, 2021
Hey @tarekgh ,
We're using activity listener to detect every activity and we're able to know if it's a root activity by checking if it's parentId is
nullor not. If it's root activity, we're callingSetParentId()to it to override the trace id. It seems a little bit tricky but is so far the only way we're able to come up with based on the APIs we can utilize from activity class.And yes, sure, we can continue discussing on #46704 as they're ultimately aiming the same target.
tarekgh commentedon Jan 8, 2021
Thanks, let's continue the discussion in the other issue. I'll try to copy and paste some of the discussions from here to there.