Skip to content

Commit 49eed0e

Browse files
authored
Fix Activity.ParentId trace flags part (#49162)
1 parent 6ce5878 commit 49eed0e

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public partial class Activity : IDisposable
7575
private string? _spanId;
7676

7777
private byte _w3CIdFlags;
78+
private byte _parentTraceFlags;
7879

7980
private TagsLinkedList? _tags;
8081
private BaggageLinkedList? _baggage;
@@ -213,14 +214,19 @@ public string? Id
213214
/// </summary>
214215
public string? ParentId
215216
{
217+
#if ALLOW_PARTIALLY_TRUSTED_CALLERS
218+
[System.Security.SecuritySafeCriticalAttribute]
219+
#endif
216220
get
217221
{
218222
// if we represented it as a traceId-spanId, convert it to a string.
219223
if (_parentId == null)
220224
{
221225
if (_parentSpanId != null)
222226
{
223-
string parentId = "00-" + _traceId + "-" + _parentSpanId + "-00";
227+
Span<char> flagsChars = stackalloc char[2];
228+
HexConverter.ToCharsBuffer((byte)((~ActivityTraceFlagsIsSet) & _parentTraceFlags), flagsChars, 0, HexConverter.Casing.Lower);
229+
string parentId = "00-" + _traceId + "-" + _parentSpanId + "-" + flagsChars.ToString();
224230
Interlocked.CompareExchange(ref _parentId, parentId, null);
225231
}
226232
else if (Parent != null)
@@ -551,6 +557,7 @@ public Activity SetParentId(ActivityTraceId traceId, ActivitySpanId spanId, Acti
551557
_traceId = traceId.ToHexString(); // The child will share the parent's traceId.
552558
_parentSpanId = spanId.ToHexString();
553559
ActivityTraceFlags = activityTraceFlags;
560+
_parentTraceFlags = (byte) activityTraceFlags;
554561
}
555562
return this;
556563
}
@@ -1072,6 +1079,7 @@ internal static Activity Create(ActivitySource source, string name, ActivityKind
10721079
}
10731080

10741081
activity.ActivityTraceFlags = parentContext.TraceFlags;
1082+
activity._parentTraceFlags = (byte) parentContext.TraceFlags;
10751083
activity._traceState = parentContext.TraceState;
10761084
}
10771085

src/libraries/System.Diagnostics.DiagnosticSource/tests/ActivityTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,32 @@ public void StructEnumerator_TagsLinkedList()
16941694
Assert.True(method.ReturnType.IsValueType);
16951695
}
16961696

1697+
[Fact]
1698+
public void TestParentTraceFlags()
1699+
{
1700+
Activity a = new Activity("ParentFlagsA");
1701+
a.SetIdFormat(ActivityIdFormat.W3C);
1702+
a.SetParentId(ActivityTraceId.CreateFromString("0123456789abcdef0123456789abcdef".AsSpan()), ActivitySpanId.CreateFromString("0123456789abcdef".AsSpan()), ActivityTraceFlags.Recorded);
1703+
Assert.Equal("00-0123456789abcdef0123456789abcdef-0123456789abcdef-01", a.ParentId);
1704+
1705+
Activity b = new Activity("ParentFlagsB");
1706+
b.SetIdFormat(ActivityIdFormat.W3C);
1707+
b.SetParentId(ActivityTraceId.CreateFromString("0123456789abcdef0123456789abcdef".AsSpan()), ActivitySpanId.CreateFromString("0123456789abcdef".AsSpan()), ActivityTraceFlags.None);
1708+
b.ActivityTraceFlags = ActivityTraceFlags.Recorded; // Setting ActivityTraceFlags shouldn't affect the parent
1709+
Assert.Equal("00-0123456789abcdef0123456789abcdef-0123456789abcdef-00", b.ParentId);
1710+
1711+
using ActivitySource aSource = new ActivitySource("CheckParentTraceFlags");
1712+
using ActivityListener listener = new ActivityListener();
1713+
listener.ShouldListenTo = (activitySource) => object.ReferenceEquals(aSource, activitySource);
1714+
listener.Sample = (ref ActivityCreationOptions<ActivityContext> activityOptions) => ActivitySamplingResult.AllDataAndRecorded;
1715+
ActivitySource.AddActivityListener(listener);
1716+
1717+
ActivityContext parentContext = new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), ActivityTraceFlags.Recorded);
1718+
a = aSource.CreateActivity("WithContext", ActivityKind.Internal, parentContext, default, default, ActivityIdFormat.W3C);
1719+
Assert.NotNull(a);
1720+
Assert.Equal("00-" + parentContext.TraceId + "-" + parentContext.SpanId + "-01", a.ParentId);
1721+
}
1722+
16971723
[Fact]
16981724
public void TestStatus()
16991725
{

0 commit comments

Comments
 (0)