Skip to content

Commit

Permalink
Add ActivityContext.TryParse overload (#63692)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekgh authored Jan 13, 2022
1 parent dc67c70 commit 93c6134
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public readonly struct ActivityEvent
public string? TraceState { get { throw null; } }
public bool IsRemote { get { throw null; } }
public static bool TryParse(string? traceParent, string? traceState, out System.Diagnostics.ActivityContext context) { throw null; }
public static bool TryParse(string? traceParent, string? traceState, bool isRemote, out System.Diagnostics.ActivityContext context) { throw null; }
public static System.Diagnostics.ActivityContext Parse(string traceParent, string? traceState) { throw null; }
public static bool operator ==(System.Diagnostics.ActivityContext left, System.Diagnostics.ActivityContext right) { throw null; }
public static bool operator !=(System.Diagnostics.ActivityContext left, System.Diagnostics.ActivityContext right) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ private static bool IsW3CId(string id)
#if ALLOW_PARTIALLY_TRUSTED_CALLERS
[System.Security.SecuritySafeCriticalAttribute]
#endif
internal static bool TryConvertIdToContext(string traceParent, string? traceState, out ActivityContext context)
internal static bool TryConvertIdToContext(string traceParent, string? traceState, bool isRemote, out ActivityContext context)
{
context = default;
if (!IsW3CId(traceParent))
Expand All @@ -941,7 +941,8 @@ internal static bool TryConvertIdToContext(string traceParent, string? traceStat
new ActivityTraceId(traceIdSpan.ToString()),
new ActivitySpanId(spanIdSpan.ToString()),
(ActivityTraceFlags) ActivityTraceId.HexByteFromChars(traceParent[53], traceParent[54]),
traceState);
traceState,
isRemote);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,27 @@ public ActivityContext(ActivityTraceId traceId, ActivitySpanId spanId, ActivityT
/// </summary>
/// <param name="traceParent">W3C trace parent header.</param>
/// <param name="traceState">W3C trace state.</param>
/// <param name="isRemote">Indicate the context is propagated from remote parent.</param>
/// <param name="context">The ActivityContext object created from the parsing operation.</param>
public static bool TryParse(string? traceParent, string? traceState, out ActivityContext context)
public static bool TryParse(string? traceParent, string? traceState, bool isRemote, out ActivityContext context)
{
if (traceParent is null)
{
context = default;
return false;
}

return Activity.TryConvertIdToContext(traceParent, traceState, out context);
return Activity.TryConvertIdToContext(traceParent, traceState, isRemote, out context);
}

/// <summary>
/// Parse W3C trace context headers to ActivityContext object.
/// </summary>
/// <param name="traceParent">W3C trace parent header.</param>
/// <param name="traceState">W3C trace state.</param>
/// <param name="context">The ActivityContext object created from the parsing operation.</param>
public static bool TryParse(string? traceParent, string? traceState, out ActivityContext context) => TryParse(traceParent, traceState, isRemote: false, out context);

/// <summary>
/// Parse W3C trace context headers to ActivityContext object.
/// </summary>
Expand All @@ -92,7 +101,7 @@ public static ActivityContext Parse(string traceParent, string? traceState)
throw new ArgumentNullException(nameof(traceParent));
}

if (!Activity.TryConvertIdToContext(traceParent, traceState, out ActivityContext context))
if (!Activity.TryConvertIdToContext(traceParent, traceState, isRemote: false, out ActivityContext context))
{
throw new ArgumentException(SR.InvalidTraceParent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static void GetRootId(out string? parentId, out string? traceState, out

traceState = activity?.TraceStateString;
parentId = activity?.ParentId ?? activity?.Id;
isW3c = parentId is not null ? Activity.TryConvertIdToContext(parentId, traceState, out _) : false;
isW3c = parentId is not null ? Activity.TryConvertIdToContext(parentId, traceState, isRemote: false, out _) : false;
baggage = activity?.Baggage;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,22 +470,41 @@ public void TestActivityContextParsing()
Assert.Equal("e82825765f051b47", context.SpanId.ToHexString());
Assert.Equal(ActivityTraceFlags.Recorded, context.TraceFlags);
Assert.Equal("k=v", context.TraceState);
Assert.False(context.IsRemote);

Assert.True(ActivityContext.TryParse(w3cId, "k=v", isRemote: true, out context));
Assert.Equal("99d43cb30a4cdb4fbeee3a19c29201b0", context.TraceId.ToHexString());
Assert.Equal("e82825765f051b47", context.SpanId.ToHexString());
Assert.Equal(ActivityTraceFlags.Recorded, context.TraceFlags);
Assert.Equal("k=v", context.TraceState);
Assert.True(context.IsRemote);

Assert.True(ActivityContext.TryParse(w3cId, "k=v", isRemote: false, out context));
Assert.Equal("99d43cb30a4cdb4fbeee3a19c29201b0", context.TraceId.ToHexString());
Assert.Equal("e82825765f051b47", context.SpanId.ToHexString());
Assert.Equal(ActivityTraceFlags.Recorded, context.TraceFlags);
Assert.Equal("k=v", context.TraceState);
Assert.False(context.IsRemote);

context = ActivityContext.Parse(w3cId, "k=v");
Assert.Equal("99d43cb30a4cdb4fbeee3a19c29201b0", context.TraceId.ToHexString());
Assert.Equal("e82825765f051b47", context.SpanId.ToHexString());
Assert.Equal(ActivityTraceFlags.Recorded, context.TraceFlags);
Assert.Equal("k=v", context.TraceState);
Assert.False(context.IsRemote);

context = ActivityContext.Parse(w3cId, null);
Assert.Null(context.TraceState);
Assert.False(context.IsRemote);

Assert.False(ActivityContext.TryParse(null, "k=v", out context));
Assert.Throws<ArgumentNullException>(() => ActivityContext.Parse(null, null));
Assert.Throws<ArgumentException>(() => ActivityContext.Parse("BadW3C", null));

const string invalidW3CContext = "00-Z9d43cb30a4cdb4fbeee3a19c29201b0-e82825765f051b47-01";
Assert.False(ActivityContext.TryParse(invalidW3CContext, null, out context));
Assert.False(ActivityContext.TryParse(invalidW3CContext, null, isRemote: true, out context));
Assert.False(ActivityContext.TryParse(invalidW3CContext, null, isRemote: false, out context));
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
Expand Down

0 comments on commit 93c6134

Please sign in to comment.