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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.14.0-beta.1" />
<PackageReference Include="Grpc.AspNetCore" Version="2.76.0" />
<PackageReference Include="Grpc.AspNetCore.HealthChecks" Version="2.76.0" />
<PackageReference Include="Aspire.StackExchange.Redis" Version="13.2.2" />
Expand Down
22 changes: 21 additions & 1 deletion samples/aspire-shop/AspireShop.BasketService/Program.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 commands related to the health check to avoid filling the dashboard with noise
options.Enrich = (activity, command) =>
{
if (command.ProfiledCommand.Command == "PING")
{
activity.IsAllDataRequested = false;
activity.ActivityTraceFlags = ActivityTraceFlags.None;
}
};
});
});

builder.Services.AddGrpc();
builder.Services.AddGrpcHealthChecks();
builder.Services.AddTransient<IBasketRepository, RedisBasketRepository>();
Expand Down
20 changes: 19 additions & 1 deletion samples/aspire-shop/AspireShop.CatalogDbManager/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
using Microsoft.EntityFrameworkCore;
using AspireShop.CatalogDb;
using AspireShop.CatalogDbManager;
using Npgsql;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();
builder.AddNpgsqlDbContext<CatalogDbContext>("catalogdb", null,
optionsBuilder => optionsBuilder.UseNpgsql(npgsqlBuilder =>
npgsqlBuilder.MigrationsAssembly(typeof(Program).Assembly.GetName().Name)));
{
npgsqlBuilder.MigrationsAssembly(typeof(Program).Assembly.GetName().Name);
npgsqlBuilder.ConfigureDataSource(dataSourceBuilder =>
{
dataSourceBuilder.ConfigureTracing(options =>
{
options.ConfigureCommandFilter(cmd =>
{
// Don't trace commands related to the health check to avoid filling the dashboard with noise
if (cmd.CommandText.Contains("SELECT 1"))
{
return false;
}
return true;
});
});
});
}));
Comment on lines 9 to +28
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This level of lambda nesting is truly unfortunate 😬

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it be useful to add a helper method to our integration to suppress?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could be yeah. What are you thinking? Something that hangs off the npgsqlBuilder in the above, hoisting it out 2 levels basically?


builder.Services.AddOpenTelemetry()
.WithTracing(tracing => tracing.AddSource(CatalogDbInitializer.ActivitySourceName));
Expand Down
21 changes: 20 additions & 1 deletion samples/aspire-shop/AspireShop.CatalogService/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
using AspireShop.CatalogDb;
using AspireShop.CatalogService;
using Microsoft.EntityFrameworkCore;
using Scalar.AspNetCore;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();
builder.AddNpgsqlDbContext<CatalogDbContext>("catalogdb");
builder.AddNpgsqlDbContext<CatalogDbContext>("catalogdb", null,
optionsBuilder => optionsBuilder.UseNpgsql(npgsqlBuilder =>
{
npgsqlBuilder.ConfigureDataSource(dataSourceBuilder =>
{
dataSourceBuilder.ConfigureTracing(options =>
{
options.ConfigureCommandFilter(cmd =>
{
// Don't trace commands related to the health check to avoid filling the dashboard with noise
if (cmd.CommandText.Contains("SELECT 1"))
{
return false;
}
return true;
});
});
});
}));

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddProblemDetails();
Expand Down
41 changes: 36 additions & 5 deletions samples/aspire-shop/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 @@ -57,11 +59,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