Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Aspire config #2871

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Marten.sln
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Helpdesk.Api", "samples\Hel
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Helpdesk.Api.Tests", "samples\Helpdesk\Helpdesk.Api.Tests\Helpdesk.Api.Tests.csproj", "{B0629D41-CA4E-4123-BC98-79A04D708A3E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Helpdesk.AppHost", "samples\Helpdesk\Helpdesk.AppHost\Helpdesk.AppHost.csproj", "{A171427B-510C-442F-8C34-F123707F3589}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Helpdesk.ServiceDefaults", "samples\Helpdesk\Helpdesk.ServiceDefaults\Helpdesk.ServiceDefaults.csproj", "{9361E77C-BCAE-42B7-AFFB-3D05FAEF3884}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -219,6 +223,14 @@ Global
{B0629D41-CA4E-4123-BC98-79A04D708A3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0629D41-CA4E-4123-BC98-79A04D708A3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0629D41-CA4E-4123-BC98-79A04D708A3E}.Release|Any CPU.Build.0 = Release|Any CPU
{A171427B-510C-442F-8C34-F123707F3589}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A171427B-510C-442F-8C34-F123707F3589}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A171427B-510C-442F-8C34-F123707F3589}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A171427B-510C-442F-8C34-F123707F3589}.Release|Any CPU.Build.0 = Release|Any CPU
{9361E77C-BCAE-42B7-AFFB-3D05FAEF3884}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9361E77C-BCAE-42B7-AFFB-3D05FAEF3884}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9361E77C-BCAE-42B7-AFFB-3D05FAEF3884}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9361E77C-BCAE-42B7-AFFB-3D05FAEF3884}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -255,5 +267,7 @@ Global
{F3DADE49-9C89-4F74-BCCE-7DAFD35675E9} = {79961196-DB50-4AD3-B749-D231799BCF2E}
{5E18DEBD-C768-4636-B167-7DA2A4954F43} = {F3DADE49-9C89-4F74-BCCE-7DAFD35675E9}
{B0629D41-CA4E-4123-BC98-79A04D708A3E} = {F3DADE49-9C89-4F74-BCCE-7DAFD35675E9}
{A171427B-510C-442F-8C34-F123707F3589} = {F3DADE49-9C89-4F74-BCCE-7DAFD35675E9}
{9361E77C-BCAE-42B7-AFFB-3D05FAEF3884} = {F3DADE49-9C89-4F74-BCCE-7DAFD35675E9}
EndGlobalSection
EndGlobal
2 changes: 2 additions & 0 deletions src/samples/Helpdesk/Helpdesk.Api/Helpdesk.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Npgsql" Version="8.0.0-preview.1.23557.2"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
<PackageReference Include="Confluent.Kafka" Version="2.3.0"/>
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0"/>
Expand All @@ -17,5 +18,6 @@
<ProjectReference Include="..\..\..\Marten.AspNetCore\Marten.AspNetCore.csproj"/>
<ProjectReference Include="..\..\..\Marten.CommandLine\Marten.CommandLine.csproj"/>
<ProjectReference Include="..\..\..\Marten\Marten.csproj"/>
<ProjectReference Include="..\Helpdesk.ServiceDefaults\Helpdesk.ServiceDefaults.csproj"/>
</ItemGroup>
</Project>
10 changes: 7 additions & 3 deletions src/samples/Helpdesk/Helpdesk.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@

var builder = WebApplication.CreateBuilder(args);

// Configure Aspire
builder
.AddServiceDefaults()
.AddNpgsqlDataSource("Incidents");
// Configure Aspire

builder.Services
.AddEndpointsApiExplorer()
Expand All @@ -39,8 +44,6 @@
var schemaName = Environment.GetEnvironmentVariable("SchemaName") ?? "Helpdesk";
options.Events.DatabaseSchemaName = schemaName;
options.DatabaseSchemaName = schemaName;
options.Connection(builder.Configuration.GetConnectionString("Incidents") ??
throw new InvalidOperationException());

options.UseDefaultSerialization(
EnumStorage.AsString,
Expand All @@ -63,7 +66,8 @@
})
.OptimizeArtifactWorkflow(TypeLoadMode.Static)
.UseLightweightSessions()
.AddAsyncDaemon(DaemonMode.Solo);
.AddAsyncDaemon(DaemonMode.Solo)
.UseNpgsqlDataSource("Incidents");

builder.Services
.AddCors(options =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"AllowedHosts": "*",
"ConnectionStrings": {
"Incidents": "Host=localhost;Port=5432;Database=marten_testing;Username=postgres;password=postgres;Command Timeout=5"
"Incidents": "Host=postgres;Port=5432;Database=marten_testing;Username=postgres;password=postgres;Command Timeout=5"
},
"KafkaProducer": {
"ProducerConfig": {
Expand Down
6 changes: 6 additions & 0 deletions src/samples/Helpdesk/Helpdesk.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"Aspire": {
"Npgsql": {
"HealthChecks": false,
"Tracing": true
}
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
19 changes: 19 additions & 0 deletions src/samples/Helpdesk/Helpdesk.AppHost/Helpdesk.AppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting" Version="8.0.0-preview.1.23557.2"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Helpdesk.Api\Helpdesk.Api.csproj"/>
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions src/samples/Helpdesk/Helpdesk.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Projects;

var builder = DistributedApplication.CreateBuilder(args);

var helpdeskApi = builder.AddProject<Helpdesk_Api>("helpdesk_api");

var app = builder.Build();

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15223",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16239"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions src/samples/Helpdesk/Helpdesk.AppHost/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
119 changes: 119 additions & 0 deletions src/samples/Helpdesk/Helpdesk.ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -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 OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace Microsoft.Extensions.Hosting;

public static class Extensions
{
public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
{
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.UseServiceDiscovery();
});

return builder;
}

public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder)
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});

builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddRuntimeInstrumentation()
.AddBuiltInMeters();
})
.WithTracing(tracing =>
{
if (builder.Environment.IsDevelopment())
{
// We want to view all traces in development
tracing.SetSampler(new AlwaysOnSampler());
}
tracing.AddAspNetCoreInstrumentation()
.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();
});

builder.AddOpenTelemetryExporters();

return builder;
}

private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder)
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);

if (useOtlpExporter)
{
builder.Services.Configure<OpenTelemetryLoggerOptions>(logging => logging.AddOtlpExporter());
builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter());
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter());
}

// Uncomment the following lines to enable the Prometheus exporter (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package)
// builder.Services.AddOpenTelemetry()
// .WithMetrics(metrics => metrics.AddPrometheusExporter());

// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.Exporter package)
// builder.Services.AddOpenTelemetry()
// .UseAzureMonitor();

return builder;
}

public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder)
{
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)
{
// Uncomment the following line to enable the Prometheus endpoint (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package)
// app.MapPrometheusScrapingEndpoint();

// 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;
}

private static MeterProviderBuilder AddBuiltInMeters(this MeterProviderBuilder meterProviderBuilder) =>
meterProviderBuilder.AddMeter(
"Microsoft.AspNetCore.Hosting",
"Microsoft.AspNetCore.Server.Kestrel",
"System.Net.Http");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net8.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="8.0.0" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="8.0.0-preview.1.23557.2" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0-alpha.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0-alpha.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.6.0-beta.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.6.0-beta.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.6.0-beta.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.5.1" />
</ItemGroup>

</Project>
Loading