♻️ Update service discovery settings

This commit is contained in:
2025-12-21 19:59:53 +08:00
parent 4242953969
commit 4c65602465
4 changed files with 28 additions and 22 deletions

View File

@@ -24,7 +24,8 @@ public static class Extensions
private const string HealthEndpointPath = "/health";
private const string AlivenessEndpointPath = "/alive";
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder)
where TBuilder : IHostApplicationBuilder
{
// Allow unencrypted grpc
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
@@ -34,6 +35,7 @@ public static class Extensions
builder.AddDefaultHealthChecks();
builder.Services.AddServiceDiscovery();
builder.Services.AddServiceDiscoveryCore();
builder.Services.ConfigureHttpClientDefaults(http =>
{
@@ -44,16 +46,16 @@ public static class Extensions
http.AddServiceDiscovery();
});
// Uncomment the following to restrict the allowed schemes for service discovery.
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
// {
// options.AllowedSchemes = ["https"];
// });
builder.Services.AddSingleton<IClock>(SystemClock.Instance);
builder.AddNatsClient("queue");
builder.AddRedisClient("cache", configureOptions: opts => { opts.AbortOnConnectFail = false; });
builder.AddRedisClient(
"cache",
configureOptions: opts =>
{
opts.AbortOnConnectFail = false;
}
);
// Setup cache service
builder.Services.AddStackExchangeRedisCache(options =>
@@ -84,16 +86,19 @@ public static class Extensions
logging.IncludeScopes = true;
});
builder.Services.AddOpenTelemetry()
builder
.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
metrics
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation();
})
.WithTracing(tracing =>
{
tracing.AddSource(builder.Environment.ApplicationName)
tracing
.AddSource(builder.Environment.ApplicationName)
.AddAspNetCoreInstrumentation(tracing =>
// Exclude health check requests from tracing
tracing.Filter = context =>
@@ -112,7 +117,9 @@ public static class Extensions
private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder)
where TBuilder : IHostApplicationBuilder
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
var useOtlpExporter = !string.IsNullOrWhiteSpace(
builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]
);
if (useOtlpExporter)
{
@@ -132,7 +139,8 @@ public static class Extensions
public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder)
where TBuilder : IHostApplicationBuilder
{
builder.Services.AddHealthChecks()
builder
.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
@@ -145,11 +153,11 @@ public static class Extensions
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")
});
app.MapHealthChecks(
AlivenessEndpointPath,
new HealthCheckOptions { Predicate = r => r.Tags.Contains("live") }
);
return app;
}
}
}