|
| 1 | +using Microsoft.AspNetCore.Builder; |
| 2 | +using Microsoft.AspNetCore.Diagnostics.HealthChecks; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using OpenTelemetry; |
| 7 | +using OpenTelemetry.Metrics; |
| 8 | +using OpenTelemetry.Trace; |
| 9 | + |
| 10 | +namespace Microsoft.Extensions.Hosting; |
| 11 | + |
| 12 | +// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. |
| 13 | +// This project should be referenced by each service project in your solution. |
| 14 | +// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults |
| 15 | +public static class Extensions |
| 16 | +{ |
| 17 | + public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder) |
| 18 | + { |
| 19 | + builder.ConfigureOpenTelemetry(); |
| 20 | + |
| 21 | + builder.AddDefaultHealthChecks(); |
| 22 | + |
| 23 | + builder.Services.AddServiceDiscovery(); |
| 24 | + |
| 25 | + builder.Services.ConfigureHttpClientDefaults(http => |
| 26 | + { |
| 27 | + // Turn on resilience by default |
| 28 | + http.AddStandardResilienceHandler(); |
| 29 | + |
| 30 | + // Turn on service discovery by default |
| 31 | + http.AddServiceDiscovery(); |
| 32 | + }); |
| 33 | + |
| 34 | + // Uncomment the following to restrict the allowed schemes for service discovery. |
| 35 | + // builder.Services.Configure<ServiceDiscoveryOptions>(options => |
| 36 | + // { |
| 37 | + // options.AllowedSchemes = ["https"]; |
| 38 | + // }); |
| 39 | + |
| 40 | + return builder; |
| 41 | + } |
| 42 | + |
| 43 | + public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder) |
| 44 | + { |
| 45 | + builder.Logging.AddOpenTelemetry(logging => |
| 46 | + { |
| 47 | + logging.IncludeFormattedMessage = true; |
| 48 | + logging.IncludeScopes = true; |
| 49 | + }); |
| 50 | + |
| 51 | + builder.Services.AddOpenTelemetry() |
| 52 | + .WithMetrics(metrics => |
| 53 | + { |
| 54 | + metrics.AddAspNetCoreInstrumentation() |
| 55 | + .AddHttpClientInstrumentation() |
| 56 | + .AddRuntimeInstrumentation() |
| 57 | + .AddMeter("Microsoft.Orleans"); |
| 58 | + }) |
| 59 | + .WithTracing(tracing => |
| 60 | + { |
| 61 | + tracing.AddAspNetCoreInstrumentation() |
| 62 | + //.AddGrpcClientInstrumentation() |
| 63 | + .AddHttpClientInstrumentation() |
| 64 | + .AddSource("Microsoft.Orleans.Application") |
| 65 | + .AddSource("AutoGen.Agent"); |
| 66 | + }); |
| 67 | + |
| 68 | + builder.AddOpenTelemetryExporters(); |
| 69 | + |
| 70 | + return builder; |
| 71 | + } |
| 72 | + |
| 73 | + private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder) |
| 74 | + { |
| 75 | + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); |
| 76 | + |
| 77 | + if (useOtlpExporter) |
| 78 | + { |
| 79 | + builder.Services.AddOpenTelemetry().UseOtlpExporter(); |
| 80 | + } |
| 81 | + |
| 82 | + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) |
| 83 | + //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) |
| 84 | + //{ |
| 85 | + // builder.Services.AddOpenTelemetry() |
| 86 | + // .UseAzureMonitor(); |
| 87 | + //} |
| 88 | + |
| 89 | + return builder; |
| 90 | + } |
| 91 | + |
| 92 | + public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder) |
| 93 | + { |
| 94 | + builder.Services.AddHealthChecks() |
| 95 | + // Add a default liveness check to ensure app is responsive |
| 96 | + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); |
| 97 | + |
| 98 | + return builder; |
| 99 | + } |
| 100 | + |
| 101 | + public static WebApplication MapDefaultEndpoints(this WebApplication app) |
| 102 | + { |
| 103 | + // Adding health checks endpoints to applications in non-development environments has security implications. |
| 104 | + // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. |
| 105 | + if (app.Environment.IsDevelopment()) |
| 106 | + { |
| 107 | + // All health checks must pass for app to be considered ready to accept traffic after starting |
| 108 | + app.MapHealthChecks("/health"); |
| 109 | + |
| 110 | + // Only health checks tagged with the "live" tag must pass for app to be considered alive |
| 111 | + app.MapHealthChecks("/alive", new HealthCheckOptions |
| 112 | + { |
| 113 | + Predicate = r => r.Tags.Contains("live") |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + return app; |
| 118 | + } |
| 119 | +} |
0 commit comments