Skip to content

[API Proposal]: ActivityTraceFlags.RandomTraceId flag #124509

Closed

Description

@Kielek

Background and motivation

Needed to comply with new OpenTelemetry specification requirements released as v1.52.0. It was marked as mandatory under open-telemetry/opentelemetry-specification#4761.

RandomTraceId definition is availab;e on W3C page.

It is also needed for open-telemetry/opentelemetry-dotnet#6768

API Proposal

namespace System.Diagnostics;

    [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.

Activity

  1. added
    api-suggestion
    on Feb 17, 2026
  2. added
    untriaged
    on Feb 17, 2026
  3. dotnet-policy-service commented on Feb 17, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @steveisok, @dotnet/area-system-diagnostics-tracing
    See info in area-owners.md if you want to be subscribed.

  4. MihuBot commented on Feb 17, 2026

    @MihuBot
  5. added and removed
    untriaged
    on Feb 17, 2026
  6. dotnet-policy-service commented on Feb 17, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-diagnostics-activity
    See info in area-owners.md if you want to be subscribed.

  7. added this to the 11.0.0 milestone on Feb 17, 2026
  8. tarekgh commented on Feb 17, 2026

    @tarekgh
    Member

    It is interesting OpenTelemetry adapted it while W3C still candidate.

    CC @noahfalk @rajkumar-rangaraj @reyang @CodeBlanch

  9. tarekgh commented on Feb 17, 2026

    @tarekgh
    Member

    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.

  10. tarekgh commented on Feb 17, 2026

    @tarekgh
    Member

    The proposal may need to add the following property to the Activity class

    public bool RandomizedTraceId { get => (ActivityTraceFlags & ActivityTraceFlags.RandomTraceId ) != 0; }
  11. Kielek commented on Feb 24, 2026

    @Kielek
    ContributorAuthor

    @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
         }
  12. 4 remaining items

  13. Kielek commented on Feb 26, 2026

    @Kielek
    ContributorAuthor

    @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.

  14. noahfalk commented on Feb 26, 2026

    @noahfalk
    Member

    Personally I'd probably leave it out, but entirely subjective. If you all would like to have it no complaint from me :)

    Thanks!

  15. tarekgh commented on Feb 26, 2026

    @tarekgh
    Member

    Personally I'd probably leave it out, but entirely subjective. If you all would like to have it no complaint from me :)

    This is something we can discuss in the design review.

  16. added
    api-ready-for-review
    and removed
    api-suggestion
    on Feb 26, 2026
  17. rajkumar-rangaraj commented on Feb 27, 2026

    @rajkumar-rangaraj

    +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!

  18. Kielek commented on Mar 4, 2026

    @Kielek
    ContributorAuthor

    @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; } }
        }
  19. tarekgh commented on Mar 4, 2026

    @tarekgh
    Member

    @tarekgh, could you please help with the formal review process? I think that we have pretty solid conclusion:

    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 😄.

  20. bartonjs commented on Apr 7, 2026

    @bartonjs
    Member

    Video

    We think adding the "Has" prefix to RandomizedTraceId makes it clearer it's not an alternative to TraceId, otherwise looks good as proposed.

    namespace System.Diagnostics
    {
        [Flags]
        public partial enum ActivityTraceFlags
        {
            RandomTraceId = 0b_0_0000010, // 2
        }
    
        public partial class Activity : IDisposable
        {
            public bool HasRandomizedTraceId { get; }
        }
    }
  21. added
    api-approved
    and removed
    api-ready-for-review
    on Apr 7, 2026
  22. added a commit that references this issue on Apr 10, 2026
    cc78323
  23. locked and limited conversation to collaborators on May 10, 2026
  24. added a commit that references this issue on Jul 15, 2026
    fc33fd3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Diagnostics.Activity

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions