63 lines
1.9 KiB
C#
63 lines
1.9 KiB
C#
// Copyright (c) StellaOps. Licensed under the AGPL-3.0-or-later.
|
|
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
using StellaOps.Eventing.Storage;
|
|
|
|
namespace StellaOps.Timeline.WebService.Endpoints;
|
|
|
|
/// <summary>
|
|
/// Health check endpoints.
|
|
/// </summary>
|
|
public static class HealthEndpoints
|
|
{
|
|
/// <summary>
|
|
/// Maps health check endpoints.
|
|
/// </summary>
|
|
public static void MapHealthEndpoints(this IEndpointRouteBuilder app)
|
|
{
|
|
app.MapHealthChecks("/health");
|
|
app.MapHealthChecks("/health/ready", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions
|
|
{
|
|
Predicate = check => check.Tags.Contains("ready")
|
|
});
|
|
app.MapHealthChecks("/health/live", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions
|
|
{
|
|
Predicate = check => check.Tags.Contains("live")
|
|
});
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Health check for timeline service.
|
|
/// </summary>
|
|
public sealed class TimelineHealthCheck : IHealthCheck
|
|
{
|
|
private readonly ITimelineEventStore _eventStore;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="TimelineHealthCheck"/> class.
|
|
/// </summary>
|
|
public TimelineHealthCheck(ITimelineEventStore eventStore)
|
|
{
|
|
_eventStore = eventStore;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<HealthCheckResult> CheckHealthAsync(
|
|
HealthCheckContext context,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
// Simple check - try to count events for a nonexistent correlation
|
|
// This validates database connectivity
|
|
await _eventStore.CountByCorrelationIdAsync("__health_check__", cancellationToken);
|
|
return HealthCheckResult.Healthy("Timeline service is healthy");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return HealthCheckResult.Unhealthy("Timeline service is unhealthy", ex);
|
|
}
|
|
}
|
|
}
|