public HealthCheckResponse Execute() { CompositeHealthCheckResult result = _healthCheckService.CheckHealthAsync().Result; bool success = result.CheckStatus == CheckStatus.Healthy; return(new HealthCheckResponse(success, result.Description)); }
/// <summary> /// the status policy enforcer /// </summary> /// <param name="result">the health check result</param> /// <returns>a status policy</returns> private IHttpActionResult EnforceStatusPolicyResult(CompositeHealthCheckResult result) { HttpStatusCode statusCode = HttpStatusCode.ServiceUnavailable; if (result.CheckStatus == CheckStatus.Healthy) { statusCode = HttpStatusCode.OK; } return(Content(statusCode, result)); }
private async Task GetChecksAsync(CancellationToken cancellationToken) { var timedTokenSource = new CancellationTokenSource(HealthCheckHandler.Timeout); var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timedTokenSource.Token); var stopwatch = Stopwatch.StartNew(); CheckResult = await GlobalHealthChecks.Service.CheckHealthAsync(linkedTokenSource.Token); ExecutionTime = stopwatch.Elapsed; }
public async Task <IHealthCheckResult> CheckMultiAsync() { var composite = new CompositeHealthCheckResult(PartiallyHealthyStatus); var httpClient = CreateHttpClient(); // REVIEW: Should these be done in parallel? foreach (var url in _urls) { await CheckUrlAsync(httpClient, url, (name, checkResult) => composite.Add(name, checkResult)).ConfigureAwait(false); } return(composite); }
private static Task WriteResponse(HttpContext httpContext, CompositeHealthCheckResult result) { httpContext.Response.ContentType = "application/json"; var json = new JObject( new JProperty("status", result.Status.ToString()), new JProperty("results", new JObject(result.Results.Select(pair => new JProperty(pair.Key, new JObject( new JProperty("status", pair.Value.Status.ToString()), new JProperty("description", pair.Value.Description), new JProperty("data", new JObject(pair.Value.Data.Select(p => new JProperty(p.Key, p.Value)))))))))); return(httpContext.Response.WriteAsync(json.ToString(Formatting.Indented))); }
public void Status_MatchesWorstStatusInResults(HealthCheckStatus statusValue) { var result = new CompositeHealthCheckResult(new Dictionary <string, HealthCheckResult>() { { "Foo", HealthCheckResult.Healthy() }, { "Bar", HealthCheckResult.Healthy() }, { "Baz", new HealthCheckResult(statusValue, exception: null, description: null, data: null) }, { "Quick", HealthCheckResult.Healthy() }, { "Quack", HealthCheckResult.Healthy() }, { "Quock", HealthCheckResult.Healthy() }, }); Assert.Equal(statusValue, result.Status); }
public void SetUp() { _description = _faker.Random.Words(); _mockHealthCheckService = new Mock <IHealthCheckService>(); CompositeHealthCheckResult compositeHealthCheckResult = new CompositeHealthCheckResult(CheckStatus.Healthy); compositeHealthCheckResult.Add("test", CheckStatus.Healthy, _description); _mockHealthCheckService.Setup(s => s.CheckHealthAsync(It.IsAny <CancellationToken>())) .ReturnsAsync(compositeHealthCheckResult); _classUnderTest = new DbHealthCheckUseCase(_mockHealthCheckService.Object); }
public async Task <IActionResult> GetStatus() { CompositeHealthCheckResult healthCheckResult = await _healthCheckService.CheckHealthAsync(); bool somethingIsWrong = healthCheckResult.CheckStatus != CheckStatus.Healthy; if (somethingIsWrong) { // healthCheckResult has a .Description property, but that shows the description of all health checks. // Including the successful ones, so let's filter those out var failedHealthCheckDescriptions = healthCheckResult.Results.Where(r => r.Value.CheckStatus != CheckStatus.Healthy) .Select(r => r.Value.Description) .ToList(); // return a 500 with JSON containing the Results of the Health Check return(new JsonResult(new { Status = healthCheckResult.CheckStatus, Errors = failedHealthCheckDescriptions }) { StatusCode = StatusCodes.Status503ServiceUnavailable }); } return(Ok(new { Status = healthCheckResult.CheckStatus })); }
public static Task WriteMinimalPlaintext(HttpContext httpContext, CompositeHealthCheckResult result) { httpContext.Response.ContentType = "text/plain"; return(httpContext.Response.WriteAsync(result.Status.ToString())); }