Skip to content

Commit a4e0470

Browse files
committed
Move to Aspire
1 parent 1558644 commit a4e0470

File tree

21 files changed

+422
-100
lines changed

21 files changed

+422
-100
lines changed

Directory.Packages.props

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
<PackageVersion Include="Amazon.Lambda.S3Events" Version="3.1.0" />
1717
<PackageVersion Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4" />
1818
<PackageVersion Include="Amazon.Lambda.SQSEvents" Version="2.2.0" />
19+
<PackageVersion Include="Aspire.Hosting" Version="9.4.0" />
20+
<PackageVersion Include="Aspire.Hosting.Testing" Version="9.4.0" />
1921
<PackageVersion Include="AWSSDK.Core" Version="4.0.0.2" />
2022
<PackageVersion Include="AWSSDK.SQS" Version="4.0.0.1" />
2123
<PackageVersion Include="AWSSDK.S3" Version="4.0.0.1" />
24+
<PackageVersion Include="Elastic.Aspire.Hosting.Elasticsearch" Version="9.3.0" />
2225
<PackageVersion Include="FakeItEasy" Version="8.3.0" />
2326
<PackageVersion Include="Elastic.Ingest.Elasticsearch" Version="0.11.3" />
2427
<PackageVersion Include="MartinCostello.Logging.XUnit.v3" Version="0.6.0" />
@@ -43,8 +46,8 @@
4346
<PackageVersion Include="DotNet.Glob" Version="3.1.3" />
4447
<PackageVersion Include="Errata" Version="0.14.0" />
4548
<PackageVersion Include="Github.Actions.Core" Version="9.0.0" />
46-
<PackageVersion Include="Microsoft.Extensions.Logging" Version="9.0.4" />
47-
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.4" />
49+
<PackageVersion Include="Microsoft.Extensions.Logging" Version="9.0.7" />
50+
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.7" />
4851
<PackageVersion Include="Markdig" Version="0.41.1" />
4952
<PackageVersion Include="NetEscapades.EnumGenerators" Version="1.0.0-beta12" PrivateAssets="all" ExcludeAssets="runtime" />
5053
<PackageVersion Include="Proc" Version="0.9.1" />
@@ -61,6 +64,17 @@
6164
</ItemGroup>
6265
<!-- Test packages -->
6366
<ItemGroup>
67+
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="9.7.0" />
68+
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="9.4.0" />
69+
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
70+
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
71+
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
72+
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
73+
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.12.0" />
74+
</ItemGroup>
75+
<!-- Test packages -->
76+
<ItemGroup>
77+
<PackageVersion Include="Aspire.Hosting.AppHost" Version="9.4.0" />
6478
<PackageVersion Include="AngleSharp.Diffing" Version="1.0.0" />
6579
<PackageVersion Include="DiffPlex" Version="1.7.2" />
6680
<PackageVersion Include="FluentAssertions" Version="7.2.0" />

docs-builder.sln

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests-integration", "tests-
127127
tests-integration\Directory.Build.props = tests-integration\Directory.Build.props
128128
EndProjectSection
129129
EndProject
130-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tests-integration\Elastic.Assembler.IntegrationTests", "tests-integration\Elastic.Assembler.IntegrationTests\Elastic.Assembler.IntegrationTests.csproj", "{A272D3EC-FAAF-4795-A796-302725382AFF}"
130+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Assembler.IntegrationTests", "tests-integration\Elastic.Assembler.IntegrationTests\Elastic.Assembler.IntegrationTests.csproj", "{A272D3EC-FAAF-4795-A796-302725382AFF}"
131+
EndProject
132+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Documentation.Aspire", "tests-integration\Elastic.Documentation.Aspire\Elastic.Documentation.Aspire.csproj", "{4DFECE72-4A1F-4B58-918E-DCD07B585231}"
133+
EndProject
134+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elastic.Documentation.ServiceDefaults", "src\Elastic.Documentation.ServiceDefaults\Elastic.Documentation.ServiceDefaults.csproj", "{2A83ED35-B631-4F02-8D4C-15611D0DB72C}"
131135
EndProject
132136
Global
133137
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -218,6 +222,14 @@ Global
218222
{A272D3EC-FAAF-4795-A796-302725382AFF}.Debug|Any CPU.Build.0 = Debug|Any CPU
219223
{A272D3EC-FAAF-4795-A796-302725382AFF}.Release|Any CPU.ActiveCfg = Release|Any CPU
220224
{A272D3EC-FAAF-4795-A796-302725382AFF}.Release|Any CPU.Build.0 = Release|Any CPU
225+
{4DFECE72-4A1F-4B58-918E-DCD07B585231}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
226+
{4DFECE72-4A1F-4B58-918E-DCD07B585231}.Debug|Any CPU.Build.0 = Debug|Any CPU
227+
{4DFECE72-4A1F-4B58-918E-DCD07B585231}.Release|Any CPU.ActiveCfg = Release|Any CPU
228+
{4DFECE72-4A1F-4B58-918E-DCD07B585231}.Release|Any CPU.Build.0 = Release|Any CPU
229+
{2A83ED35-B631-4F02-8D4C-15611D0DB72C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
230+
{2A83ED35-B631-4F02-8D4C-15611D0DB72C}.Debug|Any CPU.Build.0 = Debug|Any CPU
231+
{2A83ED35-B631-4F02-8D4C-15611D0DB72C}.Release|Any CPU.ActiveCfg = Release|Any CPU
232+
{2A83ED35-B631-4F02-8D4C-15611D0DB72C}.Release|Any CPU.Build.0 = Release|Any CPU
221233
EndGlobalSection
222234
GlobalSection(NestedProjects) = preSolution
223235
{4D198E25-C211-41DC-9E84-B15E89BD7048} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
@@ -249,5 +261,7 @@ Global
249261
{111E7029-BB29-4039-9B45-04776798A8DD} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
250262
{164F55EC-9412-4CD4-81AD-3598B57632A6} = {67B576EE-02FA-4F9B-94BC-3630BC09ECE5}
251263
{A272D3EC-FAAF-4795-A796-302725382AFF} = {BCAD38D5-6C83-46E2-8398-4BE463931098}
264+
{4DFECE72-4A1F-4B58-918E-DCD07B585231} = {BCAD38D5-6C83-46E2-8398-4BE463931098}
265+
{2A83ED35-B631-4F02-8D4C-15611D0DB72C} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
252266
EndGlobalSection
253267
EndGlobal
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using Actions.Core.Extensions;
6+
using Elastic.Documentation.Configuration;
7+
using Elastic.Documentation.Configuration.Versions;
8+
using Elastic.Documentation.Diagnostics;
9+
using Elastic.Documentation.ServiceDefaults.Logging;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.DependencyInjection.Extensions;
12+
using Microsoft.Extensions.Hosting;
13+
using Microsoft.Extensions.Logging;
14+
using Microsoft.Extensions.Logging.Console;
15+
16+
namespace Elastic.Documentation.ServiceDefaults;
17+
18+
public static class AppDefaultsExtensions
19+
{
20+
public static TBuilder AddAppDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
21+
{
22+
var args = Array.Empty<string>();
23+
return builder.AddAppDefaults(ref args);
24+
}
25+
public static TBuilder AddAppDefaults<TBuilder>(this TBuilder builder, LogLevel defaultLogLevel) where TBuilder : IHostApplicationBuilder
26+
{
27+
var args = Array.Empty<string>();
28+
return builder.AddAppDefaults(ref args, defaultLogLevel);
29+
}
30+
public static TBuilder AddAppDefaults<TBuilder>(this TBuilder builder, ref string[] args, Action<IServiceCollection, ConfigurationFileProvider> configure) where TBuilder : IHostApplicationBuilder =>
31+
builder.AddAppDefaults(ref args, null, configure);
32+
33+
public static TBuilder AddAppDefaults<TBuilder>(this TBuilder builder, ref string[] args, LogLevel? defaultLogLevel = null, Action<IServiceCollection, ConfigurationFileProvider>? configure = null) where TBuilder : IHostApplicationBuilder
34+
{
35+
var logLevel = defaultLogLevel ?? LogLevel.Information;
36+
ProcessCommandLineArguments(ref args, ref logLevel);
37+
38+
var services = builder.Services;
39+
_ = services.AddLogging(x => x
40+
.ClearProviders()
41+
.SetMinimumLevel(logLevel).AddConsole(c => c.FormatterName = "condensed")
42+
);
43+
44+
_ = services
45+
.AddGitHubActionsCore()
46+
.AddSingleton<DiagnosticsChannel>()
47+
.AddSingleton<DiagnosticsCollector>()
48+
.AddSingleton<ConfigurationFileProvider>()
49+
.AddConfigurationFileProvider((s, p) =>
50+
{
51+
_ = s.AddSingleton(p.CreateVersionConfiguration());
52+
configure?.Invoke(s, p);
53+
});
54+
services.TryAddEnumerable(ServiceDescriptor.Singleton<ConsoleFormatter, CondensedConsoleFormatter>());
55+
_ = services.AddLogging(x => x
56+
.ClearProviders()
57+
.SetMinimumLevel(logLevel)
58+
.AddConsole(c => c.FormatterName = "condensed")
59+
);
60+
61+
return builder;
62+
}
63+
64+
private static void ProcessCommandLineArguments(ref string[] args, ref LogLevel defaultLogLevel)
65+
{
66+
var newArgs = new List<string>();
67+
for (var i = 0; i < args.Length; i++)
68+
{
69+
if (args[i] == "--log-level")
70+
{
71+
if (args.Length > i + 1)
72+
defaultLogLevel = GetLogLevel(args[i + 1]);
73+
74+
i++;
75+
}
76+
else
77+
newArgs.Add(args[i]);
78+
}
79+
80+
args = [.. newArgs];
81+
}
82+
83+
private static LogLevel GetLogLevel(string? logLevel) => logLevel switch
84+
{
85+
"trace" => LogLevel.Trace,
86+
"debug" => LogLevel.Debug,
87+
"information" => LogLevel.Information,
88+
"info" => LogLevel.Information,
89+
"warning" => LogLevel.Warning,
90+
"error" => LogLevel.Error,
91+
"critical" => LogLevel.Critical,
92+
_ => LogLevel.Information
93+
};
94+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsAspireSharedProject>true</IsAspireSharedProject>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
12+
13+
<PackageReference Include="Microsoft.Extensions.Http.Resilience"/>
14+
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery"/>
15+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol"/>
16+
<PackageReference Include="OpenTelemetry.Extensions.Hosting"/>
17+
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore"/>
18+
<PackageReference Include="OpenTelemetry.Instrumentation.Http"/>
19+
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime"/>
20+
<PackageReference Include="Crayon"/>
21+
<PackageReference Include="GitHub.Actions.Core" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\Elastic.Documentation.Configuration\Elastic.Documentation.Configuration.csproj" />
26+
</ItemGroup>
27+
28+
</Project>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Diagnostics.HealthChecks;
9+
using Microsoft.Extensions.Hosting;
10+
using Microsoft.Extensions.Logging;
11+
using OpenTelemetry;
12+
using OpenTelemetry.Logs;
13+
using OpenTelemetry.Metrics;
14+
using OpenTelemetry.Trace;
15+
16+
namespace Elastic.Documentation.ServiceDefaults;
17+
18+
// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
19+
// This project should be referenced by each service project in your solution.
20+
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
21+
public static class Extensions
22+
{
23+
private const string HealthEndpointPath = "/health";
24+
private const string AlivenessEndpointPath = "/alive";
25+
26+
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
27+
{
28+
_ = builder
29+
.ConfigureOpenTelemetry()
30+
.AddDefaultHealthChecks();
31+
32+
_ = builder.Services
33+
.AddServiceDiscovery()
34+
.ConfigureHttpClientDefaults(http =>
35+
{
36+
_ = http.AddStandardResilienceHandler();
37+
_ = http.AddServiceDiscovery();
38+
});
39+
return builder;
40+
}
41+
42+
public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
43+
{
44+
_ = builder.Logging.AddOpenTelemetry(logging =>
45+
{
46+
logging.IncludeFormattedMessage = true;
47+
logging.IncludeScopes = true;
48+
});
49+
50+
_ = builder.Services.AddOpenTelemetry()
51+
.WithMetrics(metrics =>
52+
{
53+
_ = metrics.AddAspNetCoreInstrumentation()
54+
.AddHttpClientInstrumentation()
55+
.AddRuntimeInstrumentation();
56+
})
57+
.WithTracing(tracing =>
58+
{
59+
_ = tracing.AddSource(builder.Environment.ApplicationName)
60+
.AddAspNetCoreInstrumentation(tracing =>
61+
// Exclude health check requests from tracing
62+
tracing.Filter = context =>
63+
!context.Request.Path.StartsWithSegments(HealthEndpointPath)
64+
&& !context.Request.Path.StartsWithSegments(AlivenessEndpointPath)
65+
)
66+
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
67+
//.AddGrpcClientInstrumentation()
68+
.AddHttpClientInstrumentation();
69+
});
70+
71+
_ = builder.AddOpenTelemetryExporters();
72+
73+
return builder;
74+
}
75+
76+
private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
77+
{
78+
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
79+
80+
if (useOtlpExporter)
81+
_ = builder.Services.AddOpenTelemetry().UseOtlpExporter();
82+
83+
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
84+
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
85+
//{
86+
// builder.Services.AddOpenTelemetry()
87+
// .UseAzureMonitor();
88+
//}
89+
90+
return builder;
91+
}
92+
93+
public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
94+
{
95+
_ = builder.Services.AddHealthChecks()
96+
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
97+
98+
return builder;
99+
}
100+
101+
public static WebApplication MapDefaultEndpoints(this WebApplication app)
102+
{
103+
// Adding health checks endpoints to applications in non-development environments has security implications.
104+
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
105+
if (app.Environment.IsDevelopment())
106+
{
107+
// All health checks must pass for app to be considered ready to accept traffic after starting
108+
_ = app.MapHealthChecks(HealthEndpointPath);
109+
110+
// Only health checks tagged with the "live" tag must pass for app to be considered alive
111+
_ = app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions
112+
{
113+
Predicate = r => r.Tags.Contains("live")
114+
});
115+
}
116+
117+
return app;
118+
}
119+
}

src/tooling/Elastic.Documentation.Tooling/Logging/CondensedConsoleLogger.cs renamed to src/Elastic.Documentation.ServiceDefaults/Logging/CondensedConsoleLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Microsoft.Extensions.Logging.Console;
88
using static Crayon.Output;
99

10-
namespace Elastic.Documentation.Tooling.Logging;
10+
namespace Elastic.Documentation.ServiceDefaults.Logging;
1111

1212
public class CondensedConsoleFormatter() : ConsoleFormatter("condensed")
1313
{

0 commit comments

Comments
 (0)