From 2cb69a12274af1bef57bd9220426ed4fed23e8e1 Mon Sep 17 00:00:00 2001 From: Are Almaas Date: Thu, 24 Oct 2024 12:29:51 +0200 Subject: [PATCH 1/3] chore(web-api): separate the telemetry-configuration into aspnet package --- .../WebApplicationBuilderExtensions.cs | 49 ------------------- .../Digdir.Domain.Dialogporten.WebApi.csproj | 5 -- .../AspNetUtilitiesExtensions.cs | 49 ++++++++++++++++++- .../Digdir.Library.Utils.AspNet.csproj | 5 ++ 4 files changed, 52 insertions(+), 56 deletions(-) delete mode 100644 src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs b/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs deleted file mode 100644 index 88b734912..000000000 --- a/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs +++ /dev/null @@ -1,49 +0,0 @@ -using Azure.Monitor.OpenTelemetry.AspNetCore; -using OpenTelemetry.Trace; -using Npgsql; -using OpenTelemetry.Metrics; -using OpenTelemetry.Resources; - -namespace Digdir.Domain.Dialogporten.WebApi.Common.Extensions; - -internal static class WebApplicationBuilderExtensions -{ - public static WebApplicationBuilder ConfigureTelemetry(this WebApplicationBuilder builder) - { - builder.Services.AddOpenTelemetry() - .ConfigureResource(resource => resource - .AddService(serviceName: builder.Environment.ApplicationName)) - .WithTracing(tracing => - { - if (builder.Environment.IsDevelopment()) - { - tracing.SetSampler(new AlwaysOnSampler()); - } - - tracing.AddAspNetCoreInstrumentation(options => - { - options.Filter = (httpContext) => - !httpContext.Request.Path.StartsWithSegments("/health"); - }); - - tracing.AddHttpClientInstrumentation(); - tracing.AddNpgsql(); - }) - .WithMetrics(metrics => - { - metrics.AddRuntimeInstrumentation(); - }); - - if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"))) - { - builder.Services.AddOpenTelemetry().UseAzureMonitor(); - } - else - { - // Use Application Insights SDK for local development - builder.Services.AddApplicationInsightsTelemetry(); - } - - return builder; - } -} diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj b/src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj index 31715419d..e84cc903e 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj +++ b/src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj @@ -8,17 +8,12 @@ - - - - - diff --git a/src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs b/src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs index e5e4d0b4c..dd108c44b 100644 --- a/src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs +++ b/src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs @@ -1,9 +1,15 @@ +using Azure.Monitor.OpenTelemetry.AspNetCore; using Digdir.Library.Utils.AspNet.HealthChecks; using HealthChecks.UI.Client; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Hosting; +using Npgsql; +using OpenTelemetry.Trace; +using OpenTelemetry.Metrics; +using OpenTelemetry.Resources; namespace Digdir.Library.Utils.AspNet; @@ -40,4 +46,43 @@ private static WebApplication MapHealthCheckEndpoint(this WebApplication app, st app.MapHealthChecks(path, new HealthCheckOptions { Predicate = predicate, ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse }); return app; } + + public static WebApplicationBuilder ConfigureTelemetry(this WebApplicationBuilder builder) + { + builder.Services.AddOpenTelemetry() + .ConfigureResource(resource => resource + .AddService(serviceName: builder.Environment.ApplicationName)) + .WithTracing(tracing => + { + if (builder.Environment.IsDevelopment()) + { + tracing.SetSampler(new AlwaysOnSampler()); + } + + tracing.AddAspNetCoreInstrumentation(options => + { + options.Filter = (httpContext) => + !httpContext.Request.Path.StartsWithSegments("/health"); + }); + + tracing.AddHttpClientInstrumentation(); + tracing.AddNpgsql(); + }) + .WithMetrics(metrics => + { + metrics.AddRuntimeInstrumentation(); + }); + + if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"))) + { + builder.Services.AddOpenTelemetry().UseAzureMonitor(); + } + else + { + // Use Application Insights SDK for local development + builder.Services.AddApplicationInsightsTelemetry(); + } + + return builder; + } } diff --git a/src/Digdir.Library.Utils.AspNet/Digdir.Library.Utils.AspNet.csproj b/src/Digdir.Library.Utils.AspNet/Digdir.Library.Utils.AspNet.csproj index 3695da595..97901c576 100644 --- a/src/Digdir.Library.Utils.AspNet/Digdir.Library.Utils.AspNet.csproj +++ b/src/Digdir.Library.Utils.AspNet/Digdir.Library.Utils.AspNet.csproj @@ -7,9 +7,14 @@ + + + + + From 06e92869eb6b2b039d394f06f2b54a5d66e7f249 Mon Sep 17 00:00:00 2001 From: Are Almaas Date: Thu, 24 Oct 2024 13:00:17 +0200 Subject: [PATCH 2/3] feat(graphql): configure opentelemetry --- .../Program.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Digdir.Domain.Dialogporten.GraphQL/Program.cs b/src/Digdir.Domain.Dialogporten.GraphQL/Program.cs index 0ef1152e0..6e1ba1767 100644 --- a/src/Digdir.Domain.Dialogporten.GraphQL/Program.cs +++ b/src/Digdir.Domain.Dialogporten.GraphQL/Program.cs @@ -18,18 +18,18 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Options; +// Using two-stage initialization to catch startup errors. +var telemetryConfiguration = TelemetryConfiguration.CreateDefault(); Log.Logger = new LoggerConfiguration() .MinimumLevel.Warning() .Enrich.FromLogContext() .WriteTo.Console(formatProvider: CultureInfo.InvariantCulture) - .WriteTo.ApplicationInsights( - TelemetryConfiguration.CreateDefault(), - TelemetryConverter.Traces) + .WriteTo.ApplicationInsights(telemetryConfiguration, TelemetryConverter.Traces) .CreateBootstrapLogger(); try { - BuildAndRun(args); + BuildAndRun(args, telemetryConfiguration); } catch (Exception ex) when (ex is not OperationCanceledException) { @@ -41,7 +41,7 @@ Log.CloseAndFlush(); } -static void BuildAndRun(string[] args) +static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfiguration) { var builder = WebApplication.CreateBuilder(args); @@ -51,9 +51,7 @@ static void BuildAndRun(string[] args) .ReadFrom.Services(services) .Enrich.FromLogContext() .WriteTo.Console(formatProvider: CultureInfo.InvariantCulture) - .WriteTo.ApplicationInsights( - services.GetRequiredService(), - TelemetryConverter.Traces)); + .WriteTo.ApplicationInsights(telemetryConfiguration, TelemetryConverter.Traces)); builder.Configuration .AddAzureConfiguration(builder.Environment.EnvironmentName) @@ -67,6 +65,8 @@ static void BuildAndRun(string[] args) var thisAssembly = Assembly.GetExecutingAssembly(); + builder.ConfigureTelemetry(); + builder.Services // Options setup .ConfigureOptions() From 489302f3405ba28324fb408a37028e4928908580 Mon Sep 17 00:00:00 2001 From: Knut Haug Date: Thu, 24 Oct 2024 15:41:21 +0200 Subject: [PATCH 3/3] remove Application Insights SDK to avoid potential duplicate telemetry --- src/Digdir.Domain.Dialogporten.GraphQL/Program.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Digdir.Domain.Dialogporten.GraphQL/Program.cs b/src/Digdir.Domain.Dialogporten.GraphQL/Program.cs index 6e1ba1767..8c191dfc3 100644 --- a/src/Digdir.Domain.Dialogporten.GraphQL/Program.cs +++ b/src/Digdir.Domain.Dialogporten.GraphQL/Program.cs @@ -77,7 +77,6 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura .WithPubCapabilities() .Build() .AddAutoMapper(Assembly.GetExecutingAssembly()) - .AddApplicationInsightsTelemetry() .AddScoped() .AddValidatorsFromAssembly(thisAssembly, ServiceLifetime.Transient, includeInternalTypes: true) .AddAzureAppConfiguration()