Adds common Aspire services: service discovery, resilience, health checks, and OpenTelemetry. This project should be referenced by each service project in your solution. To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
| 14 | // This project should be referenced by each service project in your solution. |
| 15 | // To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults |
| 16 | public static class Extensions |
| 17 | { |
| 18 | private const string HealthEndpointPath = "/health"; |
| 19 | private const string AlivenessEndpointPath = "/alive"; |
| 20 | |
| 21 | public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder |
| 22 | { |
| 23 | builder.ConfigureOpenTelemetry(); |
| 24 | |
| 25 | builder.AddDefaultHealthChecks(); |
| 26 | |
| 27 | builder.Services.AddServiceDiscovery(); |
| 28 | |
| 29 | builder.Services.ConfigureHttpClientDefaults(http => |
| 30 | { |
| 31 | // Turn on resilience by default |
| 32 | http.AddStandardResilienceHandler(); |
| 33 | |
| 34 | // Turn on service discovery by default |
| 35 | http.AddServiceDiscovery(); |
| 36 | }); |
| 37 | |
| 38 | // Uncomment the following to restrict the allowed schemes for service discovery. |
| 39 | // builder.Services.Configure<ServiceDiscoveryOptions>(options => |
| 40 | // { |
| 41 | // options.AllowedSchemes = ["https"]; |
| 42 | // }); |
| 43 | |
| 44 | return builder; |
| 45 | } |
| 46 | |
| 47 | public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) |
| 48 | where TBuilder : IHostApplicationBuilder |
| 49 | { |
| 50 | builder.Logging.AddOpenTelemetry(logging => |
| 51 | { |
| 52 | logging.IncludeFormattedMessage = true; |
| 53 | logging.IncludeScopes = true; |
| 54 | }); |
| 55 | |
| 56 | builder.Services.AddOpenTelemetry() |
| 57 | .WithMetrics(metrics => |
| 58 | { |
| 59 | metrics.AddAspNetCoreInstrumentation() |
| 60 | .AddHttpClientInstrumentation() |
| 61 | .AddRuntimeInstrumentation(); |
| 62 | }) |
| 63 | .WithTracing(tracing => |
| 64 | { |
| 65 | tracing.AddSource(builder.Environment.ApplicationName) |
| 66 | .AddAspNetCoreInstrumentation(tracing => |
| 67 | // Exclude health check requests from tracing |
| 68 | tracing.Filter = context => |
| 69 | !context.Request.Path.StartsWithSegments(HealthEndpointPath) |
| 70 | && !context.Request.Path.StartsWithSegments(AlivenessEndpointPath) |
| 71 | ) |
| 72 | // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) |
| 73 | //.AddGrpcClientInstrumentation() |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…