diff --git a/services/Directory/FilterLists.Directory.Api/Program.cs b/services/Directory/FilterLists.Directory.Api/Program.cs index a5bbb274c..c9e4d8527 100644 --- a/services/Directory/FilterLists.Directory.Api/Program.cs +++ b/services/Directory/FilterLists.Directory.Api/Program.cs @@ -6,6 +6,8 @@ var builder = WebApplication.CreateBuilder(args); builder.WebHost.ConfigureKestrel(serverOptions => serverOptions.AddServerHeader = false); +builder.Services.AddRequestTimeouts(); +builder.Services.AddOutputCache(); builder.AddServiceDefaults(); builder.Services.AddCors(options => CorsConfiguration.SetupAction(options, builder.Configuration)); builder.Services.AddProblemDetails(); @@ -17,6 +19,8 @@ app.UseExceptionHandler(); app.UseCors(); +app.UseRequestTimeouts(); +app.UseOutputCache(); app.MapEndpoints(); app.MapDefaultEndpoints(); app.UseSwagger(o => o.RouteTemplate = "{documentName}/openapi.json"); diff --git a/services/FilterLists.ServiceDefaults/Extensions.cs b/services/FilterLists.ServiceDefaults/Extensions.cs index 9f5e184e8..6ccaac9ec 100644 --- a/services/FilterLists.ServiceDefaults/Extensions.cs +++ b/services/FilterLists.ServiceDefaults/Extensions.cs @@ -1,6 +1,5 @@ using Azure.Monitor.OpenTelemetry.AspNetCore; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Logging; @@ -100,6 +99,15 @@ private static TBuilder AddOpenTelemetryExporters(this TBuilder builde public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder { + builder.Services.AddRequestTimeouts( + configure: static timeouts => + timeouts.AddPolicy("HealthChecks", TimeSpan.FromSeconds(5))); + + builder.Services.AddOutputCache( + configureOptions: static caching => + caching.AddPolicy("HealthChecks", + build: static policy => policy.Expire(TimeSpan.FromSeconds(10)))); + builder.Services.AddHealthChecks() // Add a default liveness check to ensure app is responsive .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); @@ -109,19 +117,21 @@ public static TBuilder AddDefaultHealthChecks(this TBuilder builder) w 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); + // https://aka.ms/dotnet/aspire/healthchecks + var healthChecks = app.MapGroup(""); - // 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") - }); - // } + healthChecks + .CacheOutput("HealthChecks") + .WithRequestTimeout("HealthChecks"); + + // All health checks must pass for app to be considered ready to accept traffic after starting + healthChecks.MapHealthChecks(HealthEndpointPath); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + healthChecks.MapHealthChecks(AlivenessEndpointPath, new() + { + Predicate = static r => r.Tags.Contains("live") + }); return app; }