mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 14:11:10 +00:00
Adds OpenTelemetry pipelines and health probes to the presentation layer
This commit is contained in:
1 parent
a2c99b1b79
commit
6af3d4f5e5
22 files changed
+800
-20
No files matched your search
+31
@@ -0,0 +1,31 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.HealthChecks;
|
||||
using Lumina.Presentation.Web.IntegrationTests.Common.Setup;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.IntegrationTests.Core.Health;
|
||||
|
||||
/// <summary>
|
||||
/// Factory whose API reachability health check always reports an unhealthy state.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class FailingApiHealthFactory : LuminaWebFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Configures the web host, replacing the API reachability health check with one that always fails.
|
||||
/// </summary>
|
||||
/// <param name="builder">The web host builder to configure.</param>
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
base.ConfigureWebHost(builder);
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
services.RemoveAll<ApiReachabilityHealthCheck>();
|
||||
services.AddScoped<ApiReachabilityHealthCheck, UnhealthyApiReachabilityHealthCheck>();
|
||||
});
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Fixtures.Common.TestHelpers;
|
||||
using Lumina.Presentation.Web.IntegrationTests.Common.Setup;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.IntegrationTests.Core.Health;
|
||||
|
||||
/// <summary>
|
||||
/// Contains integration tests for the <c>/health/ready</c> probe route when the API is unreachable.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class HealthEndpointFailureTests : IClassFixture<FailingApiHealthFactory>
|
||||
{
|
||||
private readonly FailingApiHealthFactory _apiFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HealthEndpointFailureTests"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiFactory">Injected Web application factory with a failing API reachability check.</param>
|
||||
public HealthEndpointFailureTests(FailingApiHealthFactory apiFactory)
|
||||
{
|
||||
_apiFactory = apiFactory;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HealthReady_WhenApiIsUnreachable_ShouldReturnServiceUnavailable()
|
||||
{
|
||||
// Arrange
|
||||
HttpClient client = WebTestHelpers.CreateAnonymousClient(_apiFactory);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await client.GetAsync("/health/ready");
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode);
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Fixtures.Common.TestHelpers;
|
||||
using Lumina.Presentation.Web.IntegrationTests.Common.Setup;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.IntegrationTests.Core.Health;
|
||||
|
||||
/// <summary>
|
||||
/// Contains integration tests for the <c>/health/live</c> and <c>/health/ready</c> probe routes.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class HealthEndpointTests : IClassFixture<LuminaWebFactory>
|
||||
{
|
||||
private readonly LuminaWebFactory _apiFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HealthEndpointTests"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiFactory">Injected Web application factory.</param>
|
||||
public HealthEndpointTests(LuminaWebFactory apiFactory)
|
||||
{
|
||||
_apiFactory = apiFactory;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HealthLive_WhenCalled_ShouldReturnOk()
|
||||
{
|
||||
// Arrange
|
||||
HttpClient client = WebTestHelpers.CreateAnonymousClient(_apiFactory);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await client.GetAsync("/health/live");
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HealthReady_WhenApiIsReachable_ShouldReturnHealthy()
|
||||
{
|
||||
// Arrange
|
||||
HttpClient client = WebTestHelpers.CreateAnonymousClient(_apiFactory);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await client.GetAsync("/health/ready");
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.Contains("Healthy", content, System.StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.DTO.Configuration;
|
||||
using Lumina.Presentation.Web.Common.HealthChecks;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.IntegrationTests.Core.Health;
|
||||
|
||||
/// <summary>
|
||||
/// API reachability health check that always reports an unhealthy state.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
internal sealed class UnhealthyApiReachabilityHealthCheck : ApiReachabilityHealthCheck
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UnhealthyApiReachabilityHealthCheck"/> class.
|
||||
/// </summary>
|
||||
/// <param name="httpClientFactory">Injected factory for creating HTTP clients.</param>
|
||||
/// <param name="serverConfigurationOptions">Injected server configuration application settings.</param>
|
||||
public UnhealthyApiReachabilityHealthCheck(IHttpClientFactory httpClientFactory, IOptionsSnapshot<ServerConfigurationDto> serverConfigurationOptions)
|
||||
: base(httpClientFactory, serverConfigurationOptions)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports an unhealthy state without probing the API.
|
||||
/// </summary>
|
||||
public override Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.FromResult(HealthCheckResult.Unhealthy("api unreachable"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user