|
| 1 | +using System.Net; |
| 2 | +using System.Security.Cryptography; |
| 3 | +using System.Security.Cryptography.X509Certificates; |
| 4 | +using HealthChecks.ApplicationStatus.DependencyInjection; |
| 5 | +using Microsoft.AspNetCore.Builder; |
| 6 | +using Microsoft.AspNetCore.Diagnostics.HealthChecks; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | +using OpenTelemetry; |
| 10 | +using OpenTelemetry.Exporter; |
| 11 | +using OpenTelemetry.Logs; |
| 12 | +using OpenTelemetry.Metrics; |
| 13 | +using OpenTelemetry.Trace; |
| 14 | +using static System.Net.WebRequestMethods; |
| 15 | + |
| 16 | +namespace Microsoft.Extensions.Hosting; |
| 17 | + |
| 18 | +// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. |
| 19 | +// This project should be referenced by each service project in your solution. |
| 20 | +// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults |
| 21 | +public static class Extensions |
| 22 | +{ |
| 23 | + public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder |
| 24 | + { |
| 25 | + builder.ConfigureOpenTelemetry(); |
| 26 | + |
| 27 | + builder.AddDefaultHealthChecks(); |
| 28 | + |
| 29 | + builder.Services.AddServiceDiscovery(); |
| 30 | + |
| 31 | + return builder; |
| 32 | + } |
| 33 | + |
| 34 | + public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder |
| 35 | + { |
| 36 | + builder.Logging.AddOpenTelemetry(logging => |
| 37 | + { |
| 38 | + logging.IncludeFormattedMessage = true; |
| 39 | + logging.IncludeScopes = true; |
| 40 | + }); |
| 41 | + |
| 42 | + builder.Services.AddOpenTelemetry() |
| 43 | + .WithMetrics(metrics => |
| 44 | + { |
| 45 | + metrics.AddAspNetCoreInstrumentation() |
| 46 | + .AddHttpClientInstrumentation() |
| 47 | + .AddRuntimeInstrumentation() |
| 48 | + .SetExemplarFilter(ExemplarFilterType.TraceBased); |
| 49 | + }) |
| 50 | + .WithTracing(tracing => |
| 51 | + { |
| 52 | + tracing.AddAspNetCoreInstrumentation() |
| 53 | + .AddHttpClientInstrumentation() |
| 54 | + .AddOtlpExporter(); |
| 55 | + }); |
| 56 | + |
| 57 | + builder.AddOpenTelemetryExporters(); |
| 58 | + |
| 59 | + return builder; |
| 60 | + } |
| 61 | + |
| 62 | + private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder |
| 63 | + { |
| 64 | + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); |
| 65 | + |
| 66 | + if (useOtlpExporter) |
| 67 | + { |
| 68 | + |
| 69 | + if (string.Equals(Environment.GetEnvironmentVariable("YARP_UNSAFE_OLTP_CERT_ACCEPT_ANY_SERVER_CERTIFICATE"), "true", StringComparison.InvariantCultureIgnoreCase)) |
| 70 | + { |
| 71 | + // We cannot use UseOtlpExporter() since it doesn't support configuration via OtlpExporterOptions |
| 72 | + // https://github.com/open-telemetry/opentelemetry-dotnet/issues/5802 |
| 73 | + builder.Services.Configure<OtlpExporterOptions>(ConfigureOtlpExporterOptions); |
| 74 | + builder.Services.AddOpenTelemetry() |
| 75 | + .WithLogging(logging => logging.AddOtlpExporter()) |
| 76 | + .WithMetrics(metrics => metrics.AddOtlpExporter()) |
| 77 | + .WithTracing(tracing => tracing.AddOtlpExporter()); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + builder.Services.AddOpenTelemetry().UseOtlpExporter(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + static void ConfigureOtlpExporterOptions(OtlpExporterOptions options) |
| 86 | + { |
| 87 | + options.HttpClientFactory = () => |
| 88 | + { |
| 89 | + var handler = new HttpClientHandler(); |
| 90 | + handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; |
| 91 | + var httpClient = new HttpClient(handler); |
| 92 | + return httpClient; |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + return builder; |
| 97 | + } |
| 98 | + |
| 99 | + public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder |
| 100 | + { |
| 101 | + builder.Services.AddHealthChecks() |
| 102 | + // Add a default liveness check on application status. |
| 103 | + .AddApplicationStatus(tags: ["live"]); |
| 104 | + |
| 105 | + return builder; |
| 106 | + } |
| 107 | +} |
0 commit comments