-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from SkillsFundingAgency/MF-295_HealthCheckWeb
Mf 295 health check web
- Loading branch information
Showing
6 changed files
with
176 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/SFA.DAS.Reservations.Infrastructure/HealthCheck/AccountApiHealthCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/SFA.DAS.Reservations.Infrastructure/HealthCheck/CommitmentsApiHealthCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/SFA.DAS.Reservations.Infrastructure/HealthCheck/ProviderRelationshipsApiHealthCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters