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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Uncomment the following to restrict the allowed schemes for service discovery.
  28. // builder.Services.Configure<ServiceDiscoveryOptions>(options =>
  29. // {
  30. // options.AllowedSchemes = ["https"];
  31. // });
  32. return builder;
  33. }
  34. public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder)
  35. {
  36. builder.Logging.AddOpenTelemetry(logging =>
  37. {
  38. logging.IncludeFormattedMessage = true;
  39. logging.IncludeScopes = true;
  40. });
  41. builder.Services.AddOpenTelemetry()
  42. .WithMetrics(metrics =>
  43. {
  44. metrics.AddAspNetCoreInstrumentation()
  45. .AddHttpClientInstrumentation()
  46. .AddRuntimeInstrumentation()
  47. .AddMeter("Microsoft.Orleans");
  48. })
  49. .WithTracing(tracing =>
  50. {
  51. tracing.AddAspNetCoreInstrumentation()
  52. //.AddGrpcClientInstrumentation()
  53. .AddHttpClientInstrumentation()
  54. .AddSource("Microsoft.Orleans.Application")
  55. .AddSource("Starfleet.Agent");
  56. });
  57. builder.AddOpenTelemetryExporters();
  58. return builder;
  59. }
  60. private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder)
  61. {
  62. var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
  63. if (useOtlpExporter)
  64. {
  65. builder.Services.AddOpenTelemetry().UseOtlpExporter();
  66. }
  67. // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
  68. //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
  69. //{
  70. // builder.Services.AddOpenTelemetry()
  71. // .UseAzureMonitor();
  72. //}
  73. return builder;
  74. }
  75. public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder)
  76. {
  77. builder.Services.AddHealthChecks()
  78. // Add a default liveness check to ensure app is responsive
  79. .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
  80. return builder;
  81. }
  82. public static WebApplication MapDefaultEndpoints(this WebApplication app)
  83. {
  84. // Adding health checks endpoints to applications in non-development environments has security implications.
  85. // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
  86. if (app.Environment.IsDevelopment())
  87. {
  88. // All health checks must pass for app to be considered ready to accept traffic after starting
  89. app.MapHealthChecks("/health");
  90. // Only health checks tagged with the "live" tag must pass for app to be considered alive
  91. app.MapHealthChecks("/alive", new HealthCheckOptions
  92. {
  93. Predicate = r => r.Tags.Contains("live")
  94. });
  95. }
  96. return app;
  97. }
  98. }