Skip to content

ActivityContext.TryParse should support IsRemote #42575

Closed

Description

@cg110

Edited and added the proposal by @tarekgh

Description

When using ActivitySource I wanted to support IsRemote (for opentelemetry), as the majority of the time any activity started with a w3c trace context will be remote.

To start an activity with IsRemote set needs an ActivityContext, the best option appears to be use:
ActivityContext.TryParse(traceParent, traceState, out var context)
However, a context then needs creating to copy the parsed context, and set IsRemote.

It would be useful if TryParse could provide a version that can pass IsRemote into the ActivityContext. I'd expect the majority of new activities that use TryParse are providing a traceparent and state from a remote caller.

Proposal

public readonly struct ActivityContext
{
   public static bool TryParse(string? traceParent, string? traceState, out System.Diagnostics.ActivityContext context)
+  public static bool TryParse(string? traceParent, string? traceState, bool isRemote, out System.Diagnostics.ActivityContext context)
}

Example

    const string w3cId = "00-99d43cb30a4cdb4fbeee3a19c29201b0-e82825765f051b47-01";
    if (TryParseActivityContext(w3cId, "k=v", isRemote: true, out ActivityContext context)) // Parse the context with setting isRemote to true
    {
        // do something with the context
    }

Alternatives

Users can manually add the following helper method but that means every user need this functionality will need to add this method.

    public static bool TryParseActivityContext(string? traceParent, string? traceState, bool isRemote, out ActivityContext context)
    {
        if (ActivityContext.TryParse(traceParent, traceState, out  context))
        {
            context = new ActivityContext(context.TraceId, context.SpanId, context.TraceFlags, context.TraceState, isRemote);
            return true;
        }
        return false;
    }

Activity

  1. added
    untriaged
    on Sep 22, 2020
  2. added
    enhancement
    and removed
    untriaged
    on Sep 22, 2020
  3. added this to the Future milestone on Sep 22, 2020
  4. tarekgh commented on Sep 22, 2020

    @tarekgh
    Member

    However, a context then needs creating to copy the parsed context, and set IsRemote.

    Why this is not enough for you? ActivityContext is a struct and doing that wouldn't cause any real perf issue as it doesn't allocate and the struct size is really small. I don't think it is a good idea to expose a new API just to support this.

    CC @noahfalk

  5. cg110 commented on Sep 23, 2020

    @cg110
    Author

    It's more that it's error prone, and easy to forget

    Really I'd like to do Activity.StartActivity with trace parent, trace source, and isremote, but using activityContext seemed the next easiest alternative, but it needs a dance to get IsRemote set.

  6. tarekgh commented on Sep 23, 2020

    @tarekgh
    Member

    It's more that it's error prone, and easy to forget

    I am not sure I understand this. How this is error prone? You can create your own helper method doing the functionality, it is simple and I wouldn't expect anyone can get it wrong. Easy to forget would be same as if someone manually creating ActivityContext object and forget to pass isRemote as it is optional parameters. Who cares about isRemote would never forget about it.

  7. added
    api-suggestion
    and removed
    enhancement
    on Jan 8, 2021
  8. CodeBlanch commented on Nov 16, 2021

    @CodeBlanch
    Contributor

    API proposal:

    public readonly struct ActivityContext
    {
       public static bool TryParse(
          string? traceParent,
          string? traceState,
          out System.Diagnostics.ActivityContext context,
          bool isRemote = false) // New parameter with default set to current behavior.
       {
       }

    For me it isn't a question of whether the workaround is easy or not, it is a question of how do you know you need the workaround? You don't get immediate feedback that you made a misake. You might not even notice until in production when your sampling is off. If you do notice your sampling is off, it is natural to look at the sampler for issues. Triaging that all the way back to this API is difficult. I think adding the parameter would be a nice hint to users that they should consider whether or not the context belongs to a remote parent when it is created.

  9. tarekgh commented on Nov 16, 2021

    @tarekgh
    Member

    @CodeBlanch thanks for the proposal. We cannot add an extra parameter to the existing method as it will be a breaking change. It is like removing a method. I suggest we add extra overload. Something like:

    public readonly struct ActivityContext
    {
       public static bool TryParse(string? traceParent, string? traceState, out System.Diagnostics.ActivityContext context)
    +  public static bool TryParse(string? traceParent, string? traceState, bool isRemote, out System.Diagnostics.ActivityContext context)
    }

    Note, isRomte is not defaulted parameter as I want to disnguish this overload from the already existing one. The other reason is I want to stick with the TryParse pattern which having the out parameter be last parameter. What do you think?

    CC @noahfalk @cijothomas @reyang

  10. 1 remaining item

  11. tarekgh commented on Nov 17, 2021

    @tarekgh
    Member

    PS: How did you do that diff comment thing? That is neat.

    ```diff
    

    and having + in front of the line means added and will show up in green. having - in front of the line means deleted and will show up in red

  12. self-assigned this
    on Nov 21, 2021
  13. modified the milestones: Future, on Nov 24, 2021
  14. modified the milestones: , 7.0.0 on Nov 25, 2021
  15. added
    api-ready-for-review
    blocking
    and removed
    api-suggestion
    on Jan 8, 2022
  16. bartonjs commented on Jan 11, 2022

    @bartonjs
    Member

    Video

    Looks good as proposed.

    namespace System.Diagnostics
    {
        public readonly partial struct ActivityContext
        {
            // existing
            // public static bool TryParse(string? traceParent, string? traceState, out System.Diagnostics.ActivityContext context);
    
            // new
            public static bool TryParse(
                string? traceParent,
                string? traceState,
                bool isRemote,
                out System.Diagnostics.ActivityContext context);
        }
    }
  17. added
    api-approved
    and removed
    blocking
    api-ready-for-review
    on Jan 11, 2022
  18. ghost locked as resolved and limited conversation to collaborators on Feb 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Diagnostics.Activity

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions