From 6d23494fd368ee4132743da838e2d620d995cfd3 Mon Sep 17 00:00:00 2001 From: ThompsonNye Date: Sat, 28 Dec 2024 17:01:18 +0100 Subject: [PATCH] Add Aspire orchestration Therefore remove previous OpenTelemetry configuration and use the one provided in service defaults --- .../Constants.cs | 16 +++ .../Vegasco.Server.AppHost.Shared.csproj | 9 ++ src/Vegasco.Server.AppHost/Program.cs | 14 +++ .../Properties/launchSettings.json | 29 +++++ .../Vegasco.Server.AppHost.csproj | 24 ++++ .../appsettings.Development.json | 8 ++ src/Vegasco.Server.AppHost/appsettings.json | 9 ++ .../Extensions.cs | 119 ++++++++++++++++++ .../Vegasco.Server.ServiceDefaults.csproj | 22 ++++ src/WebApi/Common/Constants.cs | 2 - .../Common/DependencyInjectionExtensions.cs | 46 ++----- src/WebApi/Common/StartupExtensions.cs | 4 +- src/WebApi/WebApi.csproj | 6 + src/WebApi/appsettings.Development.json | 3 - vegasco-server.slnx | 3 + 15 files changed, 270 insertions(+), 44 deletions(-) create mode 100644 src/Vegasco.Server.AppHost.Shared/Constants.cs create mode 100644 src/Vegasco.Server.AppHost.Shared/Vegasco.Server.AppHost.Shared.csproj create mode 100644 src/Vegasco.Server.AppHost/Program.cs create mode 100644 src/Vegasco.Server.AppHost/Properties/launchSettings.json create mode 100644 src/Vegasco.Server.AppHost/Vegasco.Server.AppHost.csproj create mode 100644 src/Vegasco.Server.AppHost/appsettings.Development.json create mode 100644 src/Vegasco.Server.AppHost/appsettings.json create mode 100644 src/Vegasco.Server.ServiceDefaults/Extensions.cs create mode 100644 src/Vegasco.Server.ServiceDefaults/Vegasco.Server.ServiceDefaults.csproj diff --git a/src/Vegasco.Server.AppHost.Shared/Constants.cs b/src/Vegasco.Server.AppHost.Shared/Constants.cs new file mode 100644 index 0000000..d9a0f57 --- /dev/null +++ b/src/Vegasco.Server.AppHost.Shared/Constants.cs @@ -0,0 +1,16 @@ +namespace Vegasco.Server.AppHost.Shared; + +public static class Constants +{ + public static class Projects + { + public const string WebApiName = "webapi"; + } + + public static class Database + { + public const string ServiceName = "postgres"; + + public const string Name = "vegasco"; + } +} diff --git a/src/Vegasco.Server.AppHost.Shared/Vegasco.Server.AppHost.Shared.csproj b/src/Vegasco.Server.AppHost.Shared/Vegasco.Server.AppHost.Shared.csproj new file mode 100644 index 0000000..125f4c9 --- /dev/null +++ b/src/Vegasco.Server.AppHost.Shared/Vegasco.Server.AppHost.Shared.csproj @@ -0,0 +1,9 @@ + + + + net9.0 + enable + enable + + + diff --git a/src/Vegasco.Server.AppHost/Program.cs b/src/Vegasco.Server.AppHost/Program.cs new file mode 100644 index 0000000..bda0036 --- /dev/null +++ b/src/Vegasco.Server.AppHost/Program.cs @@ -0,0 +1,14 @@ +using Vegasco.Server.AppHost.Shared; + +var builder = DistributedApplication.CreateBuilder(args); + +var postgres = builder.AddPostgres(Constants.Database.ServiceName) + .WithDataVolume() + .AddDatabase(Constants.Database.Name); + +builder + .AddProject(Constants.Projects.WebApiName) + .WithReference(postgres) + .WaitFor(postgres); + +builder.Build().Run(); diff --git a/src/Vegasco.Server.AppHost/Properties/launchSettings.json b/src/Vegasco.Server.AppHost/Properties/launchSettings.json new file mode 100644 index 0000000..5a2b722 --- /dev/null +++ b/src/Vegasco.Server.AppHost/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17055;http://localhost:15102", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21122", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22235" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15102", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19222", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20257" + } + } + } +} diff --git a/src/Vegasco.Server.AppHost/Vegasco.Server.AppHost.csproj b/src/Vegasco.Server.AppHost/Vegasco.Server.AppHost.csproj new file mode 100644 index 0000000..c22dc1c --- /dev/null +++ b/src/Vegasco.Server.AppHost/Vegasco.Server.AppHost.csproj @@ -0,0 +1,24 @@ + + + + + + Exe + net9.0 + enable + enable + true + bb714834-9872-4af6-b154-0b98b14fcca2 + + + + + + + + + + + + + diff --git a/src/Vegasco.Server.AppHost/appsettings.Development.json b/src/Vegasco.Server.AppHost/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/src/Vegasco.Server.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/src/Vegasco.Server.AppHost/appsettings.json b/src/Vegasco.Server.AppHost/appsettings.json new file mode 100644 index 0000000..31c092a --- /dev/null +++ b/src/Vegasco.Server.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/src/Vegasco.Server.ServiceDefaults/Extensions.cs b/src/Vegasco.Server.ServiceDefaults/Extensions.cs new file mode 100644 index 0000000..13151bf --- /dev/null +++ b/src/Vegasco.Server.ServiceDefaults/Extensions.cs @@ -0,0 +1,119 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.ServiceDiscovery; +using OpenTelemetry; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +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(); + + builder.Services.ConfigureHttpClientDefaults(http => + { + // Turn on resilience by default + http.AddStandardResilienceHandler(); + + // Turn on service discovery by default + http.AddServiceDiscovery(); + }); + + // Uncomment the following to restrict the allowed schemes for service discovery. + // builder.Services.Configure(options => + // { + // options.AllowedSchemes = ["https"]; + // }); + + 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() + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) + //.AddGrpcClientInstrumentation() + .AddHttpClientInstrumentation(); + }); + + 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) + { + 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 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"]); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + // Adding health checks endpoints to applications in non-development environments has security implications. + // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. + if (app.Environment.IsDevelopment()) + { + // All health checks must pass for app to be considered ready to accept traffic after starting + app.MapHealthChecks("/health"); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks("/alive", new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + } + + return app; + } +} diff --git a/src/Vegasco.Server.ServiceDefaults/Vegasco.Server.ServiceDefaults.csproj b/src/Vegasco.Server.ServiceDefaults/Vegasco.Server.ServiceDefaults.csproj new file mode 100644 index 0000000..24b1b4f --- /dev/null +++ b/src/Vegasco.Server.ServiceDefaults/Vegasco.Server.ServiceDefaults.csproj @@ -0,0 +1,22 @@ + + + + net9.0 + enable + enable + true + + + + + + + + + + + + + + + diff --git a/src/WebApi/Common/Constants.cs b/src/WebApi/Common/Constants.cs index bc15221..c160ccc 100644 --- a/src/WebApi/Common/Constants.cs +++ b/src/WebApi/Common/Constants.cs @@ -2,8 +2,6 @@ public static class Constants { - public const string AppOtelName = "Vegasco.Api"; - public static class Authorization { public const string RequireAuthenticatedUserPolicy = "RequireAuthenticatedUser"; diff --git a/src/WebApi/Common/DependencyInjectionExtensions.cs b/src/WebApi/Common/DependencyInjectionExtensions.cs index 03a9261..7c7e8a4 100644 --- a/src/WebApi/Common/DependencyInjectionExtensions.cs +++ b/src/WebApi/Common/DependencyInjectionExtensions.cs @@ -1,10 +1,7 @@ using Asp.Versioning; using FluentValidation; using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; -using OpenTelemetry.Trace; -using System.Diagnostics; using Vegasco.WebApi.Authentication; using Vegasco.WebApi.Endpoints; using Vegasco.WebApi.Endpoints.OpenApi; @@ -20,15 +17,15 @@ public static class DependencyInjectionExtensions /// /// /// - public static void AddWebApiServices(this IServiceCollection services, IConfiguration configuration, IHostEnvironment environment) + public static void AddWebApiServices(this IHostApplicationBuilder builder) { - services + builder.Services .AddMiscellaneousServices() .AddOpenApi() .AddApiVersioning() - .AddOtel() - .AddAuthenticationAndAuthorization(environment) - .AddDbContext(configuration); + .AddAuthenticationAndAuthorization(builder.Environment); + + builder.AddDbContext(); } private static IServiceCollection AddMiscellaneousServices(this IServiceCollection services) @@ -98,26 +95,6 @@ public static class DependencyInjectionExtensions return services; } - private static IServiceCollection AddOtel(this IServiceCollection services) - { - Activity.DefaultIdFormat = ActivityIdFormat.W3C; - - ActivitySource activitySource = new(Constants.AppOtelName); - services.AddSingleton(activitySource); - - services.AddOpenTelemetry() - .WithTracing(t => - { - t.AddAspNetCoreInstrumentation() - .AddHttpClientInstrumentation() - .AddOtlpExporter() - .AddSource(activitySource.Name); - }) - .WithMetrics(); - - return services; - } - private static IServiceCollection AddAuthenticationAndAuthorization(this IServiceCollection services, IHostEnvironment environment) { services.AddOptions() @@ -153,16 +130,9 @@ public static class DependencyInjectionExtensions return services; } - private static IServiceCollection AddDbContext(this IServiceCollection services, IConfiguration configuration) + private static IHostApplicationBuilder AddDbContext(this IHostApplicationBuilder builder) { - services.AddDbContext(o => - { - o.UseNpgsql(configuration.GetConnectionString("Database"), c => - { - c.EnableRetryOnFailure(); - }); - }); - - return services; + builder.AddNpgsqlDbContext(Server.AppHost.Shared.Constants.Database.Name); + return builder; } } diff --git a/src/WebApi/Common/StartupExtensions.cs b/src/WebApi/Common/StartupExtensions.cs index 351875f..6b2a7fd 100644 --- a/src/WebApi/Common/StartupExtensions.cs +++ b/src/WebApi/Common/StartupExtensions.cs @@ -9,9 +9,11 @@ internal static class StartupExtensions { internal static WebApplication ConfigureServices(this WebApplicationBuilder builder) { + builder.AddServiceDefaults(); + builder.Configuration.AddEnvironmentVariables("Vegasco_"); - builder.Services.AddWebApiServices(builder.Configuration, builder.Environment); + builder.AddWebApiServices(); WebApplication app = builder.Build(); return app; diff --git a/src/WebApi/WebApi.csproj b/src/WebApi/WebApi.csproj index 4a838d9..8f2416f 100644 --- a/src/WebApi/WebApi.csproj +++ b/src/WebApi/WebApi.csproj @@ -13,6 +13,7 @@ + @@ -33,6 +34,11 @@ + + + + + diff --git a/src/WebApi/appsettings.Development.json b/src/WebApi/appsettings.Development.json index e66cc17..0c208ae 100644 --- a/src/WebApi/appsettings.Development.json +++ b/src/WebApi/appsettings.Development.json @@ -1,7 +1,4 @@ { - "ConnectionStrings": { - "Database": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=postgres" - }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/vegasco-server.slnx b/vegasco-server.slnx index ce8d932..5cc0fb0 100644 --- a/vegasco-server.slnx +++ b/vegasco-server.slnx @@ -6,6 +6,9 @@ + + +