diff --git a/DysonNetwork.Pass/Rewind/AccountRewindService.cs b/DysonNetwork.Pass/Rewind/AccountRewindService.cs index e0854a0..d75410a 100644 --- a/DysonNetwork.Pass/Rewind/AccountRewindService.cs +++ b/DysonNetwork.Pass/Rewind/AccountRewindService.cs @@ -25,7 +25,10 @@ public class AccountRewindService( var httpClient = httpClientFactory.CreateClient( $"{nameof(AccountRewindService)}+{CapitalizeFirstLetter(serviceId)}" ); - var channel = GrpcChannel.ForAddress($"https://_grpc.{serviceId}", new GrpcChannelOptions { HttpClient = httpClient }); + var channel = GrpcChannel.ForAddress($"https://_grpc.{serviceId}", new GrpcChannelOptions + { + HttpClient = httpClient, + }); return new RewindService.RewindServiceClient(channel); } diff --git a/DysonNetwork.Shared/Extensions.cs b/DysonNetwork.Shared/Extensions.cs index 753b889..c1fc046 100644 --- a/DysonNetwork.Shared/Extensions.cs +++ b/DysonNetwork.Shared/Extensions.cs @@ -24,127 +24,129 @@ public static class Extensions private const string HealthEndpointPath = "/health"; private const string AlivenessEndpointPath = "/alive"; - public static TBuilder AddServiceDefaults(this TBuilder builder) - where TBuilder : IHostApplicationBuilder + extension(TBuilder builder) where TBuilder : IHostApplicationBuilder { - // Allow unencrypted grpc - AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); - - builder.ConfigureOpenTelemetry(); - - builder.AddDefaultHealthChecks(); - - builder.Services.AddServiceDiscovery(); - builder.Services.AddServiceDiscoveryCore(); - - builder.Services.ConfigureHttpClientDefaults(http => + public TBuilder AddServiceDefaults() { - // Turn on resilience by default - http.AddStandardResilienceHandler(); + // Allow unencrypted grpc + AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); - // Turn on service discovery by default - http.AddServiceDiscovery(); - }); + builder.ConfigureOpenTelemetry(); - builder.Services.AddSingleton(SystemClock.Instance); + builder.AddDefaultHealthChecks(); - builder.AddNatsClient("Queue"); - builder.AddRedisClient( - "Cache", - configureOptions: opts => + builder.Services.AddServiceDiscovery(); + builder.Services.AddServiceDiscoveryCore(); + + builder.Services.ConfigureHttpClientDefaults(http => { - opts.AbortOnConnectFail = false; - } - ); - - // Setup cache service - builder.Services.AddStackExchangeRedisCache(options => - { - options.Configuration = builder.Configuration.GetConnectionString("Cache"); - options.InstanceName = "dyson:"; - }); - builder.Services.AddSingleton(sp => - { - var mux = sp.GetRequiredService(); - return RedLockFactory.Create(new List { new(mux) }); - }); - builder.Services.AddSingleton(); - if (builder.Configuration.GetSection("Cache")["Serializer"] == "MessagePack") - builder.Services.AddSingleton(); - else - builder.Services.AddSingleton(); - - 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(); - }) - .WithTracing(tracing => - { - tracing - .AddSource(builder.Environment.ApplicationName) - .AddAspNetCoreInstrumentation(tracing => - // Exclude health check requests from tracing - tracing.Filter = context => - !context.Request.Path.StartsWithSegments(HealthEndpointPath) - && !context.Request.Path.StartsWithSegments(AlivenessEndpointPath) - ) - .AddGrpcClientInstrumentation() - .AddHttpClientInstrumentation(); + // Turn on resilience by default + http.AddStandardResilienceHandler(); + // Turn on service discovery by default + http.AddServiceDiscovery(); + // Ignore CA + http.ConfigurePrimaryHttpMessageHandler(sp => new HttpClientHandler + { + ServerCertificateCustomValidationCallback = (_, _, _, _) => true, + }); }); - builder.AddOpenTelemetryExporters(); + builder.Services.AddSingleton(SystemClock.Instance); - return builder; - } + builder.AddNatsClient("Queue"); + builder.AddRedisClient( + "Cache", + configureOptions: opts => + { + opts.AbortOnConnectFail = false; + } + ); - private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) - where TBuilder : IHostApplicationBuilder - { - var useOtlpExporter = !string.IsNullOrWhiteSpace( - builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"] - ); + // Setup cache service + builder.Services.AddStackExchangeRedisCache(options => + { + options.Configuration = builder.Configuration.GetConnectionString("Cache"); + }); + builder.Services.AddSingleton(sp => + { + var mux = sp.GetRequiredService(); + return RedLockFactory.Create(new List { new(mux) }); + }); + builder.Services.AddSingleton(); + if (builder.Configuration.GetSection("Cache")["Serializer"] == "MessagePack") + builder.Services.AddSingleton(); + else + builder.Services.AddSingleton(); - if (useOtlpExporter) - { - builder.Services.AddOpenTelemetry().UseOtlpExporter(); + return builder; } - // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) - //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) - //{ - // builder.Services.AddOpenTelemetry() - // .UseAzureMonitor(); - //} + public TBuilder ConfigureOpenTelemetry() + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); - return builder; - } + builder + .Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics + .AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + tracing + .AddSource(builder.Environment.ApplicationName) + .AddAspNetCoreInstrumentation(tracing => + // Exclude health check requests from tracing + tracing.Filter = context => + !context.Request.Path.StartsWithSegments(HealthEndpointPath) + && !context.Request.Path.StartsWithSegments(AlivenessEndpointPath) + ) + .AddGrpcClientInstrumentation() + .AddHttpClientInstrumentation(); + }); - public static TBuilder AddDefaultHealthChecks(this TBuilder builder) - where TBuilder : IHostApplicationBuilder - { - builder - .Services.AddHealthChecks() - // Add a default liveness check to ensure app is responsive - .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); + builder.AddOpenTelemetryExporters(); - return builder; + return builder; + } + + private TBuilder AddOpenTelemetryExporters() + { + var useOtlpExporter = !string.IsNullOrWhiteSpace( + builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"] + ); + + if (useOtlpExporter) + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) + //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) + //{ + // builder.Services.AddOpenTelemetry() + // .UseAzureMonitor(); + //} + + return builder; + } + + public TBuilder AddDefaultHealthChecks() + { + builder + .Services.AddHealthChecks() + // Add a default liveness check to ensure app is responsive + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); + + return builder; + } } public static WebApplication MapDefaultEndpoints(this WebApplication app) diff --git a/DysonNetwork.sln.DotSettings.user b/DysonNetwork.sln.DotSettings.user index d25500b..cc2f501 100644 --- a/DysonNetwork.sln.DotSettings.user +++ b/DysonNetwork.sln.DotSettings.user @@ -78,6 +78,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded