Skip to content

Commit

Permalink
Add ElasticsearchOptions.UseClusterHealth option
Browse files Browse the repository at this point in the history
By default the option is false, meaning it will use ping as it was
previously doing. If the user wants advanced health check they can
call this method which will instruct ElasticsearchHealthCheck to use
the Cluster Health API instead. See parent commit for more information.
  • Loading branch information
inkel committed Oct 2, 2019
1 parent c2e6bea commit 123990f
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/HealthChecks.Elasticsearch/ElasticsearchHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,39 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
}
}

var healthResult = await lowLevelClient.Cluster.HealthAsync(ct: cancellationToken);

if (healthResult.ApiCall.HttpStatusCode != 200)
if (_options.CheckClusterHealth)
{
return new HealthCheckResult(context.Registration.FailureStatus);
}

switch (healthResult.Status.ToString())
{
case "green":
return HealthCheckResult.Healthy();
var healthResult = await lowLevelClient.Cluster.HealthAsync(ct: cancellationToken);

if (healthResult.ApiCall.HttpStatusCode != 200)
{
return new HealthCheckResult(context.Registration.FailureStatus);
}

case "yellow":
return HealthCheckResult.Degraded();
switch (healthResult.Status.ToString())
{
case "green":
return HealthCheckResult.Healthy();

case "yellow":
return HealthCheckResult.Degraded();

case "red":
return HealthCheckResult.Unhealthy();
}

case "red":
return HealthCheckResult.Unhealthy();
return HealthCheckResult.Unhealthy();
}
else
{
var pingResult = await lowLevelClient.PingAsync(ct: cancellationToken);
var isSuccess = pingResult.ApiCall.HttpStatusCode == 200;

return HealthCheckResult.Unhealthy();
return isSuccess
? HealthCheckResult.Healthy()
: new HealthCheckResult(context.Registration.FailureStatus);
}
}
catch (Exception ex)
{
Expand Down

0 comments on commit 123990f

Please sign in to comment.