-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
380aca9
commit acf7959
Showing
10 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Licensed to the Rapture Project under one or more agreements. | ||
// The Rapture Project licenses this file to you under the MIT license. | ||
|
||
using Azure.Monitor.OpenTelemetry.AspNetCore; | ||
using Microsoft.AspNetCore.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace Rapture.Web.Core; | ||
|
||
/// <summary> | ||
/// Core extension methods. | ||
/// </summary> | ||
public static class CoreExtensions | ||
{ | ||
/// <summary> | ||
/// Adds core configuration to a <see cref="IHostApplicationBuilder"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHostApplicationBuilder"/> to add configuration to.</param> | ||
/// <returns>The <see cref="IHostApplicationBuilder"/> to continue adding configuration to.</returns> | ||
public static IHostApplicationBuilder ConfigureCore(this IHostApplicationBuilder builder) | ||
{ | ||
builder.ConfigureTelemetry() | ||
.ConfigureHealthChecks() | ||
.ConfigureServiceDiscovery() | ||
.ConfigureHttpClient(); | ||
|
||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Adds core middleware to a <see cref="WebApplication"/>. | ||
/// </summary> | ||
/// <param name="app">The <see cref="WebApplication"/> to add middleware to.</param> | ||
/// <returns>The <see cref="WebApplication"/> to continue adding middleware to.</returns> | ||
public static WebApplication UseCore(this WebApplication app) | ||
{ | ||
app.UseHealthChecks(); | ||
|
||
return app; | ||
} | ||
|
||
private static IHostApplicationBuilder ConfigureTelemetry(this IHostApplicationBuilder builder) | ||
{ | ||
builder.Logging.AddOpenTelemetry(logging => | ||
{ | ||
logging.IncludeFormattedMessage = true; | ||
logging.IncludeScopes = true; | ||
}); | ||
|
||
builder.Services.AddOpenTelemetry() | ||
.WithMetrics(metrics => | ||
{ | ||
metrics.AddRuntimeInstrumentation() | ||
.AddAspNetCoreInstrumentation() | ||
.AddHttpClientInstrumentation(); | ||
}) | ||
.WithTracing(tracing => | ||
{ | ||
tracing.AddSource(builder.Environment.ApplicationName) | ||
.AddAspNetCoreInstrumentation() | ||
.AddHttpClientInstrumentation(); | ||
}); | ||
|
||
if (!string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"])) | ||
{ | ||
builder.Services.AddOpenTelemetry() | ||
.UseOtlpExporter(); | ||
} | ||
|
||
if (!string.IsNullOrWhiteSpace(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) | ||
{ | ||
builder.Services.AddOpenTelemetry() | ||
.UseAzureMonitor(); | ||
} | ||
|
||
return builder; | ||
} | ||
|
||
private static IHostApplicationBuilder ConfigureHealthChecks(this IHostApplicationBuilder builder) | ||
{ | ||
builder.Services.AddHealthChecks() | ||
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); | ||
|
||
return builder; | ||
} | ||
|
||
private static WebApplication UseHealthChecks(this WebApplication app) | ||
{ | ||
app.MapHealthChecks("/health"); | ||
|
||
app.MapHealthChecks("/alive", new HealthCheckOptions | ||
{ | ||
Predicate = r => r.Tags.Contains("live") | ||
}); | ||
|
||
return app; | ||
} | ||
|
||
private static IHostApplicationBuilder ConfigureServiceDiscovery(this IHostApplicationBuilder builder) | ||
{ | ||
builder.Services.AddServiceDiscovery(); | ||
|
||
return builder; | ||
} | ||
|
||
private static IHostApplicationBuilder ConfigureHttpClient(this IHostApplicationBuilder builder) | ||
{ | ||
builder.Services.ConfigureHttpClientDefaults(http => | ||
{ | ||
http.AddStandardResilienceHandler(); | ||
http.AddServiceDiscovery(); | ||
}); | ||
|
||
return builder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Licensed to the Rapture Project under one or more agreements. | ||
// The Rapture Project licenses this file to you under the MIT license. | ||
|
||
using Rapture.Web.Core; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.ConfigureCore(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseCore(); | ||
|
||
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"Rapture.Web": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" /> | ||
<PackageReference Include="Microsoft.Extensions.Http.Resilience" /> | ||
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" /> | ||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" /> | ||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" /> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" /> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.Http" /> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |