Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Diagnostics;
using System.Text;
using HotChocolate.Diagnostics;
using HotChocolate.Execution;
using Microsoft.Extensions.ObjectPool;

namespace Digdir.Domain.Dialogporten.GraphQL;

public sealed class DialogportenGqlActivityEnricher(
ObjectPool<StringBuilder> stringBuilderPool,
InstrumentationOptions options) : ActivityEnricher(stringBuilderPool, options)
{
private const string ActivityName = "gql.activity";
private const string HttpRoute = "http.route";

public override void EnrichExecuteRequest(IRequestContext context, Activity activity)
{
base.EnrichExecuteRequest(context, activity);
var rootActivity = GetRootActivity(activity);
rootActivity.SetTag(ActivityName, activity.DisplayName);
}

/// <summary>
/// Renames the root activity's operation name
///
/// Without this method, the name would be "/graphql/{**slug}" for any request, making it harder to differentiate various GQL operations in Application Insights.
/// With this method, the name will be something like "GQL: query { dialogById }"
///
/// This implementation is mostly copied from https://github.com/danhalvorsen/Energy/blob/db4c4e39f90b1f8f9cf074e20241695651cd3af3/src/Cch.MobileApi/Telemetry/CchTelemetryExtensions.cs#L105
/// </summary>
public static void RenameOperationName(Activity activity)
{
var activityName = activity.GetTagItem(ActivityName)?.ToString();
if (activityName == null)
{
return;
}

activity.SetTag(HttpRoute, "GQL: " + activityName);
activity.DisplayName = activityName;
}

private static Activity GetRootActivity(Activity activity)
{
var currentActivity = activity;

while (currentActivity.Parent != null)
{
currentActivity = currentActivity.Parent;
}

return currentActivity;
}
}
9 changes: 8 additions & 1 deletion src/Digdir.Domain.Dialogporten.GraphQL/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using Digdir.Domain.Dialogporten.Application;
Expand Down Expand Up @@ -119,7 +120,13 @@ static void BuildAndRun(string[] args)
.AddSource("Dialogporten.GraphQL")
.AddFusionCacheInstrumentation()
.AddHotChocolateInstrumentation()
.AddAspNetCoreInstrumentationExcludingHealthPaths())
.AddAspNetCoreInstrumentationExcludingHealthPaths(o =>
{
o.EnrichWithHttpResponse = (activity, _) =>
{
DialogportenGqlActivityEnricher.RenameOperationName(activity);
};
}))

// Add health checks with the well-known URLs
.AddAspNetHealthChecks((x, y) => x.HealthCheckSettings.HttpGetEndpointsToCheck = y
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
using Digdir.Domain.Dialogporten.GraphQL.EndUser.DialogById;
using Digdir.Domain.Dialogporten.GraphQL.EndUser.MutationTypes;
using Digdir.Domain.Dialogporten.Infrastructure.Persistence;
using HotChocolate.Diagnostics;

namespace Digdir.Domain.Dialogporten.GraphQL;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddDialogportenGraphQl(this IServiceCollection services) => services
.AddTransient<ActivityEnricher, DialogportenGqlActivityEnricher>()
.AddGraphQLServer()
.AddHttpRequestInterceptor<AcceptLanguageHeaderInterceptor>()
.ModifyCostOptions(o => o.ApplyCostDefaults = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.17.1" />
<PackageReference Include="HotChocolate.Diagnostics" Version="15.1.11" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.23.0"/>
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.11" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="9.0.0"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Hosting;
using Npgsql;
using OpenTelemetry.Exporter;
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
Expand Down Expand Up @@ -172,11 +173,14 @@ private static Action<OpenTelemetrySinkOptions> ConfigureOtlpSink(string endpoin
options.Protocol = protocol;
};

public static TracerProviderBuilder AddAspNetCoreInstrumentationExcludingHealthPaths(this TracerProviderBuilder builder)
public static TracerProviderBuilder AddAspNetCoreInstrumentationExcludingHealthPaths(
this TracerProviderBuilder builder,
Action<AspNetCoreTraceInstrumentationOptions>? configureAspNetCoreTraceInstrumentationOptions = null)
{
return builder.AddAspNetCoreInstrumentation(opts =>
{
opts.RecordException = true;
configureAspNetCoreTraceInstrumentationOptions?.Invoke(opts);
});
}
}