Skip to content
This repository has been archived by the owner on Jun 29, 2020. It is now read-only.

Add MySql health check #67

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion HealthChecks.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.9
VisualStudioVersion = 15.0.26730.12
MinimumVisualStudioVersion = 15.0.26228.4
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{E05DCF88-F916-4B61-A5DC-A8344C9E2429}"
EndProject
Expand All @@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNet.HealthChec
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleHealthChecker.AspNet", "samples\SampleHealthChecker.AspNet\SampleHealthChecker.AspNet.csproj", "{33FB5967-62C7-4230-B515-780EF63F748E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.HealthChecks.MySql", "src\Microsoft.Extensions.HealthChecks.MySql\Microsoft.Extensions.HealthChecks.MySql.csproj", "{29044153-D1A0-4B7E-9B1E-7587737506A0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -63,6 +65,10 @@ Global
{33FB5967-62C7-4230-B515-780EF63F748E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{33FB5967-62C7-4230-B515-780EF63F748E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{33FB5967-62C7-4230-B515-780EF63F748E}.Release|Any CPU.Build.0 = Release|Any CPU
{29044153-D1A0-4B7E-9B1E-7587737506A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{29044153-D1A0-4B7E-9B1E-7587737506A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{29044153-D1A0-4B7E-9B1E-7587737506A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29044153-D1A0-4B7E-9B1E-7587737506A0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -76,5 +82,9 @@ Global
{13BE838A-200B-4A68-8F58-3EA3BE3A1A8A} = {F9BA869A-7D5F-420F-9505-2D881F7934A7}
{2AE82E1C-6CE1-4755-A332-FA359B7CCE72} = {F9BA869A-7D5F-420F-9505-2D881F7934A7}
{33FB5967-62C7-4230-B515-780EF63F748E} = {E05DCF88-F916-4B61-A5DC-A8344C9E2429}
{29044153-D1A0-4B7E-9B1E-7587737506A0} = {F9BA869A-7D5F-420F-9505-2D881F7934A7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C19929B3-8E3C-4D58-9873-1EE86EF82CE1}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.HealthChecks\Microsoft.AspNetCore.HealthChecks.csproj" />
<ProjectReference Include="..\..\src\Microsoft.Extensions.HealthChecks.MySql\Microsoft.Extensions.HealthChecks.MySql.csproj" />
<ProjectReference Include="..\..\src\Microsoft.Extensions.HealthChecks\Microsoft.Extensions.HealthChecks.csproj" />
</ItemGroup>

Expand Down
3 changes: 2 additions & 1 deletion samples/SampleHealthChecker.AspNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public void ConfigureServices(IServiceCollection services)
)
.AddCheck("thrower", (Func<IHealthCheckResult>)(() => { throw new DivideByZeroException(); }))
.AddCheck("long-running", async cancellationToken => { await Task.Delay(10000, cancellationToken); return HealthCheckResult.Healthy("I ran too long"); })
.AddCheck<CustomHealthCheck>("custom");
.AddCheck<CustomHealthCheck>("custom")
.AddMySqlCheck("mysql", "MySql Connection String", TimeSpan.Zero);

/*
// add valid storage account credentials first
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using MySql.Data.MySqlClient;
using System;

namespace Microsoft.Extensions.HealthChecks
{
public static class HealthCheckBuilderMySqlServerExtensions
{
public static HealthCheckBuilder AddMySqlCheck(this HealthCheckBuilder builder, string name, string connectionString)
{
Guard.ArgumentNotNull(nameof(builder), builder);

return AddMySqlCheck(builder, name, connectionString, builder.DefaultCacheDuration);
}

public static HealthCheckBuilder AddMySqlCheck(this HealthCheckBuilder builder, string name, string connectionString, TimeSpan cacheDuration)
{
builder.AddCheck($"MySqlCheck({name})", async () =>
{
try
{
using (var connection = new MySqlConnection(connectionString))
{
connection.Open();
using (var cmd = new MySqlCommand("SHOW STATUS", connection))
{
var result = await cmd.ExecuteReaderAsync();
if(result.HasRows)
{
return HealthCheckResult.Healthy($"MySqlCheck({name}): Healthy");
}
return HealthCheckResult.Unhealthy($"MySqlCheck({name}): Unhealthy");
}
}
}
catch (Exception ex)
{
return HealthCheckResult.Unhealthy($"MySqlCheck({name}): Exception during check: {ex.GetType().FullName}");
}
}, cacheDuration);

return builder;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.3</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\common\Guard.cs" Link="Internal\Guard.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MySqlConnector" Version="0.28.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.Extensions.HealthChecks\Microsoft.Extensions.HealthChecks.csproj" />
</ItemGroup>

</Project>