Skip to content

Commit

Permalink
[Tracer] SpanLinks Permissive null Clean Up (#5674)
Browse files Browse the repository at this point in the history
* Addressing null permissions

* Addressing possible null sent to ParseTraceState
  • Loading branch information
link04 authored Jun 10, 2024
1 parent 9c01290 commit 5c3218a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 57 deletions.
113 changes: 58 additions & 55 deletions tracer/src/Datadog.Trace/Activity/OtlpHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,75 +218,78 @@ private static void ExtractActivityLinks<TInner>(Span span, IActivity5? activity

foreach (var link in (activity5.Links))
{
if (link.TryDuckCast<IActivityLink>(out var duckLink))
if (!link.TryDuckCast<IActivityLink>(out var duckLink)
|| duckLink.Context.TraceId.TraceId is null
|| duckLink.Context.SpanId.SpanId is null)
{
if (duckLink.Context.TraceId.TraceId is null || duckLink.Context.SpanId.SpanId is null)
{
continue;
}
continue;
}

_ = HexString.TryParseTraceId(duckLink.Context.TraceId.TraceId, out var newActivityTraceId);
_ = HexString.TryParseUInt64(duckLink.Context.SpanId.SpanId, out var newActivitySpanId);
var traceParentSample = duckLink.Context.TraceFlags > 0;
var traceState = W3CTraceContextPropagator.ParseTraceState(duckLink.Context.TraceState ?? string.Empty);
var parsedTraceId = HexString.TryParseTraceId(duckLink.Context.TraceId.TraceId, out var newActivityTraceId);
var parsedSpanId = HexString.TryParseUInt64(duckLink.Context.SpanId.SpanId, out var newActivitySpanId);

var samplingPriority = traceParentSample switch
{
true when traceState.SamplingPriority is > 0 => traceState.SamplingPriority.Value,
true => SamplingPriorityValues.AutoKeep,
false when traceState.SamplingPriority is <= 0 => traceState.SamplingPriority.Value,
false => SamplingPriorityValues.AutoReject,
};
if (!parsedTraceId || !parsedSpanId)
{
continue;
}

var spanContext = new SpanContext(
newActivityTraceId,
newActivitySpanId,
samplingPriority: samplingPriority,
serviceName: null,
origin: traceState.Origin,
isRemote: duckLink.Context.IsRemote);
var traceParentSample = duckLink.Context.TraceFlags > 0;
var traceState = W3CTraceContextPropagator.ParseTraceState(duckLink.Context.TraceState);
var traceTags = TagPropagation.ParseHeader(traceState.PropagatedTags);
var samplingPriority = traceParentSample switch
{
true when traceState.SamplingPriority != null && SamplingPriorityValues.IsKeep(traceState.SamplingPriority.Value) => traceState.SamplingPriority.Value,
true => SamplingPriorityValues.AutoKeep,
false when traceState.SamplingPriority != null && SamplingPriorityValues.IsDrop(traceState.SamplingPriority.Value) => traceState.SamplingPriority.Value,
false => SamplingPriorityValues.AutoReject
};

var traceTags = TagPropagation.ParseHeader(traceState.PropagatedTags);
if (traceParentSample && SamplingPriorityValues.IsDrop(samplingPriority))
{
traceTags.SetTag(Tags.Propagated.DecisionMaker, "-0");
}
else if (!traceParentSample && SamplingPriorityValues.IsKeep(samplingPriority))
{
traceTags.RemoveTag(Tags.Propagated.DecisionMaker);
}

if (traceParentSample && traceState.SamplingPriority <= 0)
{
traceTags.SetTag(Tags.Propagated.DecisionMaker, "-0");
}
else if (!traceParentSample && traceState.SamplingPriority > 0)
{
traceTags.RemoveTag(Tags.Propagated.DecisionMaker);
}
var spanContext = new SpanContext(
newActivityTraceId,
newActivitySpanId,
samplingPriority: samplingPriority,
serviceName: null,
origin: traceState.Origin,
isRemote: duckLink.Context.IsRemote);

spanContext.AdditionalW3CTraceState = traceState.AdditionalValues;
spanContext.LastParentId = traceState.LastParent;
spanContext.PropagatedTags = traceTags;
spanContext.AdditionalW3CTraceState = traceState.AdditionalValues;
spanContext.LastParentId = traceState.LastParent;
spanContext.PropagatedTags = traceTags;

var extractedSpan = new Span(spanContext, DateTimeOffset.Now, new CommonTags());
var spanLink = span.AddSpanLink(extractedSpan);
var extractedSpan = new Span(spanContext, DateTimeOffset.Now, new CommonTags());
var spanLink = span.AddSpanLink(extractedSpan);

if (duckLink.Tags is not null)
if (duckLink.Tags is not null)
{
foreach (var kvp in duckLink.Tags)
{
foreach (var kvp in duckLink.Tags)
if (!string.IsNullOrEmpty(kvp.Key)
&& IsAllowedAtributeType(kvp.Value))
{
if (!string.IsNullOrEmpty(kvp.Key)
&& IsAllowedAtributeType(kvp.Value))
if (kvp.Value is Array array)
{
if (kvp.Value is Array array)
int index = 0;
foreach (var item in array)
{
int index = 0;
foreach (var item in array)
if (item?.ToString() is { } value)
{
if (item is not null)
{
spanLink.AddAttribute($"{kvp.Key}.{index}", item.ToString()!);
index++;
}
spanLink.AddAttribute($"{kvp.Key}.{index}", value);
index++;
}
}
else
{
spanLink.AddAttribute(kvp.Key, kvp.Value!.ToString()!);
}
}
else if (kvp.Value?.ToString() is { } kvpValue)
{
spanLink.AddAttribute(kvp.Key, kvpValue);
}
}
}
Expand Down Expand Up @@ -376,7 +379,7 @@ internal static void SetTagObject(Span span, string key, object? value, bool all
if (index == 0)
{
// indicates that it was an empty array, we need to add the tag
AgentSetOtlpTag(span, key, JsonConvert.SerializeObject(value));
AgentSetOtlpTag(span, key, "[]");
}
}
else
Expand Down Expand Up @@ -641,7 +644,7 @@ private static bool IsAllowedAtributeType(object? value)
return false;
}

value = array.GetValue(0)!;
value = array.GetValue(0);

if (value is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,15 @@ internal static bool TryParseTraceParent(string header, out W3CTraceParent trace
return true;
}

internal static W3CTraceState ParseTraceState(string header)
internal static W3CTraceState ParseTraceState(string? header)
{
// header format: "[*,]dd=s:1;o:rum;t.dm:-4;t.usr.id:12345[,*]"
if (string.IsNullOrWhiteSpace(header))
{
return new W3CTraceState(samplingPriority: null, origin: null, lastParent: ZeroLastParent, propagatedTags: null, additionalValues: null);
}

SplitTraceStateValues(header, out var ddValues, out var additionalValues);
SplitTraceStateValues(header!, out var ddValues, out var additionalValues);

if (ddValues is null or { Length: < 6 })
{
Expand Down

0 comments on commit 5c3218a

Please sign in to comment.