diff --git a/eng/Signing.props b/eng/Signing.props index d11b1337b..3f4d20b41 100644 --- a/eng/Signing.props +++ b/eng/Signing.props @@ -1,3 +1,15 @@ - + + + + + + + + + + + + + diff --git a/eng/Versions.props b/eng/Versions.props index da0d421bd..470582df7 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -33,7 +33,13 @@ 7.0.2 2.3.0 - 8.2.2 - 8.2.2 + 9.1.0 + 9.1.0 + 1.9.0 + 1.9.0 + 1.9.0 + 1.9.0 + 1.9.0 + 9.0.0 diff --git a/src/Application/Extensions.cs b/src/Application/Extensions.cs new file mode 100644 index 000000000..f8e027f67 --- /dev/null +++ b/src/Application/Extensions.cs @@ -0,0 +1,107 @@ +using System.Net; +using System.Security.Cryptography; +using System.Security.Cryptography.X509Certificates; +using HealthChecks.ApplicationStatus.DependencyInjection; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using OpenTelemetry; +using OpenTelemetry.Exporter; +using OpenTelemetry.Logs; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; +using static System.Net.WebRequestMethods; + +namespace Microsoft.Extensions.Hosting; + +// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. +// This project should be referenced by each service project in your solution. +// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults +public static class Extensions +{ + public static TBuilder AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.ConfigureOpenTelemetry(); + + builder.AddDefaultHealthChecks(); + + builder.Services.AddServiceDiscovery(); + + return builder; + } + + public static TBuilder ConfigureOpenTelemetry(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation() + .SetExemplarFilter(ExemplarFilterType.TraceBased); + }) + .WithTracing(tracing => + { + tracing.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddOtlpExporter(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + + if (useOtlpExporter) + { + + if (string.Equals(Environment.GetEnvironmentVariable("YARP_UNSAFE_OLTP_CERT_ACCEPT_ANY_SERVER_CERTIFICATE"), "true", StringComparison.InvariantCultureIgnoreCase)) + { + // We cannot use UseOtlpExporter() since it doesn't support configuration via OtlpExporterOptions + // https://github.com/open-telemetry/opentelemetry-dotnet/issues/5802 + builder.Services.Configure(ConfigureOtlpExporterOptions); + builder.Services.AddOpenTelemetry() + .WithLogging(logging => logging.AddOtlpExporter()) + .WithMetrics(metrics => metrics.AddOtlpExporter()) + .WithTracing(tracing => tracing.AddOtlpExporter()); + } + else + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + } + + static void ConfigureOtlpExporterOptions(OtlpExporterOptions options) + { + options.HttpClientFactory = () => + { + var handler = new HttpClientHandler(); + handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; + var httpClient = new HttpClient(handler); + return httpClient; + }; + } + + return builder; + } + + public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Services.AddHealthChecks() + // Add a default liveness check on application status. + .AddApplicationStatus(tags: ["live"]); + + return builder; + } +} diff --git a/src/Application/Program.cs b/src/Application/Program.cs index bfbc7aeb1..292fc9e74 100644 --- a/src/Application/Program.cs +++ b/src/Application/Program.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; // Load configuration if (args.Length != 1) @@ -23,6 +24,7 @@ builder.Configuration.AddJsonFile(fileInfo.FullName, optional: false, reloadOnChange: true); // Configure YARP +builder.AddServiceDefaults(); builder.Services.AddServiceDiscovery(); builder.Services.AddReverseProxy() .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy")) diff --git a/src/Application/Yarp.Application.csproj b/src/Application/Yarp.Application.csproj index 64c247854..f29c6450d 100644 --- a/src/Application/Yarp.Application.csproj +++ b/src/Application/Yarp.Application.csproj @@ -16,6 +16,13 @@ + + + + + + +