You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[API Proposal]: ActivityTraceFlags.RandomTraceId flag #124509
[Flags]
public enum ActivityTraceFlags
{
None = 0b_0_0000000,
Recorded = 0b_0_0000001,
+ RandomTraceId = 0b_0_0000010, // 2
}
public partial class Activity : IDisposable
{
+ public bool RandomizedTraceId { get { throw null; } }
}
API Usage
N/A
Alternative Designs
No response
Risks
Needed to modify also the way how traceId/flags are created by the Activity class.
Worth to double check if System.Diagnostics.W3CPropagator will be able to handle correctly new flag.
Reacted by Martin Costello, Cijo Thomas, Paulus Pärssinen, Matthew Hensley, Rajkumar Rangaraj, Rasmus Kuusmann and Mateusz Łach
Needed to modify also the way how traceId/flags are created by the Activity class.
This is mostly the work need to be done. Is to ensure setting the flag when generated a random traceId.
Worth to double check if System.Diagnostics.W3CPropagator will be able to handle correctly new flag.
Propagators don't need to change anything. Inject(Activity? activity, object? carrier, PropagatorSetterCallback? setter) takes activity object which should be created correctly, and the random trace flag should be set correctly there after we support the new flag. ExtractTraceIdAndState(object? carrier, PropagatorGetterCallback? getter, out string? traceId, out string? traceState) depends on the propagator consumer (like http handlers) to extract the trace Id correctly and pass it to the propagator.
@tarekgh, I have tried to implement this functionality locally. I think that we need one more additional change to determine if generator is really random:
diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/ref/System.Diagnostics.DiagnosticSourceActivity.cs b/src/libraries/System.Diagnostics.DiagnosticSource/ref/System.Diagnostics.DiagnosticSourceActivity.cs
index 991f4048a9d..6ae59ee17c7 100644
--- a/src/libraries/System.Diagnostics.DiagnosticSource/ref/System.Diagnostics.DiagnosticSourceActivity.cs+++ b/src/libraries/System.Diagnostics.DiagnosticSource/ref/System.Diagnostics.DiagnosticSourceActivity.cs@@ -37,6 +37,7 @@ public string? Id
public string? ParentId { get { throw null; } }
public System.Diagnostics.ActivitySpanId ParentSpanId { get { throw null; } }
public bool Recorded { get { throw null; } }
+ public bool RandomizedTraceId { get { throw null; } }
public string? RootId { get { throw null; } }
public System.Diagnostics.ActivitySpanId SpanId { get { throw null; } }
public System.DateTime StartTimeUtc { get { throw null; } }
@@ -60,6 +61,7 @@ public string? Id
public object? GetTagItem(string key) { throw null; }
public System.Diagnostics.Activity SetEndTime(System.DateTime endTimeUtc) { throw null; }
public static Func<System.Diagnostics.ActivityTraceId>? TraceIdGenerator { get { throw null; } set { } }
+ public static bool? TraceIdRandomnessIndicator { get { throw null; } set { } }
public System.Diagnostics.Activity SetIdFormat(System.Diagnostics.ActivityIdFormat format) { throw null; }
public System.Diagnostics.Activity SetParentId(System.Diagnostics.ActivityTraceId traceId, System.Diagnostics.ActivitySpanId spanId, System.Diagnostics.ActivityTraceFlags activityTraceFlags = System.Diagnostics.ActivityTraceFlags.None) { throw null; }
public System.Diagnostics.Activity SetParentId(string parentId) { throw null; }
@@ -184,6 +186,7 @@ public enum ActivityTraceFlags
{
None = 0,
Recorded = 1,
+ RandomTraceId = 2,
}
public readonly partial struct ActivityTraceId : System.IEquatable<System.Diagnostics.ActivityTraceId>
{
diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs
index cd6da7a53f8..473f4925c1d 100644
--- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs+++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs@@ -939,6 +939,11 @@ public ActivityTraceId TraceId
}
}
+ /// <summary>+ /// True if the W3CIdFlags.RandomTraceId flag is set.+ /// </summary>+ public bool RandomizedTraceId { get => (ActivityTraceFlags & ActivityTraceFlags.RandomTraceId) != 0; }+
/// <summary>
/// True if the W3CIdFlags.Recorded flag is set.
/// </summary>
@@ -1014,6 +1019,19 @@ public ActivitySpanId ParentSpanId
/// </remarks>
public static Func<ActivityTraceId>? TraceIdGenerator { get; set; }
+ /// <summary>+ /// When set alongside <see cref="TraceIdGenerator"/>, indicates whether the custom generator+ /// produces random/unpredictable trace IDs. This controls the <see cref="ActivityTraceFlags.RandomTraceId"/> flag.+ /// </summary>+ /// <remarks>+ /// - If <see cref="TraceIdGenerator"/> is set but <see cref="TraceIdRandomnessIndicator"/> is null,+ /// the <see cref="ActivityTraceFlags.RandomTraceId"/> flag will NOT be set automatically.+ /// - When the default trace ID generator is used (TraceIdGenerator is null), the flag is always set,+ /// regardless of this property's value.+ /// - Set to true if the custom generator produces random IDs, false for deterministic IDs, or null to not set the flag.+ /// </remarks>+ public static bool? TraceIdRandomnessIndicator { get; set; }+
/* static state (configuration) */
/// <summary>
/// Activity tries to use the same format for IDs as its parent.
@@ -1289,8 +1307,27 @@ private void GenerateW3CId()
if (!TrySetTraceIdFromParent())
{
Func<ActivityTraceId>? traceIdGenerator = TraceIdGenerator;
- ActivityTraceId id = traceIdGenerator == null ? ActivityTraceId.CreateRandom() : traceIdGenerator();- _traceId = id.ToHexString();+ if (traceIdGenerator == null)+ {+ // Using default random generator+ ActivityTraceId id = ActivityTraceId.CreateRandom();+ _traceId = id.ToHexString();++ // Set RandomTraceId flag when using the default random generator+ ActivityTraceFlags |= ActivityTraceFlags.RandomTraceId;+ }+ else+ {+ // Using custom generator+ ActivityTraceId id = traceIdGenerator();+ _traceId = id.ToHexString();++ // Check if custom generator indicates randomness+ if (TraceIdRandomnessIndicator == true)+ {+ ActivityTraceFlags |= ActivityTraceFlags.RandomTraceId;+ }+ }
}
}
@@ -1872,6 +1909,7 @@ public enum ActivityTraceFlags
{
None = 0b_0_0000000,
Recorded = 0b_0_0000001, // The Activity (or more likely its parents) has been marked as useful to record
+ RandomTraceId = 0b_0_0000010, // The Activity has a randomized TraceId
}
@noahfalk, it is available for the Recorded flag as
public bool Recorded { get { throw null; } } in the same class. It was added just to keep consistency with existing code. It is not critical addition IMO.
+1 on this proposal. As @Kielek mentioned, the RandomTraceId flag is needed for us to comply with the OTel spec v1.52.0 requirements.
On the open questions:
TraceIdRandomnessIndicator: Agree with @tarekgh's approach to defer this. The default generator covers the primary scenario, and we can revisit if a concrete need arises for custom generators.
RandomizedTraceId property: No strong opinion — happy to go with whatever the design review decides.
The core flag addition and automatically setting it when using ActivityTraceId.CreateRandom() is what we need to unblock the OTel .NET side. Thanks @Kielek for driving this!
Activity
dotnet-policy-service commentedon Feb 17, 2026
Tagging subscribers to this area: @steveisok, @dotnet/area-system-diagnostics-tracing
See info in area-owners.md if you want to be subscribed.
MihuBot commentedon Feb 17, 2026
I'm a bot. Here is a possible duplicate issue (I may be wrong):
dotnet-policy-service commentedon Feb 17, 2026
Tagging subscribers to this area: @dotnet/area-system-diagnostics-activity
See info in area-owners.md if you want to be subscribed.
tarekgh commentedon Feb 17, 2026
It is interesting OpenTelemetry adapted it while W3C still candidate.
CC @noahfalk @rajkumar-rangaraj @reyang @CodeBlanch
tarekgh commentedon Feb 17, 2026
This is mostly the work need to be done. Is to ensure setting the flag when generated a random traceId.
Propagators don't need to change anything.
Inject(Activity? activity, object? carrier, PropagatorSetterCallback? setter)takes activity object which should be created correctly, and the random trace flag should be set correctly there after we support the new flag.ExtractTraceIdAndState(object? carrier, PropagatorGetterCallback? getter, out string? traceId, out string? traceState)depends on the propagator consumer (like http handlers) to extract the trace Id correctly and pass it to the propagator.tarekgh commentedon Feb 17, 2026
The proposal may need to add the following property to the Activity class
Kielek commentedon Feb 24, 2026
@tarekgh, I have tried to implement this functionality locally. I think that we need one more additional change to determine if generator is really random:
4 remaining items
Kielek commentedon Feb 26, 2026
@noahfalk, it is available for the
Recordedflag aspublic bool Recorded { get { throw null; } }in the same class. It was added just to keep consistency with existing code. It is not critical addition IMO.noahfalk commentedon Feb 26, 2026
Personally I'd probably leave it out, but entirely subjective. If you all would like to have it no complaint from me :)
Thanks!
tarekgh commentedon Feb 26, 2026
This is something we can discuss in the design review.
rajkumar-rangaraj commentedon Feb 27, 2026
+1 on this proposal. As @Kielek mentioned, the
RandomTraceIdflag is needed for us to comply with the OTel spec v1.52.0 requirements.On the open questions:
TraceIdRandomnessIndicator: Agree with @tarekgh's approach to defer this. The default generator covers the primary scenario, and we can revisit if a concrete need arises for custom generators.RandomizedTraceIdproperty: No strong opinion — happy to go with whatever the design review decides.The core flag addition and automatically setting it when using
ActivityTraceId.CreateRandom()is what we need to unblock the OTel .NET side. Thanks @Kielek for driving this!Kielek commentedon Mar 4, 2026
@tarekgh, could you please help with the formal review process?
I think that we have pretty solid conclusion:
Mandatory:
[Flags] public enum ActivityTraceFlags { None = 0b_0_0000000, Recorded = 0b_0_0000001, + RandomTraceId = 0b_0_0000010, // 2 }Nice to have:
public partial class Activity : IDisposable { + public bool RandomizedTraceId { get { throw null; } } }tarekgh commentedon Mar 4, 2026
The issue is marked as
api-ready-for-review, so it’s currently queued for review. We review issues in order. You can look at the list here https://github.com/dotnet/runtime/issues?q=is%3Aissue%20state%3Aopen%20label%3Aapi-ready-for-review%20sort%3Acreated-asc%20milestone%3A11.0.0 if you are interested 😄.bartonjs commentedon Apr 7, 2026
Video
We think adding the "Has" prefix to RandomizedTraceId makes it clearer it's not an alternative to TraceId, otherwise looks good as proposed.
Add ActivityTraceFlags.RandomTraceId flag (dotnet#124851)
Add ActivityTraceFlags.RandomTraceId flag (#124851)