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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Extensions.cs
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.AspNetCore.Diagnostics.HealthChecks;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Diagnostics.HealthChecks;
  7. using Microsoft.Extensions.Logging;
  8. using OpenTelemetry;
  9. using OpenTelemetry.Metrics;
  10. using OpenTelemetry.Trace;
  11. namespace Microsoft.Extensions.Hosting;
  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 TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
  18. {
  19. builder.ConfigureOpenTelemetry();
  20. builder.AddDefaultHealthChecks();
  21. builder.Services.AddServiceDiscovery();
  22. builder.Services.ConfigureHttpClientDefaults(http =>
  23. {
  24. // Turn on resilience by default
  25. http.AddStandardResilienceHandler();
  26. // Turn on service discovery by default
  27. http.AddServiceDiscovery();
  28. });
  29. // Uncomment the following to restrict the allowed schemes for service discovery.
  30. // builder.Services.Configure<ServiceDiscoveryOptions>(options =>
  31. // {
  32. // options.AllowedSchemes = ["https"];
  33. // });
  34. return builder;
  35. }
  36. public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
  37. {
  38. builder.Logging.AddOpenTelemetry(logging =>
  39. {
  40. logging.IncludeFormattedMessage = true;
  41. logging.IncludeScopes = true;
  42. });
  43. builder.Services.AddOpenTelemetry()
  44. .WithMetrics(metrics =>
  45. {
  46. metrics.AddAspNetCoreInstrumentation()
  47. .AddHttpClientInstrumentation()
  48. .AddRuntimeInstrumentation();
  49. })
  50. .WithTracing(tracing =>
  51. {
  52. tracing.AddSource(builder.Environment.ApplicationName)
  53. .AddAspNetCoreInstrumentation()
  54. // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
  55. //.AddGrpcClientInstrumentation()
  56. .AddHttpClientInstrumentation();
  57. });
  58. builder.AddOpenTelemetryExporters();
  59. return builder;
  60. }
  61. private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
  62. {
  63. var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
  64. if (useOtlpExporter)
  65. {
  66. builder.Services.AddOpenTelemetry().UseOtlpExporter();
  67. }
  68. // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
  69. //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
  70. //{
  71. // builder.Services.AddOpenTelemetry()
  72. // .UseAzureMonitor();
  73. //}
  74. return builder;
  75. }
  76. public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
  77. {
  78. builder.Services.AddHealthChecks()
  79. // Add a default liveness check to ensure app is responsive
  80. .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
  81. return builder;
  82. }
  83. public static WebApplication MapDefaultEndpoints(this WebApplication app)
  84. {
  85. // Adding health checks endpoints to applications in non-development environments has security implications.
  86. // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
  87. if (app.Environment.IsDevelopment())
  88. {
  89. // All health checks must pass for app to be considered ready to accept traffic after starting
  90. app.MapHealthChecks("/health");
  91. // Only health checks tagged with the "live" tag must pass for app to be considered alive
  92. app.MapHealthChecks("/alive", new HealthCheckOptions
  93. {
  94. Predicate = r => r.Tags.Contains("live")
  95. });
  96. }
  97. return app;
  98. }
  99. }