|
| 1 | +namespace Microsoft.Extensions.Hosting; |
| 2 | + |
| 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 | + |
| 12 | +public static class Extensions |
| 13 | +{ |
| 14 | + public static IHostApplicationBuilder AddServiceDefaults( |
| 15 | + this IHostApplicationBuilder builder |
| 16 | + ) |
| 17 | + { |
| 18 | + builder.ConfigureOpenTelemetry(); |
| 19 | + |
| 20 | + builder.AddDefaultHealthChecks(); |
| 21 | + |
| 22 | + builder.Services.AddServiceDiscovery(); |
| 23 | + |
| 24 | + builder.Services.ConfigureHttpClientDefaults(http => |
| 25 | + { |
| 26 | + // Turn on resilience by default |
| 27 | + http.AddStandardResilienceHandler(); |
| 28 | + |
| 29 | + // Turn on service discovery by default |
| 30 | + http.AddServiceDiscovery(); |
| 31 | + }); |
| 32 | + |
| 33 | + return builder; |
| 34 | + } |
| 35 | + |
| 36 | + public static IHostApplicationBuilder ConfigureOpenTelemetry( |
| 37 | + this IHostApplicationBuilder builder |
| 38 | + ) |
| 39 | + { |
| 40 | + builder.Logging.AddOpenTelemetry(logging => |
| 41 | + { |
| 42 | + logging.IncludeFormattedMessage = true; |
| 43 | + logging.IncludeScopes = true; |
| 44 | + }); |
| 45 | + |
| 46 | + builder |
| 47 | + .Services.AddOpenTelemetry() |
| 48 | + .WithMetrics(metrics => |
| 49 | + { |
| 50 | + metrics |
| 51 | + .AddAspNetCoreInstrumentation() |
| 52 | + .AddHttpClientInstrumentation() |
| 53 | + .AddRuntimeInstrumentation(); |
| 54 | + }) |
| 55 | + .WithTracing(tracing => |
| 56 | + { |
| 57 | + tracing |
| 58 | + .AddAspNetCoreInstrumentation() |
| 59 | + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) |
| 60 | + //.AddGrpcClientInstrumentation() |
| 61 | + .AddHttpClientInstrumentation(); |
| 62 | + }); |
| 63 | + |
| 64 | + builder.AddOpenTelemetryExporters(); |
| 65 | + |
| 66 | + return builder; |
| 67 | + } |
| 68 | + |
| 69 | + private static IHostApplicationBuilder AddOpenTelemetryExporters( |
| 70 | + this IHostApplicationBuilder builder |
| 71 | + ) |
| 72 | + { |
| 73 | + var useOtlpExporter = !string.IsNullOrWhiteSpace( |
| 74 | + builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"] |
| 75 | + ); |
| 76 | + |
| 77 | + if (useOtlpExporter) |
| 78 | + { |
| 79 | + builder.Services.AddOpenTelemetry().UseOtlpExporter(); |
| 80 | + } |
| 81 | + |
| 82 | + return builder; |
| 83 | + } |
| 84 | + |
| 85 | + public static IHostApplicationBuilder AddDefaultHealthChecks( |
| 86 | + this IHostApplicationBuilder builder |
| 87 | + ) |
| 88 | + { |
| 89 | + builder |
| 90 | + .Services.AddHealthChecks() |
| 91 | + // Add a default liveness check to ensure app is responsive |
| 92 | + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); |
| 93 | + |
| 94 | + return builder; |
| 95 | + } |
| 96 | + |
| 97 | + public static WebApplication MapDefaultEndpoints(this WebApplication app) |
| 98 | + { |
| 99 | + if (app.Environment.IsDevelopment()) |
| 100 | + { |
| 101 | + // All health checks must pass for app to be considered ready to accept traffic after starting |
| 102 | + app.MapHealthChecks("/health"); |
| 103 | + |
| 104 | + // Only health checks tagged with the "live" tag must pass for app to be considered alive |
| 105 | + app.MapHealthChecks( |
| 106 | + "/alive", |
| 107 | + new HealthCheckOptions { Predicate = r => r.Tags.Contains("live") } |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + return app; |
| 112 | + } |
| 113 | +} |
0 commit comments