Skip to content

Commit

Permalink
Merge pull request #149 from SkillsFundingAgency/MF-295_HealthCheckWeb
Browse files Browse the repository at this point in the history
Mf 295 health check web
  • Loading branch information
SijiOdun authored Sep 17, 2019
2 parents 726e008 + 7848ad4 commit a04b3b3
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override async Task<string> Ping()
{
var pingUrl = _apiOptions.Value.ApiBaseUrl;

pingUrl += pingUrl.EndsWith("/") ? "ping" : "/ping";
pingUrl += pingUrl.EndsWith("/") ? "api/ping" : "/api/ping";

using (var client = new HttpClient())//not unit testable using directly
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using SFA.DAS.EAS.Account.Api.Client;
using SFA.DAS.Http;
using SFA.DAS.Reservations.Infrastructure.Extensions;

namespace SFA.DAS.Reservations.Infrastructure.HealthCheck
{
public class AccountApiHealthCheck : IHealthCheck
{
private const string HealthCheckResultDescription = "Account Api check";
private readonly IAccountApiClient _apiClient;
private readonly ILogger<AccountApiHealthCheck> _logger;

public AccountApiHealthCheck(
IAccountApiClient apiClient,
ILogger<AccountApiHealthCheck> logger)
{
_apiClient = apiClient;
_logger = logger;
}
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken())
{
_logger.LogInformation("Pinging Accounts API");

try
{
var timer = Stopwatch.StartNew();
await _apiClient.GetAccount(1);
timer.Stop();

var durationString = timer.Elapsed.ToHumanReadableString();

_logger.LogInformation($"Account API ping successful and took {durationString}");

return HealthCheckResult.Healthy(HealthCheckResultDescription,
new Dictionary<string, object> { { "Duration", durationString } });
}
catch (RestHttpClientException e)
{
_logger.LogWarning($"Account API ping failed : [Code: {e.StatusCode}] - {e.ReasonPhrase}");
return HealthCheckResult.Unhealthy(HealthCheckResultDescription, e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using SFA.DAS.Http;
using SFA.DAS.Reservations.Infrastructure.Api;
using SFA.DAS.Reservations.Infrastructure.Extensions;

namespace SFA.DAS.Reservations.Infrastructure.HealthCheck
{
public class CommitmentsApiHealthCheck : IHealthCheck
{
private const string HealthCheckResultDescription = "Commitments Api check";
private readonly CommitmentsApiClient _commitmentsApiClient;
private readonly ILogger<CommitmentsApiHealthCheck> _logger;

public CommitmentsApiHealthCheck(
CommitmentsApiClient commitmentsApiClient,
ILogger<CommitmentsApiHealthCheck> logger)
{
_logger = logger;
_commitmentsApiClient = commitmentsApiClient;
}

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken())
{
_logger.LogInformation("Pinging Commitments API");

try
{
var timer = Stopwatch.StartNew();
await _commitmentsApiClient.Ping();
timer.Stop();

var durationString = timer.Elapsed.ToHumanReadableString();

_logger.LogInformation($"Commitments API ping successful and took {durationString}");

return HealthCheckResult.Healthy(HealthCheckResultDescription,
new Dictionary<string, object> { { "Duration", durationString } });
}
catch (RestHttpClientException e)
{
_logger.LogWarning($"Commitments API ping failed : [Code: {e.StatusCode}] - {e.ReasonPhrase}");
return HealthCheckResult.Unhealthy(HealthCheckResultDescription, e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using SFA.DAS.Http;
using SFA.DAS.ProviderRelationships.Api.Client;
using SFA.DAS.ProviderRelationships.Types.Dtos;
using SFA.DAS.Reservations.Infrastructure.Extensions;

namespace SFA.DAS.Reservations.Infrastructure.HealthCheck
{
public class ProviderRelationshipsApiHealthCheck : IHealthCheck
{
private const string HealthCheckResultDescription = "ProviderRelationships Api check";
private readonly IProviderRelationshipsApiClient _apiClient;
private readonly ILogger<ProviderRelationshipsApiHealthCheck> _logger;

public ProviderRelationshipsApiHealthCheck(
IProviderRelationshipsApiClient apiClient,
ILogger<ProviderRelationshipsApiHealthCheck> logger)
{
_apiClient = apiClient;
_logger = logger;
}
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken())
{
_logger.LogInformation("Pinging ProviderRelationships API");

try
{
var timer = Stopwatch.StartNew();
await _apiClient.GetAccountProviderLegalEntitiesWithPermission(new GetAccountProviderLegalEntitiesWithPermissionRequest());
timer.Stop();

var durationString = timer.Elapsed.ToHumanReadableString();

_logger.LogInformation($"ProviderRelationships API ping successful and took {durationString}");

return HealthCheckResult.Healthy(HealthCheckResultDescription,
new Dictionary<string, object> { { "Duration", durationString } });
}
catch (RestHttpClientException e)
{
_logger.LogWarning($"ProviderRelationships API ping failed : [Code: {e.StatusCode}] - {e.ReasonPhrase}");
return HealthCheckResult.Unhealthy(HealthCheckResultDescription, e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

namespace SFA.DAS.Reservations.Infrastructure.HealthCheck
{
public class ApiHealthCheck : IHealthCheck
public class ReservationsApiHealthCheck : IHealthCheck
{
private const string HealthCheckResultDescription = "Reservation Api check";

private readonly IApiClient _apiClient;
private readonly ILogger<ApiHealthCheck> _logger;
private readonly ILogger<ReservationsApiHealthCheck> _logger;

public ApiHealthCheck(IApiClient apiClient, ILogger<ApiHealthCheck> logger)
public ReservationsApiHealthCheck(IApiClient apiClient, ILogger<ReservationsApiHealthCheck> logger)
{
_apiClient = apiClient;
_logger = logger;
Expand Down
16 changes: 14 additions & 2 deletions src/SFA.DAS.Reservations.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,22 @@ public void ConfigureServices(IServiceCollection services)
});

services.AddHealthChecks()
.AddCheck<ApiHealthCheck>(
.AddCheck<ReservationsApiHealthCheck>(
"Reservation Api",
failureStatus: HealthStatus.Unhealthy,
tags: new[] {"ready"});
tags: new[] {"ready"})
.AddCheck<CommitmentsApiHealthCheck>(
"Commitments Api",
failureStatus: HealthStatus.Unhealthy,
tags: new[] {"ready"})
.AddCheck<ProviderRelationshipsApiHealthCheck>(
"ProviderRelationships Api",
failureStatus: HealthStatus.Unhealthy,
tags: new[] {"ready"})
.AddCheck<AccountApiHealthCheck>(
"Accounts Api",
failureStatus: HealthStatus.Unhealthy,
tags: new[] { "ready" });

services.AddOptions();

Expand Down

0 comments on commit a04b3b3

Please sign in to comment.