|
| 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 Microsoft.Extensions.ServiceDiscovery; |
| 7 | +using OpenTelemetry; |
| 8 | +using OpenTelemetry.Logs; |
| 9 | +using OpenTelemetry.Metrics; |
| 10 | +using OpenTelemetry.Trace; |
| 11 | +using Serilog; |
| 12 | + |
| 13 | +namespace Microsoft.Extensions.Hosting |
| 14 | +{ |
| 15 | + // Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. |
| 16 | + // This project should be referenced by each service project in your solution. |
| 17 | + // To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults |
| 18 | + public static class Extensions |
| 19 | + { |
| 20 | + public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder) |
| 21 | + { |
| 22 | + builder.ConfigureOpenTelemetry(); |
| 23 | + |
| 24 | + builder.AddDefaultHealthChecks(); |
| 25 | + |
| 26 | + builder.Services.AddServiceDiscovery(); |
| 27 | + |
| 28 | + builder.Services.ConfigureHttpClientDefaults(http => |
| 29 | + { |
| 30 | + // Turn on resilience by default |
| 31 | + http.AddStandardResilienceHandler(); |
| 32 | + |
| 33 | + // Turn on service discovery by default |
| 34 | + http.AddServiceDiscovery(); |
| 35 | + }); |
| 36 | + |
| 37 | + // Uncomment the following to restrict the allowed schemes for service discovery. |
| 38 | + // builder.Services.Configure<ServiceDiscoveryOptions>(options => |
| 39 | + // { |
| 40 | + // options.AllowedSchemes = ["https"]; |
| 41 | + // }); |
| 42 | + |
| 43 | + return builder; |
| 44 | + } |
| 45 | + |
| 46 | + public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder) |
| 47 | + { |
| 48 | + builder.Logging.AddOpenTelemetry(logging => |
| 49 | + { // Use Serilog |
| 50 | + Log.Logger = new LoggerConfiguration() |
| 51 | + // 对请求路径为 / heathz和 / metrics不进行日志记录 |
| 52 | + .Filter.ByExcluding( |
| 53 | + e => e.Properties.TryGetValue("RequestPath", out var value) && (value.ToString().StartsWith("\"/metrics\"") || value.ToString().StartsWith("\"/healthz\"")) |
| 54 | + ) |
| 55 | +#if DEBUG |
| 56 | + .MinimumLevel.Information() |
| 57 | +#else |
| 58 | + .MinimumLevel.Warning() |
| 59 | +#endif |
| 60 | + .WriteTo.Console() |
| 61 | + .WriteTo.File("logs/log-.txt", |
| 62 | + shared: true, |
| 63 | + rollingInterval: RollingInterval.Day) |
| 64 | + //.WriteTo.OpenTelemetry(options => |
| 65 | + //{ |
| 66 | + // options.Endpoint = builder.Configuration["OpenTelemetry:Endpoint"]; |
| 67 | + // options.ResourceAttributes = new Dictionary<string, object> |
| 68 | + // { |
| 69 | + // ["service.name"] = builder.Configuration["OpenTelemetry:ServiceName"], |
| 70 | + // ["index"] = 10, |
| 71 | + // ["flag"] = true, |
| 72 | + // ["value"] = 3.14 |
| 73 | + // }; |
| 74 | + //}) |
| 75 | + .CreateLogger(); |
| 76 | + |
| 77 | + logging.IncludeFormattedMessage = true; |
| 78 | + logging.IncludeScopes = true; |
| 79 | + }); |
| 80 | + |
| 81 | + builder.Services.AddOpenTelemetry() |
| 82 | + .WithMetrics(metrics => |
| 83 | + { |
| 84 | + metrics.AddAspNetCoreInstrumentation() |
| 85 | + .AddHttpClientInstrumentation() |
| 86 | + .AddRuntimeInstrumentation(); |
| 87 | + }) |
| 88 | + .WithTracing(tracing => |
| 89 | + { |
| 90 | + tracing.AddAspNetCoreInstrumentation() |
| 91 | + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) |
| 92 | + //.AddGrpcClientInstrumentation() |
| 93 | + .AddHttpClientInstrumentation(); |
| 94 | + }); |
| 95 | + |
| 96 | + builder.AddOpenTelemetryExporters(); |
| 97 | + |
| 98 | + return builder; |
| 99 | + } |
| 100 | + |
| 101 | + private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder) |
| 102 | + { |
| 103 | + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); |
| 104 | + |
| 105 | + if (useOtlpExporter) |
| 106 | + { |
| 107 | + builder.Services.Configure<OpenTelemetryLoggerOptions>(logging => logging.AddOtlpExporter()); |
| 108 | + builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter()); |
| 109 | + builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter()); |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) |
| 114 | + //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) |
| 115 | + //{ |
| 116 | + // builder.Services.AddOpenTelemetry() |
| 117 | + // .UseAzureMonitor(); |
| 118 | + //} |
| 119 | + |
| 120 | + return builder; |
| 121 | + } |
| 122 | + |
| 123 | + public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder) |
| 124 | + { |
| 125 | + builder.Services.AddHealthChecks() |
| 126 | + // Add a default liveness check to ensure app is responsive |
| 127 | + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); |
| 128 | + |
| 129 | + return builder; |
| 130 | + } |
| 131 | + |
| 132 | + public static WebApplication MapDefaultEndpoints(this WebApplication app) |
| 133 | + { |
| 134 | + // Adding health checks endpoints to applications in non-development environments has security implications. |
| 135 | + // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. |
| 136 | + if (app.Environment.IsDevelopment()) |
| 137 | + { |
| 138 | + // All health checks must pass for app to be considered ready to accept traffic after starting |
| 139 | + app.MapHealthChecks("/health"); |
| 140 | + |
| 141 | + // Only health checks tagged with the "live" tag must pass for app to be considered alive |
| 142 | + app.MapHealthChecks("/alive", new HealthCheckOptions |
| 143 | + { |
| 144 | + Predicate = r => r.Tags.Contains("live") |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + return app; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments