Skip to content

Commit 30e686c

Browse files
Filter out tracing health endpoints in service defaults templates (#8643)
* Filter out tracing health endpoints Fixes #8580 * Fix typo
1 parent d69ef36 commit 30e686c

File tree

5 files changed

+48
-14
lines changed

5 files changed

+48
-14
lines changed

playground/TestShop/TestShop.AppHost/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@
6262
.WithReference(basketCache)
6363
.WithReference(messaging).WaitFor(messaging);
6464

65-
builder.AddProject<Projects.MyFrontend>("frontend")
65+
var frontend = builder.AddProject<Projects.MyFrontend>("frontend")
6666
.WithExternalHttpEndpoints()
6767
.WithReference(basketService)
6868
.WithReference(catalogService)
6969
.WithUrls(c => c.Urls.ForEach(u => u.DisplayText = $"Online store ({u.Endpoint?.EndpointName})"));
7070

71+
var _ = frontend.GetEndpoint("https").Exists ? frontend.WithHttpsHealthCheck("/health") : frontend.WithHttpHealthCheck("/health");
72+
7173
builder.AddProject<Projects.OrderProcessor>("orderprocessor", launchProfileName: "OrderProcessor")
72-
.WithReference(messaging).WaitFor(messaging);
74+
.WithReference(messaging).WaitFor(messaging);
7375

7476
builder.AddProject<Projects.ApiGateway>("apigateway")
7577
.WithReference(basketService)

playground/TestShop/TestShop.ServiceDefaults/Extensions.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ namespace Microsoft.Extensions.Hosting;
1414
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
1515
public static class Extensions
1616
{
17+
private const string HealthEndpointPath = "/health";
18+
private const string AlivenessEndpointPath = "/alive";
19+
1720
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
1821
{
1922
builder.ConfigureOpenTelemetry();
@@ -52,7 +55,12 @@ public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) w
5255
})
5356
.WithTracing(tracing =>
5457
{
55-
tracing.AddAspNetCoreInstrumentation()
58+
tracing
59+
.AddAspNetCoreInstrumentation(tracing =>
60+
tracing.Filter = context =>
61+
!context.Request.Path.StartsWithSegments(HealthEndpointPath)
62+
&& !context.Request.Path.StartsWithSegments(AlivenessEndpointPath)
63+
)
5664
.AddGrpcClientInstrumentation()
5765
.AddHttpClientInstrumentation();
5866
});
@@ -97,10 +105,10 @@ public static WebApplication MapDefaultEndpoints(this WebApplication app)
97105
if (app.Environment.IsDevelopment())
98106
{
99107
// All health checks must pass for app to be considered ready to accept traffic after starting
100-
app.MapHealthChecks("/health");
108+
app.MapHealthChecks(HealthEndpointPath);
101109

102110
// Only health checks tagged with the "live" tag must pass for app to be considered alive
103-
app.MapHealthChecks("/alive", new HealthCheckOptions
111+
app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions
104112
{
105113
Predicate = r => r.Tags.Contains("live")
106114
});

src/Aspire.ProjectTemplates/templates/aspire-empty/9.2/AspireApplication.1.ServiceDefaults/Extensions.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ namespace Microsoft.Extensions.Hosting;
1515
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
1616
public static class Extensions
1717
{
18+
private const string HealthEndpointPath = "/health";
19+
private const string AlivenessEndpointPath = "/alive";
20+
1821
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
1922
{
2023
builder.ConfigureOpenTelemetry();
@@ -59,7 +62,12 @@ public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) w
5962
.WithTracing(tracing =>
6063
{
6164
tracing.AddSource(builder.Environment.ApplicationName)
62-
.AddAspNetCoreInstrumentation()
65+
.AddAspNetCoreInstrumentation(tracing =>
66+
// Exclude health check requests from tracing
67+
tracing.Filter = context =>
68+
!context.Request.Path.StartsWithSegments(HealthEndpointPath)
69+
&& !context.Request.Path.StartsWithSegments(AlivenessEndpointPath)
70+
)
6371
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
6472
//.AddGrpcClientInstrumentation()
6573
.AddHttpClientInstrumentation();
@@ -105,10 +113,10 @@ public static WebApplication MapDefaultEndpoints(this WebApplication app)
105113
if (app.Environment.IsDevelopment())
106114
{
107115
// All health checks must pass for app to be considered ready to accept traffic after starting
108-
app.MapHealthChecks("/health");
116+
app.MapHealthChecks(HealthEndpointPath);
109117

110118
// Only health checks tagged with the "live" tag must pass for app to be considered alive
111-
app.MapHealthChecks("/alive", new HealthCheckOptions
119+
app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions
112120
{
113121
Predicate = r => r.Tags.Contains("live")
114122
});

src/Aspire.ProjectTemplates/templates/aspire-servicedefaults/9.2/Extensions.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ namespace Microsoft.Extensions.Hosting;
1515
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
1616
public static class Extensions
1717
{
18+
private const string HealthEndpointPath = "/health";
19+
private const string AlivenessEndpointPath = "/alive";
20+
1821
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
1922
{
2023
builder.ConfigureOpenTelemetry();
@@ -59,7 +62,12 @@ public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) w
5962
.WithTracing(tracing =>
6063
{
6164
tracing.AddSource(builder.Environment.ApplicationName)
62-
.AddAspNetCoreInstrumentation()
65+
.AddAspNetCoreInstrumentation(tracing =>
66+
// Exclude health check requests from tracing
67+
tracing.Filter = context =>
68+
!context.Request.Path.StartsWithSegments(HealthEndpointPath)
69+
&& !context.Request.Path.StartsWithSegments(AlivenessEndpointPath)
70+
)
6371
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
6472
//.AddGrpcClientInstrumentation()
6573
.AddHttpClientInstrumentation();
@@ -105,10 +113,10 @@ public static WebApplication MapDefaultEndpoints(this WebApplication app)
105113
if (app.Environment.IsDevelopment())
106114
{
107115
// All health checks must pass for app to be considered ready to accept traffic after starting
108-
app.MapHealthChecks("/health");
116+
app.MapHealthChecks(HealthEndpointPath);
109117

110118
// Only health checks tagged with the "live" tag must pass for app to be considered alive
111-
app.MapHealthChecks("/alive", new HealthCheckOptions
119+
app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions
112120
{
113121
Predicate = r => r.Tags.Contains("live")
114122
});

src/Aspire.ProjectTemplates/templates/aspire-starter/9.2/Aspire-StarterApplication.1.ServiceDefaults/Extensions.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ namespace Microsoft.Extensions.Hosting;
1515
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
1616
public static class Extensions
1717
{
18+
private const string HealthEndpointPath = "/health";
19+
private const string AlivenessEndpointPath = "/alive";
20+
1821
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
1922
{
2023
builder.ConfigureOpenTelemetry();
@@ -59,7 +62,12 @@ public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) w
5962
.WithTracing(tracing =>
6063
{
6164
tracing.AddSource(builder.Environment.ApplicationName)
62-
.AddAspNetCoreInstrumentation()
65+
.AddAspNetCoreInstrumentation(tracing =>
66+
// Exclude health check requests from tracing
67+
tracing.Filter = context =>
68+
!context.Request.Path.StartsWithSegments(HealthEndpointPath)
69+
&& !context.Request.Path.StartsWithSegments(AlivenessEndpointPath)
70+
)
6371
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
6472
//.AddGrpcClientInstrumentation()
6573
.AddHttpClientInstrumentation();
@@ -105,10 +113,10 @@ public static WebApplication MapDefaultEndpoints(this WebApplication app)
105113
if (app.Environment.IsDevelopment())
106114
{
107115
// All health checks must pass for app to be considered ready to accept traffic after starting
108-
app.MapHealthChecks("/health");
116+
app.MapHealthChecks(HealthEndpointPath);
109117

110118
// Only health checks tagged with the "live" tag must pass for app to be considered alive
111-
app.MapHealthChecks("/alive", new HealthCheckOptions
119+
app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions
112120
{
113121
Predicate = r => r.Tags.Contains("live")
114122
});

0 commit comments

Comments
 (0)