Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion samples/AspireShop/AspireShop.BasketService/AppHost.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
using AspireShop.BasketService;
using System.Diagnostics;
using AspireShop.BasketService;
using AspireShop.BasketService.Repositories;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();
builder.AddRedisClient("basketcache");

builder.Services.AddOpenTelemetry()
.WithTracing(tracerProviderBuilder =>
{
tracerProviderBuilder
.AddRedisInstrumentation(options =>
{
// Don't trace requests to the health endpoint to avoid filling the dashboard with noise
options.Enrich = (activity, command) =>
{
if (string.Equals(command.Command, "PING", StringComparison.OrdinalIgnoreCase))
{
activity.IsAllDataRequested = false;
activity.ActivityTraceFlags = ActivityTraceFlags.None;
}
};
});
});

builder.Services.AddGrpc();
builder.Services.AddGrpcHealthChecks();
builder.Services.AddTransient<IBasketRepository, RedisBasketRepository>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Grpc.AspNetCore" Version="2.70.0" />
<PackageReference Include="Grpc.AspNetCore.HealthChecks" Version="2.70.0" />
<PackageReference Include="Aspire.StackExchange.Redis" Version="9.4.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.9.0-beta.1" />
</ItemGroup>

<ItemGroup>
Expand Down
41 changes: 36 additions & 5 deletions samples/AspireShop/AspireShop.ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder;
using System.Diagnostics;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
Expand All @@ -12,6 +13,7 @@ namespace Microsoft.Extensions.Hosting;
public static class Extensions
{
private const string HealthEndpointPath = "/health";
private const string GrpcHealthEndpointPath = "/grpc.health.v1.Health/Check";
private const string AlivenessEndpointPath = "/alive";

public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
Expand Down Expand Up @@ -56,11 +58,40 @@ public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicati
.AddAspNetCoreInstrumentation(tracing =>
// Don't trace requests to the health endpoint to avoid filling the dashboard with noise
tracing.Filter = httpContext =>
!(httpContext.Request.Path.StartsWithSegments(HealthEndpointPath)
|| httpContext.Request.Path.StartsWithSegments(AlivenessEndpointPath))
{
var path = httpContext.Request.Path;
if (path.StartsWithSegments(HealthEndpointPath) ||
path.StartsWithSegments(GrpcHealthEndpointPath) ||
path.StartsWithSegments(AlivenessEndpointPath))
return false;
return true;
}
)
.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();
.AddGrpcClientInstrumentation(grpcOptions =>
// Don't trace requests to the health endpoint to avoid filling the dashboard with noise
grpcOptions.EnrichWithHttpRequestMessage = (activity, request) =>
{
if (request.RequestUri?.AbsolutePath == GrpcHealthEndpointPath)
Copy link

Copilot AI May 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider aligning the comparison logic for the gRPC health endpoint with the HTTP instrumentation filter (which uses StartsWith) to maintain consistency in handling potential URL variations.

Suggested change
if (request.RequestUri?.AbsolutePath == GrpcHealthEndpointPath)
if (request.RequestUri?.AbsolutePath.StartsWith(GrpcHealthEndpointPath))

Copilot uses AI. Check for mistakes.
{
activity.IsAllDataRequested = false;
activity.ActivityTraceFlags = ActivityTraceFlags.None;
}
}
)
.AddHttpClientInstrumentation(httpOptions =>
// Don't trace requests to the health endpoint to avoid filling the dashboard with noise
httpOptions.FilterHttpRequestMessage = (request) =>
{
var path = request.RequestUri?.AbsolutePath ?? string.Empty;
if (path.StartsWith(HealthEndpointPath) ||
path.StartsWith(GrpcHealthEndpointPath) ||
path.StartsWith(AlivenessEndpointPath))
{
return false;
}
return true;
}
);
});

builder.AddOpenTelemetryExporters();
Expand Down