Skip to content
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

[Feature] Add elacticsearch provider #8

Merged
merged 4 commits into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SQLSERVER_PASSWORD=Password12!
SQLSERVER_PORT=1833
ELASTICSEARCH_PORT=9200
REDIS_PORT=6379
POSTGRES_USER=postgres
POSTGRES_PASSWORD=Password12!
Expand Down
417 changes: 212 additions & 205 deletions AspNetCore.Diagnostics.HealthChecks.sln

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ HealthChecks packages include health checks for:
- Sqlite
- Postgres
- RabbitMQ
- Elasticsearch
- Redis
- System: Disk Storage, Private Memory, Virtual Memory
- Azure Service Bus: EventHub, Queue and Topics
Expand All @@ -36,6 +37,7 @@ Install-Package AspNetCore.HealthChecks.Network
Install-Package AspNetCore.HealthChecks.SqlServer
Install-Package AspNetCore.HealthChecks.MongoDb
Install-Package AspNetCore.HealthChecks.Npgsql
Install-Package AspNetCore.HealthChecks.Elasticsearch
Install-Package AspNetCore.HealthChecks.Redis
Install-Package AspNetCore.HealthChecks.AzureStorage
Install-Package AspNetCore.HealthChecks.AzureServiceBus
Expand Down
2 changes: 2 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ if ($suffix -eq "") {
exec { & dotnet pack .\src\HealthChecks.AzureStorage\HealthChecks.AzureStorage.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
exec { & dotnet pack .\src\HealthChecks.MySql\HealthChecks.MySql.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
exec { & dotnet pack .\src\HealthChecks.DocumentDb\HealthChecks.DocumentDb.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
exec { & dotnet pack .\src\HealthChecks.Elasticsearch\HealthChecks.Elasticsearch.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
exec { & dotnet pack .\src\HealthChecks.Sqlite\HealthChecks.Sqlite.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
exec { & dotnet pack .\src\HealthChecks.Kafka\HealthChecks.Kafka.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
exec { & dotnet pack .\src\HealthChecks.RabbitMQ\HealthChecks.RabbitMQ.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
Expand All @@ -87,6 +88,7 @@ else {
exec { & dotnet pack .\src\HealthChecks.AzureStorage\HealthChecks.AzureStorage.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.MySql\HealthChecks.MySql.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.DocumentDb\HealthChecks.DocumentDb.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.Elasticsearch\HealthChecks.Elasticsearch.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.Sqlite\HealthChecks.Sqlite.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.Kafka\HealthChecks.Kafka.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.AzureServiceBus\HealthChecks.AzureServiceBus.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
Expand Down
2 changes: 2 additions & 0 deletions build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<RabbitMQClient>5.0.1</RabbitMQClient>
<OracleManagedDataAccessCore>2.18.3</OracleManagedDataAccessCore>
<Npgsql>3.2.6</Npgsql>
<NEST>6.1.0</NEST>
<MongoDBDriver>2.5.0</MongoDBDriver>
<MySqlConnector>0.40.4</MySqlConnector>
<ConfluentKafka>0.11.4</ConfluentKafka>
Expand Down Expand Up @@ -61,6 +62,7 @@
<HealthCheckSqlite>2.2.0</HealthCheckSqlite>
<HealthCheckUri>2.2.0</HealthCheckUri>
<HealthCheckRedis>2.2.0</HealthCheckRedis>
<HealthCheckElasticsearch>2.2.0</HealthCheckElasticsearch>
<HealthCheckRabbitMQ>2.2.0</HealthCheckRabbitMQ>
<HealthCheckOracle>2.2.0</HealthCheckOracle>
<HealthCheckNpgSql>2.2.0</HealthCheckNpgSql>
Expand Down
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ services:
image: redis:alpine
ports:
- ${REDIS_PORT}:6379
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.3.2
ports:
- "9300:9300"
- ${ELASTICSEARCH_PORT}:9200
postgres:
image: postgres
environment:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using HealthChecks.Elasticsearch;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.Collections.Generic;

namespace Microsoft.Extensions.DependencyInjection
{
public static class ElasticsearchHealthCheckBuilderExtensions
{
const string NAME = "elasticsearch";

/// <summary>
/// Add a health check for Elasticsearch databases.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="elasticsearchUri">The Elasticsearchv connection string to be used.</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'mongodb' will be used for the name.</param>
/// <param name="failureStatus">
/// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then
/// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported.
/// </param>
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
/// <returns>The <see cref="IHealthChecksBuilder"/>.</returns></param>
public static IHealthChecksBuilder AddElasticsearch(this IHealthChecksBuilder builder, string elasticsearchUri, string name = default, HealthStatus? failureStatus = default, IEnumerable<string> tags = default)
{
return builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp => new ElasticsearchHealthCheck(elasticsearchUri),
failureStatus,
tags));
}
}
}
36 changes: 36 additions & 0 deletions src/HealthChecks.Elasticsearch/ElasticsearchHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Threading;
using System.Threading.Tasks;
using Nest;

namespace HealthChecks.Elasticsearch
{
public class ElasticsearchHealthCheck
: IHealthCheck
{
private readonly string _elasticsearchUri;
public ElasticsearchHealthCheck(string elasticsearchUri)
{
_elasticsearchUri = elasticsearchUri ?? throw new ArgumentNullException(nameof(elasticsearchUri));
}
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
var lowlevelClient = new ElasticClient(new Uri(_elasticsearchUri));
var pingResult = await lowlevelClient.PingAsync(cancellationToken: cancellationToken);
var isSuccess = pingResult.ApiCall.HttpStatusCode == 200;

return isSuccess
? HealthCheckResult.Healthy()
: HealthCheckResult.Unhealthy(pingResult.ApiCall.DebugInformation, pingResult.ApiCall.OriginalException);

}
catch (Exception ex)
{
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
}
}
}
25 changes: 25 additions & 0 deletions src/HealthChecks.Elasticsearch/HealthChecks.Elasticsearch.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(NetStandardTargetVersion)</TargetFramework>
<PackageLicenseUrl>$(PackageLicenseUrl)</PackageLicenseUrl>
<PackageProjectUrl>$(PackageProjectUrl)</PackageProjectUrl>
<Description>HealthChecks.Elasticsearch is the health check package for Elasticsearch.</Description>
<PackageTags>HealthCheck;Health;Elasticsearch</PackageTags>
<Version>$(HealthCheckElasticsearch)</Version>
<RepositoryUrl>$(RepositoryUrl)</RepositoryUrl>
<Company>$(Company)</Company>
<Authors>$(Authors)</Authors>
<LangVersion>latest</LangVersion>
<PackageId>AspNetCore.HealthChecks.Elasticsearch</PackageId>
<PublishRepositoryUrl>$(PublishRepositoryUrl)</PublishRepositoryUrl>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder)</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NEST" Version="$(Nest)" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="$(MicrosoftExtensionsDiagnosticsHealthChecks)" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63127-02">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion src/HealthChecks.UI/HealthChecks.UI.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(NetStandardTargetVersion)</TargetFramework>
<!--<TargetFramework>netcoreapp2.1</TargetFramework>-->
Expand All @@ -12,6 +12,7 @@
<Authors>$(Authors)</Authors>
<TypeScriptCompileBlocked>True</TypeScriptCompileBlocked>
<PackageId>AspNetCore.HealthChecks.UI</PackageId>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions test/FunctionalTests/FunctionalTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\HealthChecks.DynamoDb\HealthChecks.DynamoDb.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.Elasticsearch\HealthChecks.Elasticsearch.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.IdSvr\HealthChecks.IdSvr.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.Kafka\HealthChecks.Kafka.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.MongoDb\HealthChecks.MongoDb.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using FluentAssertions;
using FunctionalTests.Base;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net;
using System.Threading.Tasks;
using Xunit;

namespace FunctionalTests.HealthChecks.Elasticsearch
{
[Collection("execution")]
public class elasticsearch_healthcheck_should
{
private readonly ExecutionFixture _fixture;

public elasticsearch_healthcheck_should(ExecutionFixture fixture)
{
_fixture = fixture ?? throw new ArgumentNullException(nameof(fixture));
}

[SkipOnAppVeyor]
public async Task be_healthy_if_elasticsearch_is_available()
{
var connectionString = @"http://localhost:9200";

var webHostBuilder = new WebHostBuilder()
.UseStartup<DefaultStartup>()
.ConfigureServices(services =>
{
services.AddHealthChecks()
.AddElasticsearch(connectionString, tags: new string[] { "elasticsearch" });
})
.Configure(app =>
{
app.UseHealthChecks("/health", new HealthCheckOptions()
{
Predicate = r => r.Tags.Contains("elasticsearch")
});
});

var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health")
.GetAsync();

response.StatusCode
.Should().Be(HttpStatusCode.OK);
}

[SkipOnAppVeyor]
public async Task be_unhealthy_if_elasticsearch_is_not_available()
{
var webHostBuilder = new WebHostBuilder()
.UseStartup<DefaultStartup>()
.ConfigureServices(services =>
{
services.AddHealthChecks()
.AddRabbitMQ("nonexistingdomain:9200", tags: new string[] { "elasticsearch" });
})
.Configure(app =>
{
app.UseHealthChecks("/health", new HealthCheckOptions()
{
Predicate = r => r.Tags.Contains("elasticsearch")
});
});

var server = new TestServer(webHostBuilder);

var response = await server.CreateRequest($"/health")
.GetAsync();

response.StatusCode
.Should().Be(HttpStatusCode.ServiceUnavailable);
}
}
}