-
Notifications
You must be signed in to change notification settings - Fork 803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace Nest to Elastic.Clients.Elasticsearch #2244
Changes from 2 commits
d185107
71dc2ed
3e50ff1
7c10eb4
e8971ec
7b374ae
80c51ce
ffd0b1e
3d3b0ac
4600380
ace64db
0e2cc0c
76e719a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,13 +1,13 @@ | ||||||||||
using System.Collections.Concurrent; | ||||||||||
using Elasticsearch.Net; | ||||||||||
using Elastic.Clients.Elasticsearch; | ||||||||||
using Elastic.Transport; | ||||||||||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||||||||||
using Nest; | ||||||||||
|
||||||||||
namespace HealthChecks.Elasticsearch; | ||||||||||
|
||||||||||
public class ElasticsearchHealthCheck : IHealthCheck | ||||||||||
{ | ||||||||||
private static readonly ConcurrentDictionary<string, ElasticClient> _connections = new(); | ||||||||||
private static readonly ConcurrentDictionary<string, ElasticsearchClient> _connections = new(); | ||||||||||
|
||||||||||
private readonly ElasticsearchOptions _options; | ||||||||||
|
||||||||||
|
@@ -21,9 +21,9 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context | |||||||||
{ | ||||||||||
try | ||||||||||
{ | ||||||||||
if (!_connections.TryGetValue(_options.Uri, out var lowLevelClient)) | ||||||||||
if (!_connections.TryGetValue(_options.Uri, out var elasticsearchClient)) | ||||||||||
{ | ||||||||||
var settings = new ConnectionSettings(new Uri(_options.Uri)); | ||||||||||
var settings = new ElasticsearchClientSettings(new Uri(_options.Uri)); | ||||||||||
|
||||||||||
if (_options.RequestTimeout.HasValue) | ||||||||||
{ | ||||||||||
|
@@ -32,49 +32,65 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context | |||||||||
|
||||||||||
if (_options.AuthenticateWithBasicCredentials) | ||||||||||
{ | ||||||||||
settings = settings.BasicAuthentication(_options.UserName, _options.Password); | ||||||||||
if (_options.UserName is null) | ||||||||||
{ | ||||||||||
throw new ArgumentNullException(nameof(_options.UserName)); | ||||||||||
} | ||||||||||
if (_options.Password is null) | ||||||||||
{ | ||||||||||
throw new ArgumentNullException(nameof(_options.Password)); | ||||||||||
} | ||||||||||
settings = settings.Authentication(new BasicAuthentication(_options.UserName, _options.Password)); | ||||||||||
} | ||||||||||
else if (_options.AuthenticateWithCertificate) | ||||||||||
{ | ||||||||||
if (_options.Certificate is null) | ||||||||||
{ | ||||||||||
throw new ArgumentNullException(nameof(_options.Certificate)); | ||||||||||
} | ||||||||||
settings = settings.ClientCertificate(_options.Certificate); | ||||||||||
} | ||||||||||
else if (_options.AuthenticateWithApiKey) | ||||||||||
{ | ||||||||||
settings = settings.ApiKeyAuthentication(_options.ApiKeyAuthenticationCredentials); | ||||||||||
if (_options.ApiKey is null) | ||||||||||
{ | ||||||||||
throw new ArgumentNullException(nameof(_options.ApiKey)); | ||||||||||
} | ||||||||||
settings.Authentication(new ApiKey(_options.ApiKey)); | ||||||||||
} | ||||||||||
|
||||||||||
if (_options.CertificateValidationCallback != null) | ||||||||||
{ | ||||||||||
settings = settings.ServerCertificateValidationCallback(_options.CertificateValidationCallback); | ||||||||||
} | ||||||||||
|
||||||||||
lowLevelClient = new ElasticClient(settings); | ||||||||||
elasticsearchClient = new ElasticsearchClient(settings); | ||||||||||
|
||||||||||
if (!_connections.TryAdd(_options.Uri, lowLevelClient)) | ||||||||||
if (!_connections.TryAdd(_options.Uri, elasticsearchClient)) | ||||||||||
{ | ||||||||||
lowLevelClient = _connections[_options.Uri]; | ||||||||||
elasticsearchClient = _connections[_options.Uri]; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not related to your specific changes, but I wonder: does
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not implement. |
||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
if (_options.UseClusterHealthApi) | ||||||||||
{ | ||||||||||
var healthResponse = await lowLevelClient.Cluster.HealthAsync(ct: cancellationToken).ConfigureAwait(false); | ||||||||||
var healthResponse = await elasticsearchClient.Cluster.HealthAsync(cancellationToken: cancellationToken).ConfigureAwait(false); | ||||||||||
|
||||||||||
if (healthResponse.ApiCall.HttpStatusCode != 200) | ||||||||||
if (healthResponse.ApiCallDetails.HttpStatusCode != 200) | ||||||||||
{ | ||||||||||
return new HealthCheckResult(context.Registration.FailureStatus); | ||||||||||
} | ||||||||||
|
||||||||||
return healthResponse.Status switch | ||||||||||
{ | ||||||||||
Health.Green => HealthCheckResult.Healthy(), | ||||||||||
Health.Yellow => HealthCheckResult.Degraded(), | ||||||||||
Elastic.Clients.Elasticsearch.HealthStatus.Green => HealthCheckResult.Healthy(), | ||||||||||
Elastic.Clients.Elasticsearch.HealthStatus.Yellow => HealthCheckResult.Degraded(), | ||||||||||
_ => new HealthCheckResult(context.Registration.FailureStatus) | ||||||||||
}; | ||||||||||
} | ||||||||||
|
||||||||||
var pingResult = await lowLevelClient.PingAsync(ct: cancellationToken).ConfigureAwait(false); | ||||||||||
bool isSuccess = pingResult.ApiCall.HttpStatusCode == 200; | ||||||||||
var pingResult = await elasticsearchClient.PingAsync(cancellationToken: cancellationToken).ConfigureAwait(false); | ||||||||||
bool isSuccess = pingResult.ApiCallDetails.HttpStatusCode == 200; | ||||||||||
|
||||||||||
return isSuccess | ||||||||||
? HealthCheckResult.Healthy() | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not related to your changes, but something that we should improve in the future:
From https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/recommendations.html:
So same as #2113 we should try to steer the users towards best practices and provide and recommend overloads that accept
ElasticsearchClient
and simply re-use it (to there would be one instance for the whole app, not one for health checks and another for the app itself).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamsitnik what about many health checks for different Elastic instances?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use the same approach we have for Azure health checks: by default a singleton instance is resolved from DI, but the users can provide an overload that solves it on their own.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Alirexaa I don't expect you to change it in this PR, this is just something to consider as a next improvement. And since we are going to introduce breaking changes now, we could simply introduce one more breaking change ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamsitnik, is the last change providing all the things we need?
I think we should make all the changes here.