asp.netgraphqlhotchocolateelastic-apmobservability

How to change root activity name in .NET HotChocolate for Elastic APM using a CustomActivityEnricher?


I am trying to change the root activity name in .NET HotChocolate for Elastic APM by creating a custom activity enricher and overriding the CreateRootActivityName method.

public class CustomActivityEnricher : ActivityEnricher
{
    public CustomActivityEnricher(ObjectPool<StringBuilder> stringBuilderPoolPool, InstrumentationOptions options): 
                            base(stringBuilderPoolPool, options)
    {

    }

    protected override string CreateRootActivityName(Activity activity, Activity root, string operationDisplayName)
    {
        return operationDisplayName;
    }
}

I added it as a Singleton service, as described in the documentation of HotChocolate and in this video demonstration:

Instrumentation

GraphQL Observability with Elastic and OpenTelemetry - Michael Staib

services.AddSingleton<ActivityEnricher, CustomActivityEnricher>();

But this doesn't seem to work for me. Instead of getting the name of the query I am running, I get the endpoint of my GraphQL server.

enter image description here

Edit: This is the configuration of my GraphQL server:

services.AddGraphQLServer(schema.Organization.GetGraphId())
                ...
                .AddInstrumentation(o =>
                {
                    o.RenameRootActivity = true;
                    o.IncludeDocument = true;
                });

Solution

  • We solved this by assigning a new name to the APM transaction when diplayName contains '{'

    protected override string CreateRootActivityName(Activity activity, Activity root, string displayName)
    {
        var baseName = base.CreateRootActivityName(activity, root, displayName);
        
        var trans = Agent.Tracer.CurrentTransaction;
        
        if (displayName.Contains('{'))
        {
            trans.Name = displayName;
        }
        
        return baseName;
    }