Skip to content

Commit

Permalink
Add Elasticsearch Cluster Health Check (#1577)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefannikolei authored Dec 22, 2022
1 parent 84f5a58 commit 360c9ab
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/HealthChecks.Elasticsearch/ElasticsearchHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using Elasticsearch.Net;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Nest;

Expand Down Expand Up @@ -54,8 +55,25 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
}
}

if (_options.UseClusterHealthApi)
{
var healthResponse = await lowLevelClient.Cluster.HealthAsync(ct: cancellationToken);

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

return healthResponse.Status switch
{
Health.Green => HealthCheckResult.Healthy(),
Health.Yellow => HealthCheckResult.Degraded(),
_ => new HealthCheckResult(context.Registration.FailureStatus)
};
}

var pingResult = await lowLevelClient.PingAsync(ct: cancellationToken);
var isSuccess = pingResult.ApiCall.HttpStatusCode == 200;
bool isSuccess = pingResult.ApiCall.HttpStatusCode == 200;

return isSuccess
? HealthCheckResult.Healthy()
Expand Down
2 changes: 2 additions & 0 deletions src/HealthChecks.Elasticsearch/ElasticsearchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class ElasticsearchOptions

public bool AuthenticateWithApiKey { get; private set; }

public bool UseClusterHealthApi { get; set; }

public Func<object, X509Certificate, X509Chain, SslPolicyErrors, bool>? CertificateValidationCallback { get; private set; }

public TimeSpan? RequestTimeout { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace HealthChecks.Elasticsearch
public string? Password { get; }
public System.TimeSpan? RequestTimeout { get; set; }
public string Uri { get; }
public bool UseClusterHealthApi { get; set; }
public string? UserName { get; }
public HealthChecks.Elasticsearch.ElasticsearchOptions UseApiKey(Elasticsearch.Net.ApiKeyAuthenticationCredentials apiKey) { }
public HealthChecks.Elasticsearch.ElasticsearchOptions UseBasicAuthentication(string name, string password) { }
Expand Down

0 comments on commit 360c9ab

Please sign in to comment.