Skip to content

Commit

Permalink
[Tracing] Add IsRemote to SpanContext (#5385)
Browse files Browse the repository at this point in the history
* Add IsRemote to SpanContext

This will help in the future when we want an easy
way to check if a SpanContext originated from
some distributed context compared to being
originated from a local context.

* DistributedContextExtractor shouldn't be remote

* Set IsRemote via SpanContext ctor

* Actually update the comment

Co-authored-by: Lucas Pimentel <lucas.pimentel@datadoghq.com>

---------

Co-authored-by: Lucas Pimentel <lucas.pimentel@datadoghq.com>
  • Loading branch information
bouwkast and lucaspimentel authored Apr 9, 2024
1 parent c9a65e8 commit e35b85c
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public bool TryExtract<TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGette

TelemetryFactory.Metrics.RecordCountContextHeaderStyleExtracted(MetricTags.ContextHeaderStyle.B3Multi);
var samplingPriority = ParseUtility.ParseInt32(carrier, carrierGetter, Sampled);
spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId, rawSpanId);
spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId, rawSpanId, isRemote: true);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public bool TryExtract<TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGette
}

var samplingPriority = rawSampled == '1' ? 1 : 0;
spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId.ToString(), rawSpanId.ToString());
spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId.ToString(), rawSpanId.ToString(), isRemote: true);
#else
string? rawTraceId;
string? rawSpanId;
Expand Down Expand Up @@ -127,7 +127,7 @@ public bool TryExtract<TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGette
}

var samplingPriority = rawSampled == '1' ? 1 : 0;
spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId, rawSpanId);
spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId, rawSpanId, isRemote: true);
#endif

TelemetryFactory.Metrics.RecordCountContextHeaderStyleExtracted(MetricTags.ContextHeaderStyle.B3SingleHeader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public bool TryExtract<TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGette
// and the upper 64 bits in "_dd.p.tid"
var traceId = GetFullTraceId((ulong)traceIdLower, traceTags);

spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, origin)
spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, origin, isRemote: true)
{
PropagatedTags = traceTags
PropagatedTags = traceTags,
};

TelemetryFactory.Metrics.RecordCountContextHeaderStyleExtracted(MetricTags.ContextHeaderStyle.Datadog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public bool TryExtract<TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGette
traceId = GetFullTraceId((ulong)traceIdLower, traceTags);
}

spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, origin, rawTraceId, rawSpanId)
// we don't consider contexts coming from this as "remote" as it could be from a version conflict scenario
spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, origin, rawTraceId, rawSpanId, isRemote: false)
{
PropagatedTags = traceTags,
AdditionalW3CTraceState = w3CTraceState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,8 @@ public bool TryExtract<TCarrier, TCarrierGetter>(
serviceName: null,
origin: traceState.Origin,
rawTraceId: traceParent.RawTraceId,
rawSpanId: traceParent.RawParentId);
rawSpanId: traceParent.RawParentId,
isRemote: true);

spanContext.PropagatedTags = traceTags;
spanContext.AdditionalW3CTraceState = traceState.AdditionalValues;
Expand Down
17 changes: 14 additions & 3 deletions tracer/src/Datadog.Trace/SpanContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ public SpanContext(ulong? traceId, ulong spanId, SamplingPriority? samplingPrior
/// <param name="samplingPriority">The propagated sampling priority.</param>
/// <param name="serviceName">The service name to propagate to child spans.</param>
/// <param name="origin">The propagated origin of the trace.</param>
internal SpanContext(TraceId traceId, ulong spanId, int? samplingPriority, string serviceName, string origin)
/// <param name="isRemote">Whether this <see cref="SpanContext"/> was from a distributed context.</param>
internal SpanContext(TraceId traceId, ulong spanId, int? samplingPriority, string serviceName, string origin, bool isRemote = false)
: this(traceId, serviceName)
{
SpanId = spanId;
SamplingPriority = samplingPriority;
Origin = origin;
IsRemote = isRemote;
}

/// <summary>
Expand All @@ -102,14 +104,16 @@ internal SpanContext(TraceId traceId, ulong spanId, int? samplingPriority, strin
/// <param name="origin">The propagated origin of the trace.</param>
/// <param name="rawTraceId">The raw propagated trace id</param>
/// <param name="rawSpanId">The raw propagated span id</param>
internal SpanContext(TraceId traceId, ulong spanId, int? samplingPriority, string serviceName, string origin, string rawTraceId, string rawSpanId)
/// <param name="isRemote">Whether this <see cref="SpanContext"/> was from a distributed context.</param>
internal SpanContext(TraceId traceId, ulong spanId, int? samplingPriority, string serviceName, string origin, string rawTraceId, string rawSpanId, bool isRemote = false)
: this(traceId, serviceName)
{
SpanId = spanId;
SamplingPriority = samplingPriority;
Origin = origin;
_rawTraceId = rawTraceId;
_rawSpanId = rawSpanId;
IsRemote = isRemote;
}

/// <summary>
Expand All @@ -123,7 +127,8 @@ internal SpanContext(TraceId traceId, ulong spanId, int? samplingPriority, strin
/// <param name="spanId">The propagated span id.</param>
/// <param name="rawTraceId">Raw trace id value</param>
/// <param name="rawSpanId">Raw span id value</param>
internal SpanContext(ISpanContext parent, TraceContext traceContext, string serviceName, TraceId traceId = default, ulong spanId = 0, string rawTraceId = null, string rawSpanId = null)
/// <param name="isRemote">Whether this <see cref="SpanContext"/> was from a distributed context.</param>
internal SpanContext(ISpanContext parent, TraceContext traceContext, string serviceName, TraceId traceId = default, ulong spanId = 0, string rawTraceId = null, string rawSpanId = null, bool isRemote = false)
: this(GetTraceId(parent, traceId), serviceName)
{
// if 128-bit trace ids are enabled, also use full uint64 for span id,
Expand All @@ -145,6 +150,7 @@ internal SpanContext(ISpanContext parent, TraceContext traceContext, string serv
}

_rawSpanId = rawSpanId;
IsRemote = isRemote;
}

private SpanContext(TraceId traceId, string serviceName)
Expand Down Expand Up @@ -257,6 +263,11 @@ internal string Origin

internal PathwayContext? PathwayContext { get; private set; }

/// <summary>
/// Gets a value indicating whether this <see cref="SpanContext"/> was propagated from a remote parent.
/// </summary>
internal bool IsRemote { get; }

/// <inheritdoc/>
int IReadOnlyCollection<KeyValuePair<string, string>>.Count => KeyNames.Length;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public void Extract_IHeadersCollection(
RawSpanId = rawSpanId,
Origin = null,
SamplingPriority = samplingPriority,
IsRemote = true,
});
}

Expand Down Expand Up @@ -211,6 +212,7 @@ public void Extract_CarrierAndDelegate(
RawSpanId = rawSpanId,
Origin = null,
SamplingPriority = samplingPriority,
IsRemote = true,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public void Extract_IHeadersCollection(string header, ulong traceIdUpper, ulong
RawSpanId = rawSpanId,
Origin = null,
SamplingPriority = samplingPriority,
IsRemote = true,
});
}

Expand Down Expand Up @@ -162,6 +163,7 @@ public void Extract_CarrierAndDelegate(string header, ulong traceIdUpper, ulong
RawSpanId = rawSpanId,
Origin = null,
SamplingPriority = samplingPriority,
IsRemote = true,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public void Extract_IHeadersCollection()
Origin = Origin,
SamplingPriority = SamplingPriority,
PropagatedTags = PropagatedTagsCollection,
IsRemote = true,
});
}

Expand All @@ -238,6 +239,7 @@ public void Extract_CarrierAndDelegate()
Origin = Origin,
SamplingPriority = SamplingPriority,
PropagatedTags = PropagatedTagsCollection,
IsRemote = true,
});
}

Expand All @@ -261,6 +263,7 @@ public void Extract_ReadOnlyDictionary()
Origin = Origin,
SamplingPriority = SamplingPriority,
PropagatedTags = PropagatedTagsCollection,
IsRemote = true,
});
}

Expand Down Expand Up @@ -291,6 +294,7 @@ public void Extract_TraceIdOnly()
RawTraceId = RawTraceId,
RawSpanId = "0000000000000000",
PropagatedTags = EmptyPropagatedTags,
IsRemote = true,
});
}

Expand Down Expand Up @@ -355,6 +359,7 @@ public void Extract_InvalidSpanId(string spanId)
Origin = Origin,
SamplingPriority = SamplingPriority,
PropagatedTags = PropagatedTagsCollection,
IsRemote = true,
});
}

Expand Down Expand Up @@ -389,6 +394,7 @@ public void Extract_InvalidSamplingPriority(string samplingPriority, int? expect
Origin = Origin,
SamplingPriority = expectedSamplingPriority,
PropagatedTags = PropagatedTagsCollection,
IsRemote = true,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void Extract_InvalidSpanId(string spanId)
RawSpanId = RawSpanId,
SamplingPriority = SamplingPriority,
PropagatedTags = PropagatedTagsCollection,
AdditionalW3CTraceState = AdditionalW3CTraceState
AdditionalW3CTraceState = AdditionalW3CTraceState,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public void Extract_B3_IHeadersCollection()
RawSpanId = "000000003ade68b1",
Origin = null,
SamplingPriority = SamplingPriorityValues.AutoKeep,
IsRemote = true,
});
}

Expand All @@ -319,6 +320,7 @@ public void Extract_B3SingleHeader_IHeadersCollection()
RawSpanId = "000000003ade68b1",
Origin = null,
SamplingPriority = SamplingPriorityValues.AutoKeep,
IsRemote = true,
});
}

Expand Down Expand Up @@ -349,6 +351,7 @@ public void Extract_W3C_IHeadersCollection_traceparent()
Origin = null,
SamplingPriority = SamplingPriorityValues.AutoKeep,
PropagatedTags = EmptyPropagatedTags,
IsRemote = true,
});
}

Expand Down Expand Up @@ -385,6 +388,7 @@ public void Extract_W3C_IHeadersCollection_traceparent_tracestate()
PropagatedTags = PropagatedTagsCollection,
Parent = null,
ParentId = null,
IsRemote = true,
});
}

Expand Down Expand Up @@ -424,6 +428,7 @@ public void Extract_Datadog_IHeadersCollection()
Origin = "rum",
SamplingPriority = SamplingPriorityValues.AutoKeep,
PropagatedTags = PropagatedTagsCollection,
IsRemote = true,
});
}

Expand Down Expand Up @@ -571,6 +576,7 @@ public void TraceContextPrecedence_Respected_WhenHavingMatchingTraceIds(bool ext
AdditionalW3CTraceState = !extractFirst || w3CHeaderFirst ? "foo=1" : null,
Parent = null,
ParentId = null,
IsRemote = true,
});
}

Expand Down Expand Up @@ -622,6 +628,7 @@ public void TraceContextPrecedence_Correct_WithDifferentTracestate(bool extractF
AdditionalW3CTraceState = !extractFirst || w3CHeaderFirst ? "foo=1" : null,
Parent = null,
ParentId = null,
IsRemote = true,
});
}

Expand Down Expand Up @@ -673,6 +680,7 @@ public void TraceContextPrecedence_ExtractedState_WhenMissingDD_OnTracestate(boo
AdditionalW3CTraceState = !extractFirst || w3CHeaderFirst ? "foo=1" : null,
Parent = null,
ParentId = null,
IsRemote = true,
});
}

Expand Down Expand Up @@ -724,6 +732,7 @@ public void TraceContextPrecedence_ConsistentBehaviour_WithDifferentParentId(boo
AdditionalW3CTraceState = !extractFirst || w3CHeaderFirst ? "foo=1" : null,
Parent = null,
ParentId = null,
IsRemote = true,
});
}

Expand Down Expand Up @@ -777,6 +786,7 @@ public void TraceContextPrecedence_ConsistentBehaviour_WithDifferentTraceIds(boo
AdditionalW3CTraceState = w3CHeaderFirst ? "foo=1" : null,
Parent = null,
ParentId = null,
IsRemote = true,
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="SpanContextMock.cs" company="Datadog">
// <copyright file="SpanContextMock.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
Expand Down Expand Up @@ -27,6 +27,8 @@ internal class SpanContextMock

public string AdditionalW3CTraceState { get; set; }

public bool IsRemote { get; set; }

public ISpanContext Parent { get; set; }

public ulong? ParentId { get; set; }
Expand Down
Loading

0 comments on commit e35b85c

Please sign in to comment.