From c0f580b1e2e262641a96ee462be3ad20c70c6257 Mon Sep 17 00:00:00 2001 From: Knut Haug Date: Fri, 27 Sep 2024 14:17:40 +0200 Subject: [PATCH 01/14] initial setup of OpenTelemetry for WebApi --- .../InfrastructureExtensions.cs | 16 ++++++++- .../Digdir.Domain.Dialogporten.WebApi.csproj | 8 +++++ .../Program.cs | 36 ++++++++++++++++--- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs b/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs index 0b40eff71..ed1940fbd 100644 --- a/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs +++ b/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs @@ -68,8 +68,8 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi services.AddFusionCacheNeueccMessagePackSerializer(); + services.ConfigureRedisCache(infrastructureSettings); - services.AddStackExchangeRedisCache(opt => opt.Configuration = infrastructureSettings.Redis.ConnectionString); services.AddFusionCacheStackExchangeRedisBackplane(opt => opt.Configuration = infrastructureSettings.Redis.ConnectionString); services.AddGraphQlRedisSubscriptions(infrastructureSettings.Redis.ConnectionString); @@ -278,4 +278,18 @@ private static IServiceCollection ConfigureFusionCache(this IServiceCollection s return services; } + + private static IServiceCollection ConfigureRedisCache(this IServiceCollection services, + InfrastructureSettings infrastructureSettings) + { + var redisConnection = ConnectionMultiplexer.Connect(infrastructureSettings.Redis.ConnectionString); + services.AddSingleton(redisConnection); + + services.AddStackExchangeRedisCache(options => + { + options.ConnectionMultiplexerFactory = () => Task.FromResult(redisConnection); + }); + + return services; + } } 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 1fc55ede1..28302e96d 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj +++ b/src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj @@ -8,12 +8,20 @@ + + + + + + + + diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs index 0ff2a5188..3d80b9415 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs @@ -3,6 +3,7 @@ using System.Reflection; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; +using Azure.Monitor.OpenTelemetry.AspNetCore; using Digdir.Domain.Dialogporten.Application; using Digdir.Domain.Dialogporten.Application.Common.Extensions; using Digdir.Domain.Dialogporten.Application.Common.Extensions.OptionExtensions; @@ -19,9 +20,12 @@ using FastEndpoints; using FastEndpoints.Swagger; using FluentValidation; -using Microsoft.ApplicationInsights.Extensibility; using Microsoft.AspNetCore.Authorization; +using Npgsql; using NSwag; +using OpenTelemetry.Resources; +using OpenTelemetry.Trace; +using OpenTelemetry.Metrics; using Serilog; // Using two-stage initialization to catch startup errors. @@ -30,7 +34,6 @@ .Enrich.FromLogContext() .WriteTo.Console(formatProvider: CultureInfo.InvariantCulture) .WriteTo.ApplicationInsights( - TelemetryConfiguration.CreateDefault(), TelemetryConverter.Traces) .CreateBootstrapLogger(); @@ -58,7 +61,6 @@ static void BuildAndRun(string[] args) .ReadFrom.Services(services) .Enrich.FromLogContext() .WriteTo.ApplicationInsights( - services.GetRequiredService(), TelemetryConverter.Traces)); builder.Configuration @@ -98,7 +100,6 @@ static void BuildAndRun(string[] args) .AddHttpContextAccessor() .AddValidatorsFromAssembly(thisAssembly, ServiceLifetime.Transient, includeInternalTypes: true) .AddAzureAppConfiguration() - .AddApplicationInsightsTelemetry() .AddEndpointsApiExplorer() .AddFastEndpoints() .SwaggerDocument(x => @@ -126,6 +127,33 @@ static void BuildAndRun(string[] args) .AddNewtonsoftJson() .Services + // OpenTelemetry + .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("/healthz"); + }); + + tracing.AddHttpClientInstrumentation(); + tracing.AddNpgsql(); + tracing.AddRedisInstrumentation(options => options.SetVerboseDatabaseStatements = true); + }) + .WithMetrics(metrics => + { + metrics.AddRuntimeInstrumentation(); + }) + .UseAzureMonitor() + .Services // Auth .AddDialogportenAuthentication(builder.Configuration) From fb19b3d4a85ed8d9816b72a9181ab9462686ef35 Mon Sep 17 00:00:00 2001 From: Knut Haug Date: Fri, 27 Sep 2024 17:09:04 +0200 Subject: [PATCH 02/14] --wip-- [skip-ci] --- docs/schema/V1/swagger.verified.json | 2 +- .../InfrastructureExtensions.cs | 19 ++++--------------- .../Program.cs | 17 ++++++++++------- .../appsettings.Development.json | 14 +++++++------- .../Features/V1/SwaggerSnapshotTests.cs | 4 ++-- 5 files changed, 24 insertions(+), 32 deletions(-) diff --git a/docs/schema/V1/swagger.verified.json b/docs/schema/V1/swagger.verified.json index 4f86d1c70..604c02988 100644 --- a/docs/schema/V1/swagger.verified.json +++ b/docs/schema/V1/swagger.verified.json @@ -6174,7 +6174,7 @@ }, "servers": [ { - "url": "https://altinn-dev-api.azure-api.net/dialogporten" + "url": "https://localhost:7214/" } ] } diff --git a/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs b/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs index ed1940fbd..6f7a86b01 100644 --- a/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs +++ b/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs @@ -68,7 +68,10 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi services.AddFusionCacheNeueccMessagePackSerializer(); - services.ConfigureRedisCache(infrastructureSettings); + services.AddStackExchangeRedisCache(options => + options.ConnectionMultiplexerFactory = () => Task.FromResult( + ConnectionMultiplexer.Connect(infrastructureSettings.Redis.ConnectionString) + )); services.AddFusionCacheStackExchangeRedisBackplane(opt => opt.Configuration = infrastructureSettings.Redis.ConnectionString); @@ -278,18 +281,4 @@ private static IServiceCollection ConfigureFusionCache(this IServiceCollection s return services; } - - private static IServiceCollection ConfigureRedisCache(this IServiceCollection services, - InfrastructureSettings infrastructureSettings) - { - var redisConnection = ConnectionMultiplexer.Connect(infrastructureSettings.Redis.ConnectionString); - services.AddSingleton(redisConnection); - - services.AddStackExchangeRedisCache(options => - { - options.ConnectionMultiplexerFactory = () => Task.FromResult(redisConnection); - }); - - return services; - } } diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs index 3d80b9415..88b07e527 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs @@ -151,14 +151,17 @@ static void BuildAndRun(string[] args) .WithMetrics(metrics => { metrics.AddRuntimeInstrumentation(); - }) - .UseAzureMonitor() - .Services + }); + + if (!builder.Environment.IsDevelopment()) + { + builder.Services.AddOpenTelemetry().UseAzureMonitor(); + } - // Auth - .AddDialogportenAuthentication(builder.Configuration) - .AddAuthorization() - .AddHealthChecks(); + // Auth + builder.Services.AddDialogportenAuthentication(builder.Configuration) + .AddAuthorization() + .AddHealthChecks(); if (builder.Environment.IsDevelopment()) { diff --git a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json index e1067c8d2..c44ec303c 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json +++ b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json @@ -66,15 +66,15 @@ } }, "LocalDevelopment": { - "UseLocalDevelopmentUser": true, - "UseLocalDevelopmentResourceRegister": true, - "UseLocalDevelopmentOrganizationRegister": true, - "UseLocalDevelopmentNameRegister": true, - "UseLocalDevelopmentAltinnAuthorization": true, + "UseLocalDevelopmentUser": false, + "UseLocalDevelopmentResourceRegister": false, + "UseLocalDevelopmentOrganizationRegister": false, + "UseLocalDevelopmentNameRegister": false, + "UseLocalDevelopmentAltinnAuthorization": false, "UseLocalDevelopmentCloudEventBus": true, "UseLocalDevelopmentCompactJwsGenerator": true, "DisableShortCircuitOutboxDispatcher": true, - "DisableCache": true, - "DisableAuth": true + "DisableCache": false, + "DisableAuth": false } } diff --git a/tests/Digdir.Domain.Dialogporten.WebApi.Integration.Tests/Features/V1/SwaggerSnapshotTests.cs b/tests/Digdir.Domain.Dialogporten.WebApi.Integration.Tests/Features/V1/SwaggerSnapshotTests.cs index ed2d01235..ef0da0ad6 100644 --- a/tests/Digdir.Domain.Dialogporten.WebApi.Integration.Tests/Features/V1/SwaggerSnapshotTests.cs +++ b/tests/Digdir.Domain.Dialogporten.WebApi.Integration.Tests/Features/V1/SwaggerSnapshotTests.cs @@ -26,7 +26,7 @@ public async Task FailIfSwaggerSnapshotDoesNotMatch() var swaggerPath = Path.Combine(rootPath!, "docs/schema/V1"); var client = _webApplicationFactory - .WithWebHostBuilder(builder => builder.UseEnvironment("test")) + .WithWebHostBuilder(builder => builder.UseEnvironment("Development")) .CreateClient(); // Act @@ -35,7 +35,7 @@ public async Task FailIfSwaggerSnapshotDoesNotMatch() // The order of the properties in the swagger.json file is not cross-platform deterministic. // Running client.GetAsync("/swagger/v1/swagger.json"); on Windows and Mac will produce // different ordering of the results (although the content is the same). So we force an - // alphabetical ordering of the properties to make the test deterministic. + // alphabetical ordering of the properties to make the test deterministic. // Ref: https://github.com/digdir/dialogporten/issues/996 var orderedSwagger = SortJson(newSwagger); From ab663995f84b7619733a941d438b32548ada8797 Mon Sep 17 00:00:00 2001 From: Knut Haug Date: Fri, 11 Oct 2024 13:16:50 +0200 Subject: [PATCH 03/14] --wip-- --- .../Program.cs | 23 ++++++++----------- .../appsettings.Development.json | 3 +++ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs index 0c6d4bf32..d9f15a3ee 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs @@ -35,8 +35,7 @@ .MinimumLevel.Warning() .Enrich.FromLogContext() .WriteTo.Console(formatProvider: CultureInfo.InvariantCulture) - .WriteTo.ApplicationInsights( - TelemetryConverter.Traces) + .WriteTo.ApplicationInsights(TelemetryConverter.Traces) .CreateBootstrapLogger(); try @@ -62,8 +61,7 @@ static void BuildAndRun(string[] args) .ReadFrom.Configuration(context.Configuration) .ReadFrom.Services(services) .Enrich.FromLogContext() - .WriteTo.ApplicationInsights( - TelemetryConverter.Traces)); + .WriteTo.ApplicationInsights(TelemetryConverter.Traces)); builder.Configuration .AddAzureConfiguration(builder.Environment.EnvironmentName) @@ -150,7 +148,7 @@ static void BuildAndRun(string[] args) tracing.AddAspNetCoreInstrumentation(options => { options.Filter = (httpContext) => - !httpContext.Request.Path.StartsWithSegments("/healthz"); + !httpContext.Request.Path.StartsWithSegments("/health"); }); tracing.AddHttpClientInstrumentation(); @@ -160,16 +158,13 @@ static void BuildAndRun(string[] args) .WithMetrics(metrics => { metrics.AddRuntimeInstrumentation(); - }); - - if (!builder.Environment.IsDevelopment()) - { - builder.Services.AddOpenTelemetry().UseAzureMonitor(); - } + }) + .UseAzureMonitor() + .Services - // Auth - builder.Services.AddDialogportenAuthentication(builder.Configuration) - .AddAuthorization(); + // Auth + .AddDialogportenAuthentication(builder.Configuration) + .AddAuthorization(); if (builder.Environment.IsDevelopment()) { diff --git a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json index c44ec303c..4edad013b 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json +++ b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json @@ -1,4 +1,7 @@ { + "AzureMonitor": { + "ConnectionString": "InstrumentationKey=00000000-0000-0000-0000-APP-SETTING" + }, "Infrastructure": { "Redis": { "ConnectionString": "localhost:6379" From a8902996c41b34676031a0f50d78a437ee4fddd4 Mon Sep 17 00:00:00 2001 From: Knut Haug Date: Fri, 11 Oct 2024 13:37:11 +0200 Subject: [PATCH 04/14] move config test --- docs/schema/V1/swagger.verified.json | 2 +- .../appsettings.Development.json | 3 --- src/Digdir.Domain.Dialogporten.WebApi/appsettings.json | 3 +++ .../Features/V1/SwaggerSnapshotTests.cs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/schema/V1/swagger.verified.json b/docs/schema/V1/swagger.verified.json index 2b657025f..f8d874223 100644 --- a/docs/schema/V1/swagger.verified.json +++ b/docs/schema/V1/swagger.verified.json @@ -7319,7 +7319,7 @@ }, "servers": [ { - "url": "https://localhost:7214/" + "url": "https://altinn-dev-api.azure-api.net/dialogporten" } ] } diff --git a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json index 4edad013b..c44ec303c 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json +++ b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json @@ -1,7 +1,4 @@ { - "AzureMonitor": { - "ConnectionString": "InstrumentationKey=00000000-0000-0000-0000-APP-SETTING" - }, "Infrastructure": { "Redis": { "ConnectionString": "localhost:6379" diff --git a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.json b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.json index ffd6358a4..6ea407a46 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.json +++ b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.json @@ -1,4 +1,7 @@ { + "AzureMonitor": { + "ConnectionString": "InstrumentationKey=00000000-0000-0000-0000-APP-SETTING" + }, "Logging": { "LogLevel": { "Default": "Warning" diff --git a/tests/Digdir.Domain.Dialogporten.WebApi.Integration.Tests/Features/V1/SwaggerSnapshotTests.cs b/tests/Digdir.Domain.Dialogporten.WebApi.Integration.Tests/Features/V1/SwaggerSnapshotTests.cs index ef0da0ad6..187a1269d 100644 --- a/tests/Digdir.Domain.Dialogporten.WebApi.Integration.Tests/Features/V1/SwaggerSnapshotTests.cs +++ b/tests/Digdir.Domain.Dialogporten.WebApi.Integration.Tests/Features/V1/SwaggerSnapshotTests.cs @@ -26,7 +26,7 @@ public async Task FailIfSwaggerSnapshotDoesNotMatch() var swaggerPath = Path.Combine(rootPath!, "docs/schema/V1"); var client = _webApplicationFactory - .WithWebHostBuilder(builder => builder.UseEnvironment("Development")) + .WithWebHostBuilder(builder => builder.UseEnvironment("test")) .CreateClient(); // Act From 48fb98bad64b5e52d36359359a5516e365e59559 Mon Sep 17 00:00:00 2001 From: Knut Haug Date: Fri, 18 Oct 2024 08:30:09 +0200 Subject: [PATCH 05/14] --wip-- [skip-ci] --- .../Digdir.Domain.Dialogporten.WebApi.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 d30dd8e33..e2b5cb69e 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj +++ b/src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj @@ -7,14 +7,15 @@ - + - + + @@ -22,7 +23,6 @@ - From ac3c9ed09292ed42164406905f2e3933ac70ff34 Mon Sep 17 00:00:00 2001 From: Knut Haug Date: Fri, 18 Oct 2024 11:33:42 +0200 Subject: [PATCH 06/14] --wip-- --- .../Program.cs | 24 +++++++++++-------- .../appsettings.Development.json | 2 +- .../appsettings.json | 3 --- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs index 6211ec0cd..da45bb047 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs @@ -20,6 +20,7 @@ using FastEndpoints; using FastEndpoints.Swagger; using FluentValidation; +using Microsoft.ApplicationInsights.Extensibility; using Microsoft.AspNetCore.Authorization; using Npgsql; using NSwag; @@ -30,16 +31,17 @@ 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(TelemetryConverter.Traces) + .WriteTo.ApplicationInsights(telemetryConfiguration, TelemetryConverter.Traces) .CreateBootstrapLogger(); try { - BuildAndRun(args); + BuildAndRun(args, telemetryConfiguration); } catch (Exception ex) when (ex is not OperationCanceledException) { @@ -51,7 +53,7 @@ Log.CloseAndFlush(); } -static void BuildAndRun(string[] args) +static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfiguration) { var builder = WebApplication.CreateBuilder(args); @@ -60,7 +62,7 @@ static void BuildAndRun(string[] args) .ReadFrom.Configuration(context.Configuration) .ReadFrom.Services(services) .Enrich.FromLogContext() - .WriteTo.ApplicationInsights(TelemetryConverter.Traces)); + .WriteTo.ApplicationInsights(telemetryConfiguration, TelemetryConverter.Traces)); builder.Configuration .AddAzureConfiguration(builder.Environment.EnvironmentName) @@ -124,6 +126,9 @@ static void BuildAndRun(string[] args) .JwtBearerTokenSchemas? .Select(z => z.WellKnown) .ToList() ?? []) + // Auth + .AddDialogportenAuthentication(builder.Configuration) + .AddAuthorization() // OpenTelemetry .AddOpenTelemetry() @@ -149,13 +154,12 @@ static void BuildAndRun(string[] args) .WithMetrics(metrics => { metrics.AddRuntimeInstrumentation(); - }) - .UseAzureMonitor() - .Services + }); - // Auth - .AddDialogportenAuthentication(builder.Configuration) - .AddAuthorization(); + if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"))) + { + builder.Services.AddOpenTelemetry().UseAzureMonitor(); + } if (builder.Environment.IsDevelopment()) { diff --git a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json index 00bfaadc1..b1e106a2d 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json +++ b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json @@ -74,7 +74,7 @@ "UseLocalDevelopmentCloudEventBus": true, "UseLocalDevelopmentCompactJwsGenerator": true, "DisableCache": true, - "DisableAuth": true, + "DisableAuth": false, "UseInMemoryServiceBusTransport": true } } diff --git a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.json b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.json index 6ea407a46..ffd6358a4 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.json +++ b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.json @@ -1,7 +1,4 @@ { - "AzureMonitor": { - "ConnectionString": "InstrumentationKey=00000000-0000-0000-0000-APP-SETTING" - }, "Logging": { "LogLevel": { "Default": "Warning" From 95bc9d78c33ccf739b20792b94c652e182637b6d Mon Sep 17 00:00:00 2001 From: Knut Haug Date: Fri, 18 Oct 2024 11:38:33 +0200 Subject: [PATCH 07/14] --wip-- [skip-ci] --- .../appsettings.Development.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json index b1e106a2d..7d4f6fafd 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json +++ b/src/Digdir.Domain.Dialogporten.WebApi/appsettings.Development.json @@ -66,15 +66,15 @@ } }, "LocalDevelopment": { - "UseLocalDevelopmentUser": false, - "UseLocalDevelopmentResourceRegister": false, - "UseLocalDevelopmentOrganizationRegister": false, - "UseLocalDevelopmentNameRegister": false, - "UseLocalDevelopmentAltinnAuthorization": false, + "UseLocalDevelopmentUser": true, + "UseLocalDevelopmentResourceRegister": true, + "UseLocalDevelopmentOrganizationRegister": true, + "UseLocalDevelopmentNameRegister": true, + "UseLocalDevelopmentAltinnAuthorization": true, "UseLocalDevelopmentCloudEventBus": true, "UseLocalDevelopmentCompactJwsGenerator": true, "DisableCache": true, - "DisableAuth": false, + "DisableAuth": true, "UseInMemoryServiceBusTransport": true } } From 093ae620122598c7fda2888d1b83d5ab148da2bd Mon Sep 17 00:00:00 2001 From: Knut Haug Date: Fri, 18 Oct 2024 12:03:35 +0200 Subject: [PATCH 08/14] Remove redis workaround --- .../InfrastructureExtensions.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs b/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs index da0e182e0..85d2d653d 100644 --- a/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs +++ b/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs @@ -97,10 +97,7 @@ internal static void AddInfrastructure_Internal(InfrastructureBuilderContext bui services.AddFusionCacheNeueccMessagePackSerializer(); - services.AddStackExchangeRedisCache(options => - options.ConnectionMultiplexerFactory = () => Task.FromResult( - ConnectionMultiplexer.Connect(infrastructureSettings.Redis.ConnectionString) - )); + services.AddStackExchangeRedisCache(opt => opt.Configuration = infrastructureSettings.Redis.ConnectionString); services.AddFusionCacheStackExchangeRedisBackplane(opt => opt.Configuration = infrastructureSettings.Redis.ConnectionString); From 18e6a7def89f21d06eb163d33abac3b0e1a6d68e Mon Sep 17 00:00:00 2001 From: Knut Haug <154342485+knuhau@users.noreply.github.com> Date: Fri, 18 Oct 2024 12:07:15 +0200 Subject: [PATCH 09/14] whitespace --- .../InfrastructureExtensions.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs b/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs index 85d2d653d..0b0aaa955 100644 --- a/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs +++ b/src/Digdir.Domain.Dialogporten.Infrastructure/InfrastructureExtensions.cs @@ -96,9 +96,7 @@ internal static void AddInfrastructure_Internal(InfrastructureBuilderContext bui .Decorate(typeof(INotificationHandler<>), typeof(IdempotentNotificationHandler<>)); services.AddFusionCacheNeueccMessagePackSerializer(); - services.AddStackExchangeRedisCache(opt => opt.Configuration = infrastructureSettings.Redis.ConnectionString); - services.AddFusionCacheStackExchangeRedisBackplane(opt => opt.Configuration = infrastructureSettings.Redis.ConnectionString); services.ConfigureFusionCache(nameof(Altinn.NameRegistry), new() From 465d25cde12ca86bf627a1703d98d25acfc335c2 Mon Sep 17 00:00:00 2001 From: Knut Haug Date: Fri, 18 Oct 2024 12:39:25 +0200 Subject: [PATCH 10/14] removed unneeded packages --- .../Digdir.Domain.Dialogporten.WebApi.csproj | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) 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 e2b5cb69e..e9a2efc60 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj +++ b/src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj @@ -15,14 +15,11 @@ - - - - - - + + + From e10b1e30e72e0dd3b31609e99bedeb0ac21bbe8c Mon Sep 17 00:00:00 2001 From: Knut Haug Date: Fri, 18 Oct 2024 14:33:08 +0200 Subject: [PATCH 11/14] refactoring, add app insights SDK for local dev --- .../WebApplicationBuilderExtensions.cs | 50 +++++++++++++++++++ .../Program.cs | 40 ++------------- 2 files changed, 54 insertions(+), 36 deletions(-) create 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 new file mode 100644 index 000000000..014c85b1e --- /dev/null +++ b/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs @@ -0,0 +1,50 @@ +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 ServiceCollectionExtensions +{ + 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(); + tracing.AddRedisInstrumentation(options => options.SetVerboseDatabaseStatements = true); + }) + .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/Program.cs b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs index da45bb047..aef33bcd3 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs @@ -3,17 +3,16 @@ using System.Reflection; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; -using Azure.Monitor.OpenTelemetry.AspNetCore; using Digdir.Domain.Dialogporten.Application; using Digdir.Domain.Dialogporten.Application.Common.Extensions; using Digdir.Domain.Dialogporten.Application.Common.Extensions.OptionExtensions; using Digdir.Domain.Dialogporten.Application.Externals.Presentation; +using Digdir.Domain.Dialogporten.WebApi.Common.Extensions; using Digdir.Domain.Dialogporten.Infrastructure; using Digdir.Domain.Dialogporten.WebApi; using Digdir.Domain.Dialogporten.WebApi.Common; using Digdir.Domain.Dialogporten.WebApi.Common.Authentication; using Digdir.Domain.Dialogporten.WebApi.Common.Authorization; -using Digdir.Domain.Dialogporten.WebApi.Common.Extensions; using Digdir.Domain.Dialogporten.WebApi.Common.Json; using Digdir.Domain.Dialogporten.WebApi.Common.Swagger; using Digdir.Library.Utils.AspNet; @@ -22,11 +21,7 @@ using FluentValidation; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.AspNetCore.Authorization; -using Npgsql; using NSwag; -using OpenTelemetry.Resources; -using OpenTelemetry.Trace; -using OpenTelemetry.Metrics; using Serilog; using Microsoft.Extensions.Options; @@ -76,6 +71,8 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura var thisAssembly = Assembly.GetExecutingAssembly(); + builder.ConfigureTelemetry(); + builder.Services // Options setup .ConfigureOptions() @@ -128,38 +125,9 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura .ToList() ?? []) // Auth .AddDialogportenAuthentication(builder.Configuration) - .AddAuthorization() + .AddAuthorization(); - // OpenTelemetry - .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(); - tracing.AddRedisInstrumentation(options => options.SetVerboseDatabaseStatements = true); - }) - .WithMetrics(metrics => - { - metrics.AddRuntimeInstrumentation(); - }); - if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"))) - { - builder.Services.AddOpenTelemetry().UseAzureMonitor(); - } if (builder.Environment.IsDevelopment()) { From ea61f36748fc258b6685518c733d1bec6a0e2c3f Mon Sep 17 00:00:00 2001 From: Knut Haug Date: Fri, 18 Oct 2024 17:29:13 +0200 Subject: [PATCH 12/14] --wip-- [skip-ci] --- .../Common/Extensions/WebApplicationBuilderExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs b/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs index 014c85b1e..88400eb72 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs @@ -6,7 +6,7 @@ namespace Digdir.Domain.Dialogporten.WebApi.Common.Extensions; -internal static class ServiceCollectionExtensions +internal static class WebApplicationBuilderExtensions { public static WebApplicationBuilder ConfigureTelemetry(this WebApplicationBuilder builder) { From d56888bbf51f3b16b363b62466edb64739b60395 Mon Sep 17 00:00:00 2001 From: Knut Haug <154342485+knuhau@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:31:20 +0200 Subject: [PATCH 13/14] whitespace [skip-ci] --- src/Digdir.Domain.Dialogporten.WebApi/Program.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs index aef33bcd3..f69497778 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Program.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/Program.cs @@ -127,8 +127,6 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura .AddDialogportenAuthentication(builder.Configuration) .AddAuthorization(); - - if (builder.Environment.IsDevelopment()) { var localDevelopmentSettings = builder.Configuration.GetLocalDevelopmentSettings(); From ef4c60ed764ec908a072da411d3239d1b9ef6d07 Mon Sep 17 00:00:00 2001 From: Knut Haug Date: Mon, 21 Oct 2024 14:54:58 +0200 Subject: [PATCH 14/14] remove redis instrumentation --- .../Common/Extensions/WebApplicationBuilderExtensions.cs | 1 - .../Digdir.Domain.Dialogporten.WebApi.csproj | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs b/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs index 88400eb72..88b734912 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs +++ b/src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs @@ -28,7 +28,6 @@ public static WebApplicationBuilder ConfigureTelemetry(this WebApplicationBuilde tracing.AddHttpClientInstrumentation(); tracing.AddNpgsql(); - tracing.AddRedisInstrumentation(options => options.SetVerboseDatabaseStatements = true); }) .WithMetrics(metrics => { 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 e9a2efc60..31715419d 100644 --- a/src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj +++ b/src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj @@ -17,7 +17,6 @@ -