Skip to content

[API Proposal]: Activity.IsStopped (functionality required to conform to OpenTelemetry specification) #63353

Closed

Description

@Oberon00

Background and motivation

As far as I know, the Activity class is meant to provide all the functionality required by the OpenTelemetry tracing specification for the concept that is called "Span" in OTel terms.

However, I could not find a way to get the following from Activity:

It must also be able to reliably determine whether the Span has ended (some languages might implement this by having an end timestamp of null, others might have an explicit hasEnded boolean).

-- https://github.com/open-telemetry/opentelemetry-specification/blob/v1.8.0/specification/trace/sdk.md#additional-span-interfaces

At first, I thought one could check if Activity.Duration != TimeSpan.Zero, but while this is indeed true for all stopped/ended activities, even activities that are still running can have a non-zero duration if SetEndTime was called but Stop/Dispose was not.

In some situations, one could could use the callback from the Source and maintain a ConditionalWeakTable to record whether an Activity has stopped, but this is rather cumbersome, slow and not possible if you do not have control over the activities' source.

It seems that Activity already has this implemented as private functinality as a property IsFinished: https://github.com/dotnet/runtime/blob/v6.0.1/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs#L1301-L1303

Exposing the existing name IsFinished would also be an acceptable IMHO (I think the most OTel-like name would be HasEnded, but since End is called Stop, my primary suggestion is IsStopped).

The interface in OpenTelemetry Java is here https://github.com/open-telemetry/opentelemetry-java/blob/v1.9.1/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/ReadableSpan.java#L64-L71 and here https://github.com/open-telemetry/opentelemetry-java/blob/v1.9.1/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/data/SpanData.java#L117-L122

API Proposal

namespace System.Diagnostics
{
    public class Activity: IDisposable
    {
        // ...
        public bool IsStopped { get; }
        // ...
    }
}

API Usage

var a = new Activity("foo");
Debug.Assert(!a.IsStopped);
a.Start();
Debug.Assert(!a.IsStopped);
a.Stop();
Debug.Assert(a.IsStopped);

In an ActivityListener.ActivityStopped callback, IsStopped of the notifying Activity must also be true to conform to OTel semantics.

Alternative Designs

If SetEndTime would always unconditionally Stop the activity, the check for nonzero Duration could be used instead of using a separate property. However, that would probably be an unacceptable breaking change. Note: The XML doc of Duration claims that calling SetEndTime ends the activity (is ending the same as stopping?) but unless Duration != Zero does logically end the activity (without invoking a listener), this does not seem to be true.

Alternative Workaround

OpenTelemetry SDK is already listening to Activity Start and Stop events, it can manually either create a weak reference table mapping the Activity object to its wrapper span object and then mark the span ended when receiving Stop event. Another idea is to set some property/tag value on the Activity when receiving the stop event. These are not clean solutions considering the simplicity we'll get if we added the proposed API.

Risks

None known. This proposal would add a new property exposing existing functionality that is required as an API surface by the OpenTelemetry specification.

Activity

  1. added
    api-suggestion
    on Jan 4, 2022
  2. ghost added
    untriaged
    on Jan 4, 2022
  3. removed
    untriaged
    on Jan 4, 2022
  4. added this to the 7.0.0 milestone on Jan 4, 2022
  5. tarekgh commented on Jan 4, 2022

    @tarekgh
    Member

    The proposal looks reasonable to me.

    CC @noahfalk @cijothomas @reyang

  6. noahfalk commented on Jan 4, 2022

    @noahfalk
    Member

    Yeah, this looks fine to me too.

  7. reyang commented on Jan 5, 2022

    @reyang
    Contributor

    Is this considered part of the API or SDK? (this is part of the SDK spec).

  8. tarekgh commented on Jan 5, 2022

    @tarekgh
    Member

    Looks it is part of the SDK but how the SDK can implement that without having Activity provide this functionality?

  9. reyang commented on Jan 5, 2022

    @reyang
    Contributor

    Looks it is part of the SDK but how the SDK can implement that without having Activity provide this functionality?

    What I can think of (not necessarily against the proposal, just to call out that there might be many different solutions):

    1. The SDK has full access to start/stop event, so it could do something.
    2. The Activity API can expose something totally different (e.g. the end timestamp) and still allow the SDK to tell if an activity is stopped.
  10. Oberon00 commented on Jan 5, 2022

    @Oberon00
    Author

    The end timestamp is already exposed, but can be set without stopping the Activity (see the issue description).

    The SDK might be able to solve this with events, but it then SHOULD ensure that users of the SDK have a way to tell if a Activity they received in SpanProcessor.OnStart is stopped at any later point in time (https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#onstart "It SHOULD be possible to keep a reference to this span object and updates to the span SHOULD be reflected in it."). As stated in the issue description, this would probably require a ConditionalWeakTable.

  11. 1 remaining item

  12. tarekgh commented on Jan 8, 2022

    @tarekgh
    Member

    If there is no more feedback, can we move forward with the proposal? @reyang any more feedback? Thanks!

  13. reyang commented on Jan 8, 2022

    @reyang
    Contributor

    @reyang any more feedback?

    Nope. I guess it is possible to implement this efficiently in the SDK without having to introduce a new API to the runtime. But I don't see adding a new runtime API as bad idea if there is good reason and use case.

  14. tarekgh commented on Jan 8, 2022

    @tarekgh
    Member

    @Oberon00 I have added a small section Alternative Workaround in the description to capture @reyang feedback. Feel free to edit it as needed.

  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 partial class Activity
        {
            public bool IsStopped { get; }
        }
    }
  17. added
    api-approved
    and removed
    api-ready-for-review
    blocking
    on Jan 11, 2022
  18. self-assigned this
    on Jan 12, 2022
  19. ghost added
    in-pr
    on Jan 12, 2022
  20. ghost removed
    in-pr
    on Jan 14, 2022
  21. ghost locked as resolved and limited conversation to collaborators on Feb 13, 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