Skip to content

Commit 5c3218a

Browse files
authored
[Tracer] SpanLinks Permissive null Clean Up (#5674)
* Addressing null permissions * Addressing possible null sent to ParseTraceState
1 parent 9c01290 commit 5c3218a

File tree

2 files changed

+60
-57
lines changed

2 files changed

+60
-57
lines changed

tracer/src/Datadog.Trace/Activity/OtlpHelpers.cs

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -218,75 +218,78 @@ private static void ExtractActivityLinks<TInner>(Span span, IActivity5? activity
218218

219219
foreach (var link in (activity5.Links))
220220
{
221-
if (link.TryDuckCast<IActivityLink>(out var duckLink))
221+
if (!link.TryDuckCast<IActivityLink>(out var duckLink)
222+
|| duckLink.Context.TraceId.TraceId is null
223+
|| duckLink.Context.SpanId.SpanId is null)
222224
{
223-
if (duckLink.Context.TraceId.TraceId is null || duckLink.Context.SpanId.SpanId is null)
224-
{
225-
continue;
226-
}
225+
continue;
226+
}
227227

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

233-
var samplingPriority = traceParentSample switch
234-
{
235-
true when traceState.SamplingPriority is > 0 => traceState.SamplingPriority.Value,
236-
true => SamplingPriorityValues.AutoKeep,
237-
false when traceState.SamplingPriority is <= 0 => traceState.SamplingPriority.Value,
238-
false => SamplingPriorityValues.AutoReject,
239-
};
231+
if (!parsedTraceId || !parsedSpanId)
232+
{
233+
continue;
234+
}
240235

241-
var spanContext = new SpanContext(
242-
newActivityTraceId,
243-
newActivitySpanId,
244-
samplingPriority: samplingPriority,
245-
serviceName: null,
246-
origin: traceState.Origin,
247-
isRemote: duckLink.Context.IsRemote);
236+
var traceParentSample = duckLink.Context.TraceFlags > 0;
237+
var traceState = W3CTraceContextPropagator.ParseTraceState(duckLink.Context.TraceState);
238+
var traceTags = TagPropagation.ParseHeader(traceState.PropagatedTags);
239+
var samplingPriority = traceParentSample switch
240+
{
241+
true when traceState.SamplingPriority != null && SamplingPriorityValues.IsKeep(traceState.SamplingPriority.Value) => traceState.SamplingPriority.Value,
242+
true => SamplingPriorityValues.AutoKeep,
243+
false when traceState.SamplingPriority != null && SamplingPriorityValues.IsDrop(traceState.SamplingPriority.Value) => traceState.SamplingPriority.Value,
244+
false => SamplingPriorityValues.AutoReject
245+
};
248246

249-
var traceTags = TagPropagation.ParseHeader(traceState.PropagatedTags);
247+
if (traceParentSample && SamplingPriorityValues.IsDrop(samplingPriority))
248+
{
249+
traceTags.SetTag(Tags.Propagated.DecisionMaker, "-0");
250+
}
251+
else if (!traceParentSample && SamplingPriorityValues.IsKeep(samplingPriority))
252+
{
253+
traceTags.RemoveTag(Tags.Propagated.DecisionMaker);
254+
}
250255

251-
if (traceParentSample && traceState.SamplingPriority <= 0)
252-
{
253-
traceTags.SetTag(Tags.Propagated.DecisionMaker, "-0");
254-
}
255-
else if (!traceParentSample && traceState.SamplingPriority > 0)
256-
{
257-
traceTags.RemoveTag(Tags.Propagated.DecisionMaker);
258-
}
256+
var spanContext = new SpanContext(
257+
newActivityTraceId,
258+
newActivitySpanId,
259+
samplingPriority: samplingPriority,
260+
serviceName: null,
261+
origin: traceState.Origin,
262+
isRemote: duckLink.Context.IsRemote);
259263

260-
spanContext.AdditionalW3CTraceState = traceState.AdditionalValues;
261-
spanContext.LastParentId = traceState.LastParent;
262-
spanContext.PropagatedTags = traceTags;
264+
spanContext.AdditionalW3CTraceState = traceState.AdditionalValues;
265+
spanContext.LastParentId = traceState.LastParent;
266+
spanContext.PropagatedTags = traceTags;
263267

264-
var extractedSpan = new Span(spanContext, DateTimeOffset.Now, new CommonTags());
265-
var spanLink = span.AddSpanLink(extractedSpan);
268+
var extractedSpan = new Span(spanContext, DateTimeOffset.Now, new CommonTags());
269+
var spanLink = span.AddSpanLink(extractedSpan);
266270

267-
if (duckLink.Tags is not null)
271+
if (duckLink.Tags is not null)
272+
{
273+
foreach (var kvp in duckLink.Tags)
268274
{
269-
foreach (var kvp in duckLink.Tags)
275+
if (!string.IsNullOrEmpty(kvp.Key)
276+
&& IsAllowedAtributeType(kvp.Value))
270277
{
271-
if (!string.IsNullOrEmpty(kvp.Key)
272-
&& IsAllowedAtributeType(kvp.Value))
278+
if (kvp.Value is Array array)
273279
{
274-
if (kvp.Value is Array array)
280+
int index = 0;
281+
foreach (var item in array)
275282
{
276-
int index = 0;
277-
foreach (var item in array)
283+
if (item?.ToString() is { } value)
278284
{
279-
if (item is not null)
280-
{
281-
spanLink.AddAttribute($"{kvp.Key}.{index}", item.ToString()!);
282-
index++;
283-
}
285+
spanLink.AddAttribute($"{kvp.Key}.{index}", value);
286+
index++;
284287
}
285288
}
286-
else
287-
{
288-
spanLink.AddAttribute(kvp.Key, kvp.Value!.ToString()!);
289-
}
289+
}
290+
else if (kvp.Value?.ToString() is { } kvpValue)
291+
{
292+
spanLink.AddAttribute(kvp.Key, kvpValue);
290293
}
291294
}
292295
}
@@ -376,7 +379,7 @@ internal static void SetTagObject(Span span, string key, object? value, bool all
376379
if (index == 0)
377380
{
378381
// indicates that it was an empty array, we need to add the tag
379-
AgentSetOtlpTag(span, key, JsonConvert.SerializeObject(value));
382+
AgentSetOtlpTag(span, key, "[]");
380383
}
381384
}
382385
else
@@ -641,7 +644,7 @@ private static bool IsAllowedAtributeType(object? value)
641644
return false;
642645
}
643646

644-
value = array.GetValue(0)!;
647+
value = array.GetValue(0);
645648

646649
if (value is null)
647650
{

tracer/src/Datadog.Trace/Propagators/W3CTraceContextPropagator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,15 @@ internal static bool TryParseTraceParent(string header, out W3CTraceParent trace
322322
return true;
323323
}
324324

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

333-
SplitTraceStateValues(header, out var ddValues, out var additionalValues);
333+
SplitTraceStateValues(header!, out var ddValues, out var additionalValues);
334334

335335
if (ddValues is null or { Length: < 6 })
336336
{

0 commit comments

Comments
 (0)