[Question] Tag child span with tags from parent tag/scope (without global tags)
I haven't been able to find this documented anywhere and haven't been able to figure it out poking through what's available in Datadog.Trace so I figure I'd ask here. Hopefully that's alright!
I'm trying to create a helper method that adds tags to the current active span from its parent span/scope. My situation is something like the following:
using Datadog.Trace;
static void Main()
{
Data myData = new Data
{
Value1 = "data1",
Value2 = "data2"
};
using(IScope parentScope = Tracer.Instance.StartActive("scope.parent"))
{
ISpan parentSpan = parentScope.Span;
parentSpan.SetTag("example1", myData.Value1);
parentSpan.SetTag("example2", myData.Value2);
DoOtherWork();
}
}
[Trace(OperationName="scope.child")]
static void DoOtherWork()
{
ISpan childSpan = Tracer.Instance.ActiveScope;
// I want to be able to add the parent tags to the child span from its parent
// NOTE: I *do not* have access to the same data object here and I can't just pass it in
}
What I'm ultimately trying to accomplish it to be able to search for/group operations in DataDog by an identifier, but the identifier will change numerous times throughout the life of the program and we have several threads going on so there are several of these identifiers at one time (meaning I can't just set the environment variable(s) dynamically either).
The application is a .NET Framework 4.6.1 worker service so no ASP.NET or anything crazy.
Thanks in advance for the time and assistance 😃
Hi, I'd like to request essentially the same thing. We are working on multi-tenant applications in .NET 6. I'd like to decorate our traces with tag for tenant id.
Currently i can decorate current span with tenant id as:
var scope = Tracer.Instance.ActiveScope;
scope.Span.SetTag("tenant-id", tenantId);
However this sets the tag only for this single span. I want this tag propagated to all spans within the same trace context (any calls to Redis, external http services, etc).
Conceptually this should not be a problem, because setting some selected tags for the whole trace context is already supported. For example setting env tag is always set on trace context level.
Code enabling this:
https://github.com/DataDog/dd-trace-dotnet/blob/master/tracer/src/Datadog.Trace/Span.cs#L188
A very simple backwards compatible change to the Span class could easily open this up so custom tags can be set on TraceContext instead of Span. Something like this:
ISpan ISpan.SetTag(string key, string value) => SetTag(key, value, false);
ISpan ISpan.SetTraceContextTag(string key, string value) => SetTag(key, value, true);
internal ISpan SetTag(string key, string value, bool setOnTraceContext = false)
{
if (IsFinished)
{
Log.Warning("SetTag should not be called after the span was closed");
return this;
}
static void LogMissingTraceContext(string key, string value, bool setOnTraceContext)
{
Log.Warning("Ignoring ISpan.SetTag({Key}, {Value}) because the span is not associated to a TraceContext.", key, value);
}
switch (key)
{
// all the spacial case handling removed to shorten the sample
default:
if (setOnTraceContext)
{
if (Context.TraceContext == null)
{
LogMissingTraceContext(key, value);
return this;
}
Context.TraceContext.Tags.SetTag(key, value);
}
else
{
Tags.SetTag(key, value);
}
break;
}
return this;
}
Hello @Troesler-illuminate, @vojtech-jakubec-deltatre , we have announced at Dash yesterday the private beta for Trace Queries which I believe would solve your use cases as you can lookup entire traces based on the tags of multiple spans. You can ask to be part of the private beta (cf the form at the top of the documentation I linked).
From a tracer point of view, we were not pushy to allow propagating a tag or setting a tag on a root span from a child span as it comes with extra complexities and would enforce us to use context propagation for the former and could be impossible in some cases for the latter. But Trace Level queries should solve most of the use cases we have investigating.
Let us know if that works out for you
Could you just use baggage for this? https://datadoghq.dev/dd-trace-js/interfaces/span.html#setbaggageitem and https://datadoghq.dev/dd-trace-js/interfaces/span.html#getbaggageitem