Add Aspire orchestration
Some checks failed
continuous-integration/drone/push Build is failing

Therefore remove previous OpenTelemetry configuration and use the one
provided in service defaults
This commit is contained in:
2024-12-28 17:01:18 +01:00
parent bbac953660
commit 6d23494fd3
15 changed files with 270 additions and 44 deletions

View File

@@ -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";
}
}

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -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<Projects.WebApi>(Constants.Projects.WebApiName)
.WithReference(postgres)
.WaitFor(postgres);
builder.Build().Run();

View File

@@ -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"
}
}
}
}

View File

@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<Sdk Name="Aspire.AppHost.Sdk" Version="9.0.0" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>bb714834-9872-4af6-b154-0b98b14fcca2</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0" />
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="9.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Vegasco.Server.AppHost.Shared\Vegasco.Server.AppHost.Shared.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\WebApi\WebApi.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}

View File

@@ -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<TBuilder>(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<ServiceDiscoveryOptions>(options =>
// {
// options.AllowedSchemes = ["https"];
// });
return builder;
}
public static TBuilder ConfigureOpenTelemetry<TBuilder>(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<TBuilder>(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<TBuilder>(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;
}
}

View File

@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
</ItemGroup>
</Project>

View File

@@ -2,8 +2,6 @@
public static class Constants
{
public const string AppOtelName = "Vegasco.Api";
public static class Authorization
{
public const string RequireAuthenticatedUserPolicy = "RequireAuthenticatedUser";

View File

@@ -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
/// <param name="services"></param>
/// <param name="configuration"></param>
/// <param name="environment"></param>
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<JwtOptions>()
@@ -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<ApplicationDbContext>(o =>
{
o.UseNpgsql(configuration.GetConnectionString("Database"), c =>
{
c.EnableRetryOnFailure();
});
});
return services;
builder.AddNpgsqlDbContext<ApplicationDbContext>(Server.AppHost.Shared.Constants.Database.Name);
return builder;
}
}

View File

@@ -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;

View File

@@ -13,6 +13,7 @@
<ItemGroup>
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.11.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
@@ -33,6 +34,11 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Vegasco.Server.AppHost.Shared\Vegasco.Server.AppHost.Shared.csproj" />
<ProjectReference Include="..\Vegasco.Server.ServiceDefaults\Vegasco.Server.ServiceDefaults.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Nerdbank.GitVersioning" Version="3.7.112" />
</ItemGroup>

View File

@@ -1,7 +1,4 @@
{
"ConnectionStrings": {
"Database": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=postgres"
},
"Logging": {
"LogLevel": {
"Default": "Information",