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 faea6ce
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 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
8 changes: 7 additions & 1 deletion src/HealthChecks.Elasticsearch/ElasticsearchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ElasticsearchOptions
public X509Certificate Certificate { get; private set; }
public bool AuthenticateWithBasicCredentials { get; private set; } = false;
public bool AuthenticateWithCertificate { get; private set; } = false;
public bool CheckClusterHealth { get; private set; } = false;
public Func<object, X509Certificate, X509Chain, SslPolicyErrors, bool> CertificateValidationCallback { get; private set; }
public ElasticsearchOptions UseBasicAuthentication(string name, string password)
{
Expand Down Expand Up @@ -44,5 +45,10 @@ public ElasticsearchOptions UseCertificateValidationCallback(Func<object, X509Ce
CertificateValidationCallback = callback;
return this;
}
public ElasticsearchOptions UseClusterHealth()
{
CheckClusterHealth = true;
return this;
}
}
}
}

0 comments on commit faea6ce

Please sign in to comment.