You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Extensions.cs 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. namespace Microsoft.Extensions.Hosting;
  10. // Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
  11. // This project should be referenced by each service project in your solution.
  12. // To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
  13. public static class Extensions
  14. {
  15. public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
  16. {
  17. builder.ConfigureOpenTelemetry();
  18. builder.AddDefaultHealthChecks();
  19. builder.Services.AddServiceDiscovery();
  20. builder.Services.ConfigureHttpClientDefaults(http =>
  21. {
  22. // Turn on resilience by default
  23. http.AddStandardResilienceHandler();
  24. // Turn on service discovery by default
  25. http.AddServiceDiscovery();
  26. });
  27. return builder;
  28. }
  29. public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder)
  30. {
  31. builder.Logging.AddOpenTelemetry(logging =>
  32. {
  33. logging.IncludeFormattedMessage = true;
  34. logging.IncludeScopes = true;
  35. });
  36. builder.Services.AddOpenTelemetry()
  37. .WithMetrics(metrics =>
  38. {
  39. metrics.AddAspNetCoreInstrumentation()
  40. .AddHttpClientInstrumentation()
  41. .AddRuntimeInstrumentation();
  42. })
  43. .WithTracing(tracing =>
  44. {
  45. tracing.AddAspNetCoreInstrumentation()
  46. // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
  47. //.AddGrpcClientInstrumentation()
  48. .AddHttpClientInstrumentation();
  49. });
  50. builder.AddOpenTelemetryExporters();
  51. return builder;
  52. }
  53. private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder)
  54. {
  55. var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
  56. if (useOtlpExporter)
  57. {
  58. builder.Services.AddOpenTelemetry().UseOtlpExporter();
  59. }
  60. // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
  61. //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
  62. //{
  63. // builder.Services.AddOpenTelemetry()
  64. // .UseAzureMonitor();
  65. //}
  66. return builder;
  67. }
  68. public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder)
  69. {
  70. builder.Services.AddHealthChecks()
  71. // Add a default liveness check to ensure app is responsive
  72. .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
  73. return builder;
  74. }
  75. public static WebApplication MapDefaultEndpoints(this WebApplication app)
  76. {
  77. // Adding health checks endpoints to applications in non-development environments has security implications.
  78. // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
  79. if (app.Environment.IsDevelopment())
  80. {
  81. // All health checks must pass for app to be considered ready to accept traffic after starting
  82. app.MapHealthChecks("/health");
  83. // Only health checks tagged with the "live" tag must pass for app to be considered alive
  84. app.MapHealthChecks("/alive", new HealthCheckOptions
  85. {
  86. Predicate = r => r.Tags.Contains("live")
  87. });
  88. }
  89. return app;
  90. }
  91. }