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

Features/add raven db health check #77

Merged
merged 12 commits into from
Feb 4, 2019
7 changes: 7 additions & 0 deletions AspNetCore.Diagnostics.HealthChecks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HealthChecks.Consul", "src\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HealthChecks.Hangfire", "src\HealthChecks.Hangfire\HealthChecks.Hangfire.csproj", "{439DF9B7-5D0F-4531-BA19-F1BD01B244DB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HealthChecks.RavenDB", "src\HealthChecks.RavenDB\HealthChecks.RavenDB.csproj", "{E91924B0-BE65-4CF8-A43A-2F22EEFE53F2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -229,6 +231,10 @@ Global
{439DF9B7-5D0F-4531-BA19-F1BD01B244DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{439DF9B7-5D0F-4531-BA19-F1BD01B244DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{439DF9B7-5D0F-4531-BA19-F1BD01B244DB}.Release|Any CPU.Build.0 = Release|Any CPU
{E91924B0-BE65-4CF8-A43A-2F22EEFE53F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E91924B0-BE65-4CF8-A43A-2F22EEFE53F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E91924B0-BE65-4CF8-A43A-2F22EEFE53F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E91924B0-BE65-4CF8-A43A-2F22EEFE53F2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -268,6 +274,7 @@ Global
{B7C5C213-677D-4B03-ADA9-101299BCA047} = {092533AB-7505-4EDC-8932-D40BF575D0D2}
{6F2F1445-01C9-4CFD-9F41-BD95E4F96BA6} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
{439DF9B7-5D0F-4531-BA19-F1BD01B244DB} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
{E91924B0-BE65-4CF8-A43A-2F22EEFE53F2} = {2A3FD988-2BB8-43CF-B3A2-B70E648259D4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2B8C62A1-11B6-469F-874C-A02443256568}
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

[![Build status](https://ci.appveyor.com/api/projects/status/ldk031dvcn2no51g/branch/master?svg=true)](https://ci.appveyor.com/project/Xabaril/aspnetcore-diagnostics-healthchecks)
[![Build status](https://ci.appveyor.com/api/projects/status/ldk031dvcn2no51g/branch/master?svg=true)](https://ci.appveyor.com/project/Xabaril/aspnetcore-diagnostics-healthchecks)

[![Build history](https://buildstats.info/appveyor/chart/xabaril/aspnetcore-diagnostics-healthchecks)](https://ci.appveyor.com/project/xabaril/aspnetcore-diagnostics-healthchecks/history)

Expand All @@ -16,11 +16,12 @@ HealthChecks packages include health checks for:
- MySql
- Oracle
- Sqlite
- Postgres
- RavenDB
- Postgres
- EventStore
- RabbitMQ
- Elasticsearch
- Redis
- Redis
- System: Disk Storage, Private Memory, Virtual Memory
- Azure Service Bus: EventHub, Queue and Topics
- Azure Storage: Blob, Queue and Table
Expand Down Expand Up @@ -52,6 +53,7 @@ Install-Package AspNetCore.HealthChecks.AzureKeyVault
Install-Package AspNetCore.HealthChecks.MySql
Install-Package AspNetCore.HealthChecks.DocumentDb
Install-Package AspNetCore.HealthChecks.SqLite
Install-Package AspNetCore.HealthChecks.RavenDB
Install-Package AspNetCore.HealthChecks.Kafka
Install-Package AspNetCore.HealthChecks.RabbitMQ
Install-Package AspNetCore.HealthChecks.OpenIdConnectServer
Expand Down Expand Up @@ -85,7 +87,7 @@ public void ConfigureServices(IServiceCollection services)
.AddSqlServer(
connectionString: Configuration["Data:ConnectionStrings:Sql"],
healthQuery: "SELECT 1;",
name: "sql",
name: "sql",
failureStatus: HealthStatus.Degraded,
tags: new string[] { "db", "sql", "sqlserver" });
}
Expand Down Expand Up @@ -121,7 +123,7 @@ To integrate HealthChecks.UI in your project you just need to add the HealthChec

```csharp
public class Startup
{
{
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecksUI();
Expand All @@ -134,7 +136,7 @@ public class Startup
}
```

This automatically registers a new interface on **/healthchecks-ui** where the spa will be served.
This automatically registers a new interface on **/healthchecks-ui** where the spa will be served.

> Optionally, *UseHealthChecksUI* can be configured to serve it's health api, webhooks api and the front-end resources in different endpoints using the UseHealthChecksUI(setup =>) method overload. Default configured urls for this endpoints can be found [here](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks/blob/master/src/HealthChecks.UI/Configuration/Options.cs)

Expand Down Expand Up @@ -163,7 +165,7 @@ By default HealthChecks returns a simple Status Code (200 or 503) without the He

> *WriteHealthCheckUIResponse* is defined on HealthChecks.UI.Client nuget package.

To show these HealthChecks in HealthCheck-UI they have to be configured through the **HealthCheck-UI** settings.
To show these HealthChecks in HealthCheck-UI they have to be configured through the **HealthCheck-UI** settings.

```json
{
Expand Down
16 changes: 9 additions & 7 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ $buildSuffix = @{ $true = "$($suffix)-$($commitHash)"; $false = "$($branch)-$($c

echo "build: Tag is $tag"
echo "build: Package version suffix is $suffix"
echo "build: Build version suffix is $buildSuffix"
echo "build: Build version suffix is $buildSuffix"

exec { & dotnet build AspNetCore.Diagnostics.HealthChecks.sln -c Release --version-suffix=$buildSuffix -v q /nologo }

echo "Running unit tests"
echo "Running unit tests"

try {

Push-Location -Path .\test\UnitTests
exec { & dotnet test}
} finally {
Expand All @@ -58,7 +58,7 @@ echo "compose up done"
echo "Running functional tests"

try {

Push-Location -Path .\test\FunctionalTests
exec { & dotnet test}
} finally {
Expand Down Expand Up @@ -93,8 +93,9 @@ if ($suffix -eq "") {
exec { & dotnet pack .\src\HealthChecks.UI.Client\HealthChecks.UI.Client.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
exec { & dotnet pack .\src\HealthChecks.Publisher.ApplicationInsights\HealthChecks.Publisher.ApplicationInsights.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
exec { & dotnet pack .\src\HealthChecks.Publisher.Prometheus\HealthChecks.Publisher.Prometheus.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
exec { & dotnet pack .\src\HealthChecks.Consul\HealthChecks.Consul.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
}
exec { & dotnet pack .\src\HealthChecks.Consul\HealthChecks.Consul.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
exec { & dotnet pack .\src\HealthChecks.RavenDB\HealthChecks.RavenDB.csproj -c Release -o ..\..\artifacts --include-symbols --no-build }
}

else {
exec { & dotnet pack .\src\HealthChecks.SqlServer\HealthChecks.SqlServer.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
Expand Down Expand Up @@ -123,6 +124,7 @@ else {
exec { & dotnet pack .\src\HealthChecks.UI.Client\HealthChecks.UI.Client.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.Publisher.ApplicationInsights\HealthChecks.Publisher.ApplicationInsights.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.Publisher.Prometheus\HealthChecks.Publisher.Prometheus.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.Consul\HealthChecks.Consul.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.Consul\HealthChecks.Consul.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
exec { & dotnet pack .\src\HealthChecks.RavenDB\HealthChecks.RavenDB.csproj -c Release -o ..\..\artifacts --include-symbols --no-build --version-suffix=$suffix }
}

6 changes: 4 additions & 2 deletions build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
<MicrosoftDataSqlite>2.0.1</MicrosoftDataSqlite>
<StackExchangeRedis>2.0.513</StackExchangeRedis>
<RabbitMQClient>5.0.1</RabbitMQClient>
<RavenDBClient>4.1.3</RavenDBClient>
<OracleManagedDataAccessCore>2.18.3</OracleManagedDataAccessCore>
<Npgsql>3.2.6</Npgsql>
<MongoDBDriver>2.5.0</MongoDBDriver>
<MySqlConnector>0.40.4</MySqlConnector>
<ConfluentKafka>0.11.4</ConfluentKafka>
<SSHNet>2016.1.0</SSHNet>
<NEST>6.1.0</NEST>
<NEST>6.1.0</NEST>
<SystemBuffers>4.5.0</SystemBuffers>
<MicrosoftAzureDocumentDbCore>1.9.1</MicrosoftAzureDocumentDbCore>
<HangfireCore>1.6.21</HangfireCore>
Expand Down Expand Up @@ -72,7 +73,7 @@
<HealthCheckRedis>2.2.2</HealthCheckRedis>
<HealthCheckRabbitMQ>2.2.0</HealthCheckRabbitMQ>
<HealthCheckEventStore>2.2.0</HealthCheckEventStore>
<HealthCheckElasticsearch>2.2.1</HealthCheckElasticsearch>
<HealthCheckElasticsearch>2.2.1</HealthCheckElasticsearch>
<HealthCheckOracle>2.2.0</HealthCheckOracle>
<HealthCheckNpgSql>2.2.0</HealthCheckNpgSql>
<HealthCheckMongoDB>2.2.1</HealthCheckMongoDB>
Expand All @@ -93,5 +94,6 @@
<HealthCheckAWSS3>2.2.0</HealthCheckAWSS3>
<HealthCheckKeyVault>2.2.2</HealthCheckKeyVault>
<HealthCheckPublisherSeq>2.2.0</HealthCheckPublisherSeq>
<HealthCheckRavenDB>2.2.0</HealthCheckRavenDB>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using HealthChecks.RavenDB;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.Collections.Generic;

namespace Microsoft.Extensions.DependencyInjection
{
public static class HealthCheckBuilderExtensions
{
const string NAME = "ravendb";

/// <summary>
/// Add a health check for RabbitMQ services.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="rabbitMQConnectionString">The RabbitMQ connection string to be used.</param>
/// <param name="connectionString">Connection string to RavenDB.</param>
/// <param name="name">
/// The health check name. Optional. If <see langword="null"/> the type name 'ravendb' 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 <see langword="null"/> 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 AddRavenDB(
this IHealthChecksBuilder builder,
string connectionString,
string databaseName = default,
string name = default,
HealthStatus? failureStatus = default,
IEnumerable<string> tags = default)
=> builder.Add(new HealthCheckRegistration(
name ?? NAME,
sp => new RavenDBHealthCheck(connectionString, databaseName),
failureStatus,
tags));
}
}
25 changes: 25 additions & 0 deletions src/HealthChecks.RavenDB/HealthChecks.RavenDB.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>
<PackageTags>HealthCheck;Health;MongoDb</PackageTags>
<Description>HealthChecks.RavenDB is the health check package for RavenDB.</Description>
<Version>$(HealthCheckRavenDB)</Version>
<RepositoryUrl>$(RepositoryUrl)</RepositoryUrl>
<Company>$(Company)</Company>
<Authors>$(Authors)</Authors>
<LangVersion>latest</LangVersion>
<PackageId>AspNetCore.HealthChecks.RavenDB</PackageId>
<PublishRepositoryUrl>$(PublishRepositoryUrl)</PublishRepositoryUrl>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder)</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<ItemGroup>
<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>
<PackageReference Include="RavenDB.Client" Version="$(RavenDBClient)" />
</ItemGroup>
</Project>
57 changes: 57 additions & 0 deletions src/HealthChecks.RavenDB/RavenDBHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Raven.Client.Documents;
using Raven.Client.ServerWide.Operations;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace HealthChecks.RavenDB
{
/// <summary>
/// Health check for RavenDB.
/// </summary>
/// <seealso cref="Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck" />
public class RavenDBHealthCheck : IHealthCheck
{
private readonly string _connectionString;
private readonly string _specifiedDatabase;

public RavenDBHealthCheck(string connectionString, string databaseName = default)
{
_connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
_specifiedDatabase = databaseName;
CarlosLanderas marked this conversation as resolved.
Show resolved Hide resolved
}

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
using (var store = new DocumentStore
{
Urls = new string[] { _connectionString }
})
{
store.Initialize();
var databases = await store.Maintenance.Server.SendAsync(new GetDatabaseNamesOperation(0, 100));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the meaning of 0, 100 parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that the server sends the first 100 existing database names. I know that if it checks the database name and has more than 100 databases it can fail. But, according to my experience, it is unlikely that it has more than 100 databases under one server.


if (!string.IsNullOrWhiteSpace(_specifiedDatabase)
CarlosLanderas marked this conversation as resolved.
Show resolved Hide resolved
&& !databases.Contains(_specifiedDatabase, StringComparer.OrdinalIgnoreCase))
{
return new HealthCheckResult(
context.Registration.FailureStatus,
$"RavenDB doesn't contains '{_specifiedDatabase}' database.");
}
else
{
return HealthCheckResult.Healthy();
}
}
}
catch (Exception ex)
{
return new HealthCheckResult(context.Registration.FailureStatus, exception: ex);
}
}
}
}
1 change: 1 addition & 0 deletions test/FunctionalTests/FunctionalTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<ProjectReference Include="..\..\src\HealthChecks.Oracle\HealthChecks.Oracle.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.Publisher.Prometheus\HealthChecks.Publisher.Prometheus.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.RabbitMQ\HealthChecks.RabbitMQ.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.RavenDB\HealthChecks.RavenDB.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.Redis\HealthChecks.Redis.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.Sqlite\HealthChecks.Sqlite.csproj" />
<ProjectReference Include="..\..\src\HealthChecks.SqlServer\HealthChecks.SqlServer.csproj" />
Expand Down
Loading