Skip to content

Commit

Permalink
added dataForsyningenHealthCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsPilgaard committed Jun 11, 2023
1 parent 8e4fbd2 commit 73b6265
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/web/Server/Features/UserSearch/DataForsyningenHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Net;
using Jordnaer.Shared.UserSearch;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Jordnaer.Server.Features.UserSearch;

public class DataForsyningenHealthCheck : IHealthCheck
{
private readonly IDataForsyningenClient _dataForsyningenClient;
private readonly ILogger<DataForsyningenHealthCheck> _logger;

public DataForsyningenHealthCheck(IDataForsyningenClient dataForsyningenClient, ILogger<DataForsyningenHealthCheck> logger)
{
_dataForsyningenClient = dataForsyningenClient;
_logger = logger;
}

public async Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default)
{
var pingResult = await _dataForsyningenClient.Ping(cancellationToken);

if (pingResult.IsSuccessStatusCode)
{
return HealthCheckResult.Healthy();
}
if (pingResult.StatusCode is HttpStatusCode.TooManyRequests)
{
_logger.LogWarning("The {healthCheckName} has hit the rate limit.", nameof(DataForsyningenHealthCheck));
return HealthCheckResult.Healthy();
}
if (pingResult.Error is not null)
{
return new HealthCheckResult(HealthStatus.Degraded, pingResult.Error.Message, pingResult.Error);
}

return HealthCheckResult.Healthy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public static IServiceCollection AddUserSearchServices(this IServiceCollection s

services.AddDataForsyningenClient();

services.AddHealthChecks().AddCheck<DataForsyningenHealthCheck>("dataforsyningen", tags: new[] { "external", "api" });

return services;
}
}

0 comments on commit 73b6265

Please sign in to comment.