opentelemetry-dotnet
opentelemetry-dotnet copied to clipboard
Root Activity creation is not supported (in presence of active parent)
Describe your environment. Describe any aspect of your environment relevant to the problem:
- SDK version: (nuget versions of the all the relevant packages) 0.4.0-beta
- .NET runtime version (.NET or .NET Core, TargetFramework in the .csproj file): any
- Platform and OS version: any
Steps to reproduce.
OTel API spec requires a method that create a root span Implementations MUST provide an option to create a Span as a root span
There is no option to force .NET Activity to be root in the presence of Activity.Current
It seems to be an edge-case scenario: we fork current execution and link Activity.Current rather than creating a child of it.
Below is a workaround for this issue.
using (var parent = mySource.StartActivity("parent"))
{
// dirty hack to force creating new Activity without parent
Activity.Current = null;
using (var newRoot = mySource.StartActivity(
"new root",
ActivityKind.Internal,
parentContext: default,
tags: null,
links: new[] { new ActivityLink(parent.Context) }))
{
// do stuff
}
// Current is null and we need another dirty hack to bring it back
// there are other implicit dirty hacks (create newRoot in async Task
// so that Current changes are not propagated back to parent execution context )
Activity.Current = parent;
}
What is the expected behavior? There should be a way to force root Activity creation without hacks. There should be a way to restore original context after forced root is gone.
What is the actual behavior?
There is no API to force root creation - you have to provide the fake new context to force it. After new root is gone, Current needs to be manually restored to parent
I'm creating this issue to discuss whether OTel or .NET should support this scenario. On the .NET side, it should be fine to ship it in the future release (the change would be controversial and not critical enough). This scenario is a real-world case from one of the MS internal teams.
Can we support this on OTel side in the meantime?
-
one option is to support it through Span API: implement
Tracerextension methodStartRootSpanand make sure Current is captured/restored. This will force instrumentation to use Span API on top of Activity. -
another options to support it thought
ActivitySourceextension method, but then there is no straightforward way to restoreCurrentafter newRoot ends and only possible if Activity would capture active context (but not parent) before is starts and restore it on dispose.
cc: @rajkumar-rangaraj We can handle this in OTel API while figuring out if .NET can support it in next release. We already have an issue about StartSpan vs StartActiveSpan which also need to do similar messing with .Current. @rajkumar-rangaraj is working to get that, and I think this can be clubbed into that.
@rajkumar-rangaraj Can you confirm if this is addressed?
@rajkumar-rangaraj Can you confirm if this is addressed?
Yes @cijothomas, this part is fixed in tracer shim. PR #994
I've created a sample implementation for that workaround: https://gist.github.com/arakis/6063fc4c263ef797651c7066d6a47870
+1 on this request. Just a simple helper or an override to indicate “forceNew” will help. We are trying to stitch async workflows using trace links in our services
Tagging with need-runtime-change as this likely needs support from DS.
+1 on this request as I am having to do a similar workaround here in this sample: https://github.com/open-telemetry/opentelemetry-dotnet/pull/4957
This issue was marked stale due to lack of activity and will be closed in 7 days. Commenting will instruct the bot to automatically remove the label. This bot runs once per day.