From 3865616802d5239335d01062be33c0a72b5111e2 Mon Sep 17 00:00:00 2001 From: Collin Barrett <6483057+collinbarrett@users.noreply.github.com> Date: Sat, 31 May 2025 11:56:32 -0500 Subject: [PATCH] refactor(api): reset default aspire config --- .../FilterLists.ServiceDefaults/Extensions.cs | 143 +++++++++++------- 1 file changed, 85 insertions(+), 58 deletions(-) diff --git a/services/FilterLists.ServiceDefaults/Extensions.cs b/services/FilterLists.ServiceDefaults/Extensions.cs index 43ba90556..a2d364643 100644 --- a/services/FilterLists.ServiceDefaults/Extensions.cs +++ b/services/FilterLists.ServiceDefaults/Extensions.cs @@ -9,30 +9,43 @@ using OpenTelemetry.Metrics; using OpenTelemetry.Trace; -// ReSharper disable once CheckNamespace namespace Microsoft.Extensions.Hosting; +// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. +// This project should be referenced by each service project in your solution. +// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults public static class Extensions { private const string HealthEndpointPath = "/health"; private const string AlivenessEndpointPath = "/alive"; - private const string HealthCheckPolicyName = "HealthChecks"; - public static void AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder + public static TBuilder AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder { builder.ConfigureOpenTelemetry(); + builder.AddDefaultHealthChecks(); + builder.Services.AddServiceDiscovery(); + builder.Services.ConfigureHttpClientDefaults(http => { + // Turn on resilience by default http.AddStandardResilienceHandler(); + + // Turn on service discovery by default http.AddServiceDiscovery(); }); - builder.Services.Configure(options => options.AllowedSchemes = ["https"]); + // Uncomment the following to restrict the allowed schemes for service discovery. + builder.Services.Configure(options => + { + options.AllowedSchemes = ["https"]; + }); + + return builder; } - private static void ConfigureOpenTelemetry(this TBuilder builder) where TBuilder : IHostApplicationBuilder + public static TBuilder ConfigureOpenTelemetry(this TBuilder builder) where TBuilder : IHostApplicationBuilder { builder.Logging.AddOpenTelemetry(logging => { @@ -40,62 +53,76 @@ private static void ConfigureOpenTelemetry(this TBuilder builder) wher logging.IncludeScopes = true; }); - var otBuilder = builder.Services.AddOpenTelemetry() + builder.Services.AddOpenTelemetry() .WithMetrics(metrics => + { metrics.AddAspNetCoreInstrumentation() .AddHttpClientInstrumentation() - .AddRuntimeInstrumentation()) - .WithTracing(tracing => - tracing.AddSource(builder.Environment.ApplicationName) - .AddAspNetCoreInstrumentation(tracingOptions => - tracingOptions.Filter = context => - !context.Request.Path.StartsWithSegments(HealthEndpointPath, - StringComparison.InvariantCultureIgnoreCase) - && !context.Request.Path.StartsWithSegments(AlivenessEndpointPath, - StringComparison.InvariantCultureIgnoreCase) - ) - .AddHttpClientInstrumentation()); - - if (!string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"])) - otBuilder.UseOtlpExporter(); - - if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) - otBuilder.UseAzureMonitor(); - } - - // https://learn.microsoft.com/en-us/dotnet/aspire/fundamentals/health-checks#non-development-environments - private static void AddDefaultHealthChecks(this IHostApplicationBuilder builder) - { - builder.Services.AddRequestTimeouts(); - builder.Services.AddOutputCache(); - - builder.Services.AddRequestTimeouts(static timeouts => - timeouts.AddPolicy(HealthCheckPolicyName, TimeSpan.FromSeconds(5))); - - builder.Services.AddOutputCache(static caching => - caching.AddPolicy(HealthCheckPolicyName, static policy => policy.Expire(TimeSpan.FromSeconds(10)))); - - builder.Services.AddHealthChecks() - .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); - } - - // https://learn.microsoft.com/en-us/dotnet/aspire/fundamentals/health-checks#non-development-environments - public static void MapDefaultEndpoints(this WebApplication app) - { - app.UseRequestTimeouts(); - app.UseOutputCache(); - - var healthChecks = app.MapGroup(""); - - healthChecks - .CacheOutput(HealthCheckPolicyName) - .WithRequestTimeout(HealthCheckPolicyName); - - healthChecks.MapHealthChecks(HealthEndpointPath).DisableHttpMetrics(); - healthChecks.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions - { - Predicate = static r => r.Tags.Contains("live") + .AddRuntimeInstrumentation(); }) - .DisableHttpMetrics(); + .WithTracing(tracing => + { + tracing.AddSource(builder.Environment.ApplicationName) + .AddAspNetCoreInstrumentation(tracing => + // Exclude health check requests from tracing + tracing.Filter = context => + !context.Request.Path.StartsWithSegments(HealthEndpointPath) + && !context.Request.Path.StartsWithSegments(AlivenessEndpointPath) + ) + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) + //.AddGrpcClientInstrumentation() + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + + if (useOtlpExporter) + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) + if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) + { + builder.Services.AddOpenTelemetry() + .UseAzureMonitor(); + } + + return builder; + } + + public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Services.AddHealthChecks() + // Add a default liveness check to ensure app is responsive + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + // Adding health checks endpoints to applications in non-development environments has security implications. + // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. + // if (app.Environment.IsDevelopment()) + // { + // All health checks must pass for app to be considered ready to accept traffic after starting + app.MapHealthChecks(HealthEndpointPath); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + // } + + return app; } } \ No newline at end of file