Skip to content

Commit

Permalink
Add loging to health check.
Browse files Browse the repository at this point in the history
  • Loading branch information
BS-jiriceska committed Nov 1, 2024
1 parent 4133b61 commit 658483f
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ namespace DancingGoat.HealthChecks
internal class K13StoreApiHealthCheck : IHealthCheck
{
private readonly IHttpClientFactory httpClientFactory;
private readonly ILogger<K13StoreApiHealthCheck> logger;

public K13StoreApiHealthCheck(IHttpClientFactory httpClientFactory) => this.httpClientFactory = httpClientFactory;
public K13StoreApiHealthCheck(IHttpClientFactory httpClientFactory, ILogger<K13StoreApiHealthCheck> logger)
{
this.httpClientFactory = httpClientFactory;
this.logger = logger;
}

public async Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
Expand All @@ -15,8 +20,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(
try
{
using var httpClient = httpClientFactory.CreateClient(nameof(K13StoreApiHealthCheck));
var response = await httpClient.GetAsync("/status", cancellationToken);

var response = await httpClient.PostAsync("/api/store/auth/token", new StringContent("test"));
if (response.IsSuccessStatusCode)
{
return HealthCheckResult.Healthy("App is healthy.");
Expand All @@ -35,11 +39,13 @@ public async Task<HealthCheckResult> CheckHealthAsync(
string errorMessage = $"Request to K13 Store API failed with status code {(int)statusCode} ({statusCode}). " +
$"Reason: {reasonPhrase}. " +
$"Response: {responseBody}";
logger.LogError(errorMessage);
return HealthCheckResult.Unhealthy(errorMessage);
}
}
catch (HttpRequestException e)
{
logger.LogError(e, e.Message);
return HealthCheckResult.Unhealthy(exception: e);
}
}
Expand Down

0 comments on commit 658483f

Please sign in to comment.