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 @@ + + + + +