Skip to content

Commit

Permalink
Fix Activity.ParentId trace flags part (#49162)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekgh authored Mar 7, 2021
1 parent 6ce5878 commit 49eed0e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public partial class Activity : IDisposable
private string? _spanId;

private byte _w3CIdFlags;
private byte _parentTraceFlags;

private TagsLinkedList? _tags;
private BaggageLinkedList? _baggage;
Expand Down Expand Up @@ -213,14 +214,19 @@ public string? Id
/// </summary>
public string? ParentId
{
#if ALLOW_PARTIALLY_TRUSTED_CALLERS
[System.Security.SecuritySafeCriticalAttribute]
#endif
get
{
// if we represented it as a traceId-spanId, convert it to a string.
if (_parentId == null)
{
if (_parentSpanId != null)
{
string parentId = "00-" + _traceId + "-" + _parentSpanId + "-00";
Span<char> flagsChars = stackalloc char[2];
HexConverter.ToCharsBuffer((byte)((~ActivityTraceFlagsIsSet) & _parentTraceFlags), flagsChars, 0, HexConverter.Casing.Lower);
string parentId = "00-" + _traceId + "-" + _parentSpanId + "-" + flagsChars.ToString();
Interlocked.CompareExchange(ref _parentId, parentId, null);
}
else if (Parent != null)
Expand Down Expand Up @@ -551,6 +557,7 @@ public Activity SetParentId(ActivityTraceId traceId, ActivitySpanId spanId, Acti
_traceId = traceId.ToHexString(); // The child will share the parent's traceId.
_parentSpanId = spanId.ToHexString();
ActivityTraceFlags = activityTraceFlags;
_parentTraceFlags = (byte) activityTraceFlags;
}
return this;
}
Expand Down Expand Up @@ -1072,6 +1079,7 @@ internal static Activity Create(ActivitySource source, string name, ActivityKind
}

activity.ActivityTraceFlags = parentContext.TraceFlags;
activity._parentTraceFlags = (byte) parentContext.TraceFlags;
activity._traceState = parentContext.TraceState;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,32 @@ public void StructEnumerator_TagsLinkedList()
Assert.True(method.ReturnType.IsValueType);
}

[Fact]
public void TestParentTraceFlags()
{
Activity a = new Activity("ParentFlagsA");
a.SetIdFormat(ActivityIdFormat.W3C);
a.SetParentId(ActivityTraceId.CreateFromString("0123456789abcdef0123456789abcdef".AsSpan()), ActivitySpanId.CreateFromString("0123456789abcdef".AsSpan()), ActivityTraceFlags.Recorded);
Assert.Equal("00-0123456789abcdef0123456789abcdef-0123456789abcdef-01", a.ParentId);

Activity b = new Activity("ParentFlagsB");
b.SetIdFormat(ActivityIdFormat.W3C);
b.SetParentId(ActivityTraceId.CreateFromString("0123456789abcdef0123456789abcdef".AsSpan()), ActivitySpanId.CreateFromString("0123456789abcdef".AsSpan()), ActivityTraceFlags.None);
b.ActivityTraceFlags = ActivityTraceFlags.Recorded; // Setting ActivityTraceFlags shouldn't affect the parent
Assert.Equal("00-0123456789abcdef0123456789abcdef-0123456789abcdef-00", b.ParentId);

using ActivitySource aSource = new ActivitySource("CheckParentTraceFlags");
using ActivityListener listener = new ActivityListener();
listener.ShouldListenTo = (activitySource) => object.ReferenceEquals(aSource, activitySource);
listener.Sample = (ref ActivityCreationOptions<ActivityContext> activityOptions) => ActivitySamplingResult.AllDataAndRecorded;
ActivitySource.AddActivityListener(listener);

ActivityContext parentContext = new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded);
a = aSource.CreateActivity("WithContext", ActivityKind.Internal, parentContext, default, default, ActivityIdFormat.W3C);
Assert.NotNull(a);
Assert.Equal("00-" + parentContext.TraceId + "-" + parentContext.SpanId + "-01", a.ParentId);
}

[Fact]
public void TestStatus()
{
Expand Down

0 comments on commit 49eed0e

Please sign in to comment.