Skip to content

Allow additional Tags for activity creation #50488

Open
@martinjt

Description

@martinjt

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

When doing sampling with Activity/Traces, the information that you have to make the decision on whether to Sample or Drop is only that which is passed in as tags to Start/CreateActivity method. Sampling at the Head (root activity) is the most efficient method as it allows child activitys to inherit that decision and therefore reduce processing. You can sample at an individual (child) activity level too.

Right now, there is very little information to go on when trying to apply Head Sampling in a .NET application. This is because of the Activity being created having no tags being used as part of the Create.

activity = _activitySource.CreateActivity(ActivityName, ActivityKind.Server, string.IsNullOrEmpty(requestId) ? null! : requestId);

This means that the samplers can only base their decision on the parent context.

In order to use other contextual information, you need to use something like the IHttpContextAccessor to access the ambient context.

Describe the solution you'd like

What would make this better from a consumer perspective would be an extension point to augment the activity tags at creation time.

I don't know if Features for HTTP are available to get during the pipeline, however, something like the following

interface IHttpActivityEnrichmentFeature
{
   ActivityTagsCollection OnCreate(HttpContext context);
   void PostCreate(Activity activity, HttpContext context);
}

This would allow users to do that enrichment at the right time. It would allow the level of data being used to be configured according to the performance concerns of the user.

Additional context

No response

Activity

ghost added
needs-area-labelUsed by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically
on Sep 2, 2023
added
api-suggestionEarly API idea and discussion, it is NOT ready for implementation
area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
and removed
needs-area-labelUsed by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically
on Sep 4, 2023
removed
pending-ci-rerunWhen assigned to a PR indicates that the CI checks should be rerun
on Feb 6, 2024
removed
pending-ci-rerunWhen assigned to a PR indicates that the CI checks should be rerun
on Feb 13, 2024
joegoldman2

joegoldman2 commented on Sep 1, 2024

@joegoldman2
Contributor

What do you think about renaming the two methods to ActivityCreating and ActivityCreated?

/// <summary>
/// Defines a feature to enrich activities with additional tags.
/// </summary>
public interface IHttpActivityEnrichmentFeature
{
    /// <summary>
    /// Called during the creation of an activity.
    /// </summary>
    /// <param name="context">The current HTTP context.</param>
    ActivityTagsCollection ActivityCreating(HttpContext context);

    /// <summary>
    /// Called after an activity has been created to perform further enrichment.
    /// </summary>
    /// <param name="activity">The created activity.</param>
    /// <param name="context">The current HTTP context.</param>
    void ActivityCreated(Activity activity, HttpContext context);
}

This convention is already used in some places like https://github.com/dotnet/efcore/blob/main/src/EFCore.Relational/Diagnostics/IDbConnectionInterceptor.cs.

rkargMsft

rkargMsft commented on May 23, 2025

@rkargMsft

Taking a look at this.
Ideally, if something like the host or method were added at Create time then it wouldn't also be added again later after an Activity passes the sampling check but I don't see a clean way to avoid that duplicate work right now.

Maybe that extra work of setting it twice, only for the sampled Activity case, isn't that big of a deal?

rkargMsft

rkargMsft commented on May 23, 2025

@rkargMsft

Adding feature as a hook for users to be able to specify what tags should be added on creation

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-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @martinjt@gfoidl@wtgodbe@joegoldman2@rkargMsft

      Issue actions

        Allow additional Tags for activity creation · Issue #50488 · dotnet/aspnetcore