Skip to content

Allow setting the Activity properties after Activity creation and before starting it when using ActivitySource #42784

Closed
@macrogreg

Description

@macrogreg

The description and the Proposal edited by @tarekgh. The original description is moved down in the comment #42784 (comment)

ActivitySource provides the method StartActivity which will try to create and start the Activity object. There is a scenario that needs to add some more properties to the Activity object after it is created and before it gets started.
The reason is the caller wants to avoid creating the objects need to set to the Activity before ensuring the Activity object is created. It is important to have these properties set before starting the Activity so the listeners of the Start event will get the full set of the data.

Proposal

namespace System.Diagnostics
{
    public sealed class ActivitySource : IDisposable
    {
        // Alternative proposed name is CreateUnstartedActivity
        public Activity? CreateActivity(
                             string name,
                             ActivityKind kind, 
                             ActivityContext parentContext,
                             IEnumerable<KeyValuePair<string, object?>>? tags = null, 
                             IEnumerable<ActivityLink>? links = null);

        public Activity? CreateActivity(
                             string name, 
                             ActivityKind kind,
                             string parentContext,
                             IEnumerable<KeyValuePair<string, object?>>? tags = null, 
                             IEnumerable<ActivityLink>? links = null);
    }
}

Example

    using (Activity activity = activitySource.CreateActivity("SomeName", ActivityKind.Server, context))
    {
        activity?.AddBaggage("db.type", "mongo");
        activity?.Start();

        // Do any necessary work      
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions