Skip to content

Feature for setting activity tags on creation #62090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

rkargMsft
Copy link

@rkargMsft rkargMsft commented May 23, 2025

Feature for adding tags at Activity creation time

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Summary of the changes

Adding a feature so that implementations can specify tags to add at Activity creation. This is to support using those tags when making sampling decisions as only tags provided in the CreateActivity/StartActivity are available at that time.

Description

Implementations of IHttpActivityCreationTagsFeature can determine what tags are desired to be used for sampling. It's important that this is an opt-in where the user specifies the tags to add since this work will get done for every Activity created for an incoming request and not just for the ones that eventually pass the sampling filter.

Example implementation:

public class BasicHttpActivityCreationTagsFeature : IHttpActivityCreationTagsFeature
{
    private readonly HttpContext _context;

    public BasicHttpActivityCreationTagsFeature(HttpContext context)
    {
        ArgumentNullException.ThrowIfNull(context);
        _context = context;
    }

    public IEnumerable<KeyValuePair<string, object?>>? ActivityCreationTags
    {
        get
        {
            if (_context?.Request is null)
            {
                return null;
            }

            var tags = new TagList();
            if (_context.Request.Host.HasValue)
            {
                tags.Add("server.address", _context.Request.Host.Host);
                if (_context.Request.Host.Port is not null)
                {
                    tags.Add("server.port", _context.Request.Host.Port);
                }
            }
            tags.Add("http.request.method", _context.Request.Method);
            tags.Add("url.scheme", _context.Request.Scheme);
            return tags;
        }
    }
}

Fixes #50488

@github-actions github-actions bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label May 23, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label May 23, 2025
@BrennanConroy BrennanConroy requested a review from JamesNK May 23, 2025 19:20
@rkargMsft
Copy link
Author

It may make sense to actually have the ActivityCreationTags property be IEnumerable<KeyValuePair<string, object?>>? which is what ActivityCreator takes as a parameter and then let the implementation decide what to return.

public static Activity? CreateFromRemote(
ActivitySource activitySource,
DistributedContextPropagator propagator,
object distributedContextCarrier,
DistributedContextPropagator.PropagatorGetterCallback propagatorGetter,
string activityName,
ActivityKind kind,
IEnumerable<KeyValuePair<string, object?>>? tags,
IEnumerable<ActivityLink>? links,
bool diagnosticsOrLoggingEnabled)

also fixed up unshipped API documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow additional Tags for activity creation
2 participants