Skip to content

Commit

Permalink
feat!: Use of configration and registration refactored (#77)
Browse files Browse the repository at this point in the history
* feat: HealthCheck name prefix removed and configuration usage simplified

* chore: Added local build options

* chore: Removed `NetEvolve.Extensions.Strings` from `Directory.Packages.props`

* fix: Missing `_TestTargetFrameworks`

* docs: Updated Package README`s

* docs: Minor changes
  • Loading branch information
samtrion authored Oct 9, 2023
1 parent 9edec68 commit 229470a
Show file tree
Hide file tree
Showing 88 changed files with 228 additions and 305 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PropertyGroup Label="General settings for all projects">
<_ProjectTargetFrameworks>net6.0;net7.0;net8.0</_ProjectTargetFrameworks>
<_TestTargetFrameworks>net6.0;net7.0;net8.0</_TestTargetFrameworks>
<_TestTargetFrameworks Condition=" '$(BuildingInsideVisualStudio)' == 'true' ">net8.0</_TestTargetFrameworks>
</PropertyGroup>

<PropertyGroup Label="Settings for non test projects" Condition=" !$(IsTestableProject) ">
Expand Down
5 changes: 2 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project>
<Import Project="$(DirEngineeringSettings)sln.packages.props" />
<Import Project="$(MSBuildThisFileDirectory)Local.packages.props" Condition=" Exists('$(MSBuildThisFileDirectory)Local.packages.props') " />
<ItemGroup>
<GlobalPackageReference Include="CSharpier.MSBuild" Version="0.25.0" />
<GlobalPackageReference Include="GitVersion.MsBuild" Version="5.12.0" />
<GlobalPackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.4" />
<GlobalPackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.4" />
<GlobalPackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<GlobalPackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.7.30" />
<GlobalPackageReference Include="SonarAnalyzer.CSharp" Version="9.11.0.78383" Condition=" '$(BuildingInsideVisualStudio)' == 'true' " />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
Expand All @@ -21,7 +21,6 @@
<PackageVersion Include="MySql.Data" Version="8.1.0" />
<PackageVersion Include="MySqlConnector" Version="2.2.7" />
<PackageVersion Include="NetEvolve.Arguments" Version="1.0.8" />
<PackageVersion Include="NetEvolve.Extensions.Strings" Version="1.2.2" />
<PackageVersion Include="NetEvolve.Extensions.Tasks" Version="1.1.6" />
<PackageVersion Include="NetEvolve.Extensions.XUnit" Version="1.1.95" />
<PackageVersion Include="Npgsql" Version="7.0.6" />
Expand All @@ -38,4 +37,4 @@
<PackageVersion Include="xunit.runner.console" Version="2.5.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.1" />
</ItemGroup>
</Project>
</Project>
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
## What is HealthChecks?
This is a mono repository for several NuGet packages based on the [Microsoft.Extensions.Diagnostics.HealthChecks](https://www.nuget.org/packages/Microsoft.Extensions.Diagnostics.HealthChecks) package. The main goal of this repository is to provide a set of health checks for different services and frameworks, which are fully configurable either via code or configuration.

## What is the difference between this repository and the [AspNetCore.Diagnostics.HealthChecks](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks) repository?
The main difference is that we try to focus on delivering packages, which are fully configurable via code and configuration. This means that you can configure the health checks in your `Program.cs` file or in your `appsettings.json` file, or any other configuration provider.
### What is the difference between this repository and the [AspNetCore.Diagnostics.HealthChecks](https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks) repository?
The main difference is that we try to focus on providing packages that are fully configurable via code or configuration. This means that you can configure the health checks in your `Program.cs` file, or in your `appsettings.json` file, or in any other configuration provider. In some cases, we provide the same healthcheck for a service with an alternative implementation. For example, we provide a healthcheck for MySql that is based on `MySql.Data` and one that is based on `MySqlConnector`. This allows you to choose the implementation that best suits your needs or fits your existing dependencies.
In addition, we try to support the latest LTS and STS versions of .NET ([.NET Support Policy](https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core)) as well as the latest preview version of .NET for at least 3 years, but we **can't guarantee** this. This depends on the support of related NuGet packages and the .NET platform itself. See the [Supported .NET Version](#supported-net-version) section for more details.

## Available NuGet packages
<!-- packages:start -->
Expand All @@ -33,12 +34,15 @@ The main difference is that we try to focus on delivering packages, which are fu
## Supported .NET version
We try to support the latest LTS and STS versions of .NET ([.NET Support Policy](https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core)), as well as the latest preview version of .NET. We will try to support each framework version for at least 3 years, but we can't guarantee it. This depends on the support of related NuGet packages and the .NET platform itself.

| .NET Version | Supported | Until |
|-------------------|:----------------------:|:--------------------:|
| .NET 6.0 | :heavy_check_mark: Yes | :bangbang: Dez. 2024 |
| .NET 7.0 | :heavy_check_mark: Yes | |
| .NET 8.0 | :heavy_check_mark: Yes | |
| .NET Version | Supported | Until |
|--------------|:----------------------:|:--------------------------------:|
| .NET 6.0 | :heavy_check_mark: Yes | :exclamation: Earliest Dec. 2024 |
| .NET 7.0 | :heavy_check_mark: Yes | |
| .NET 8.0 | :heavy_check_mark: Yes | |

Why have we decided to this approach? Because we want to be able to use the latest features of the .NET platform,
and the performance gains that come with it. We know that not every of our NuGet packages will gain performance
from this, but this is our general strategy and nobody knows what the future will bring.
Why did we choose this approach? Because we want to be able to take advantage of the latest language features of the .NET platform and the performance gains that come with them. We know that not all of our NuGet packages will gain performance from this, but this is our general strategy and nobody knows what the future will bring.

### Why not .NET Standard?
With the .NET Standard Microsoft created a specification for APIs that are intended to be available on all .NET implementations. This was a great idea, but it also has some drawbacks. The main drawback is that the .NET Standard is a specification and not an implementation. This means that the real work is done by .NET implementations, such as .NET 5.0 and later versions. Which is why we decided us against the .NET Standard and for the concrete .NET implementations.

See [The future of .NET Standard](https://devblogs.microsoft.com/dotnet/the-future-of-net-standard/) for more details.
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,18 @@ params string[] tags
.ConfigureOptions<MySqlConfigure>();
}

var internalName = name.EnsureStartsWith("MySql", StringComparison.OrdinalIgnoreCase);

if (builder.IsNameAlreadyUsed(internalName))
if (builder.IsNameAlreadyUsed(name))
{
throw new ArgumentException($"Name `{name}` already in use.", nameof(name), null);
}

if (options is not null)
{
_ = builder.Services.Configure(internalName, options);
_ = builder.Services.Configure(name, options);
}

return builder.AddCheck<MySqlCheck>(
internalName,
name,
HealthStatus.Unhealthy,
new[] { "mysql", "database" }.Union(tags, StringComparer.OrdinalIgnoreCase)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal sealed class MySqlConfigure
public void Configure(string? name, MySqlOptions options)
{
Argument.ThrowIfNullOrWhiteSpace(name);
_configuration.Bind($"HealthChecks:{name}", options);
_configuration.Bind($"HealthChecks:MySql:{name}", options);
}

public void Configure(MySqlOptions options) => Configure(Options.DefaultName, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<ItemGroup>
<PackageReference Include="MySqlConnector" />
<PackageReference Include="NetEvolve.Arguments" />
<PackageReference Include="NetEvolve.Extensions.Strings" />
<PackageReference Include="NetEvolve.Extensions.Tasks" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,10 @@ using NetEvolve.HealthChecks.MySql.Connector;
```
Therefor you can use two different approaches. In both approaches you have to provide a name for the health check.

:heavy_exclamation_mark: Please be aware that the name is prefixed by `MySql` in the health check. The prefixed Name will be used to identify the configuration object.

**Examples:**

| Name | Prefixed Name |
| --------------- | ---------------------- |
| `MySqlServer` | `MySqlServer` |
| `1` | `MySql1` |
| `Test` | `MySqlTest` |

:heavy_exclamation_mark: The configuration of this package is compatible with the [NetEvolve.HealthChecks.MySql](https://www.nuget.org/packages/NetEvolve.HealthChecks.MySql/) package.

### Parameters
- `name`: The name of the health check. Please be aware that the name is prefixed by `MySql` in the health check. The prefixed Name will be used to identify the configuration object.
- `name`: The name of the health check. The name will be used to identify the configuration object.
- `options`: The configuration options for the health check. If you don't provide any options, the health check will use the configuration based approach.
- `tags`: The tags for the health check. The tags `mysql` and `database` are always used as default and combined with the user input. You can provide additional tags to group or filter the health checks.

Expand All @@ -55,10 +45,11 @@ The configuration looks like this:
{
..., // other configuration
"HealthChecks": {
"AddMySql<name>": {
"ConnectionString": "<connection string>",
"Timeout": "<timeout>" // optional, default is 100 milliseconds
}
"MySql": {
"<name>": {
"ConnectionString": "<connection string>",
"Timeout": "<timeout>" // optional, default is 100 milliseconds
}
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ await RunAndVerify(
var values = new Dictionary<string, string?>
{
{
"HealthChecks:MySqlTestContainerHealthy:ConnectionString",
"HealthChecks:MySql:TestContainerHealthy:ConnectionString",
_database.ConnectionString
}
};
Expand All @@ -108,10 +108,10 @@ await RunAndVerify(
var values = new Dictionary<string, string?>
{
{
"HealthChecks:MySqlTestContainerDegraded:ConnectionString",
"HealthChecks:MySql:TestContainerDegraded:ConnectionString",
_database.ConnectionString
},
{ "HealthChecks:MySqlTestContainerDegraded:Timeout", "0" }
{ "HealthChecks:MySql:TestContainerDegraded:Timeout", "0" }
};
_ = config.AddInMemoryCollection(values);
}
Expand All @@ -128,7 +128,7 @@ await RunAndVerify(
{
var values = new Dictionary<string, string?>
{
{ "HealthChecks:MySqlTestNoValues:ConnectionString", "" }
{ "HealthChecks:MySql:TestNoValues:ConnectionString", "" }
};
_ = config.AddInMemoryCollection(values);
}
Expand All @@ -146,10 +146,10 @@ await RunAndVerify(
var values = new Dictionary<string, string?>
{
{
"HealthChecks:MySqlTestNoValues:ConnectionString",
"HealthChecks:MySql:TestNoValues:ConnectionString",
_database.ConnectionString
},
{ "HealthChecks:MySqlTestNoValues:Timeout", "-2" }
{ "HealthChecks:MySql:TestNoValues:Timeout", "-2" }
};
_ = config.AddInMemoryCollection(values);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
results: [
{
description: MySqlTestNoValues: Unexpected error.,
description: TestNoValues: Unexpected error.,
exception: {
message: The connection string cannot be null or whitespace.,
type: Microsoft.Extensions.Options.OptionsValidationException
},
name: MySqlTestNoValues,
name: TestNoValues,
status: Unhealthy,
tags: [
mysql,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
results: [
{
description: MySqlTestNoValues: Unexpected error.,
description: TestNoValues: Unexpected error.,
exception: {
message: The timeout cannot be less than infinite (-1).,
type: Microsoft.Extensions.Options.OptionsValidationException
},
name: MySqlTestNoValues,
name: TestNoValues,
status: Unhealthy,
tags: [
mysql,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
results: [
{
description: MySqlTestContainerDegraded: Degraded,
name: MySqlTestContainerDegraded,
description: TestContainerDegraded: Degraded,
name: TestContainerDegraded,
status: Degraded,
tags: [
mysql,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
results: [
{
description: MySqlTestContainerHealthy: Healthy,
name: MySqlTestContainerHealthy,
description: TestContainerHealthy: Healthy,
name: TestContainerHealthy,
status: Healthy,
tags: [
mysql,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
results: [
{
description: MySqlTestContainerDegraded: Degraded,
name: MySqlTestContainerDegraded,
description: TestContainerDegraded: Degraded,
name: TestContainerDegraded,
status: Degraded,
tags: [
mysql,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
results: [
{
description: MySqlTestContainerHealthy: Healthy,
name: MySqlTestContainerHealthy,
description: TestContainerHealthy: Healthy,
name: TestContainerHealthy,
status: Healthy,
tags: [
mysql,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
results: [
{
description: MySqlTestContainerUnhealthy: Unexpected error.,
description: TestContainerUnhealthy: Unexpected error.,
exception: {
message: This is a test.,
type: MySqlConnector.MySqlException
},
name: MySqlTestContainerUnhealthy,
name: TestContainerUnhealthy,
status: Unhealthy,
tags: [
mysql,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,18 @@ params string[] tags
.ConfigureOptions<MySqlConfigure>();
}

var internalName = name.EnsureStartsWith("MySql", StringComparison.OrdinalIgnoreCase);

if (builder.IsNameAlreadyUsed(internalName))
if (builder.IsNameAlreadyUsed(name))
{
throw new ArgumentException($"Name `{name}` already in use.", nameof(name), null);
}

if (options is not null)
{
_ = builder.Services.Configure(internalName, options);
_ = builder.Services.Configure(name, options);
}

return builder.AddCheck<MySqlCheck>(
internalName,
name,
HealthStatus.Unhealthy,
new[] { "mysql", "database" }.Union(tags, StringComparer.OrdinalIgnoreCase)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal sealed class MySqlConfigure
public void Configure(string? name, MySqlOptions options)
{
Argument.ThrowIfNullOrWhiteSpace(name);
_configuration.Bind($"HealthChecks:{name}", options);
_configuration.Bind($"HealthChecks:MySql:{name}", options);
}

public void Configure(MySqlOptions options) => Configure(Options.DefaultName, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<ItemGroup>
<PackageReference Include="MySql.Data" />
<PackageReference Include="NetEvolve.Arguments" />
<PackageReference Include="NetEvolve.Extensions.Strings" />
<PackageReference Include="NetEvolve.Extensions.Tasks" />
</ItemGroup>

Expand Down
20 changes: 6 additions & 14 deletions src/HealthChecks.MySql/src/NetEvolve.HealthChecks.MySql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,10 @@ using NetEvolve.HealthChecks.MySql;
```
Therefor you can use two different approaches. In both approaches you have to provide a name for the health check.

:heavy_exclamation_mark: Please be aware that the name is prefixed by `MySql` in the health check. The prefixed Name will be used to identify the configuration object.

**Examples:**

| Name | Prefixed Name |
| --------------- | ---------------------- |
| `MySqlServer` | `MySqlServer` |
| `1` | `MySql1` |
| `Test` | `MySqlTest` |

:heavy_exclamation_mark: The configuration of this package is compatible with the [NetEvolve.HealthChecks.MySql.Connector](https://www.nuget.org/packages/NetEvolve.HealthChecks.MySql.Connector/) package.

### Parameters
- `name`: The name of the health check. Please be aware that the name is prefixed by `MySql` in the health check. The prefixed Name will be used to identify the configuration object.
- `name`: The name of the health check. The name will be used to identify the configuration object.
- `options`: The configuration options for the health check. If you don't provide any options, the health check will use the configuration based approach.
- `tags`: The tags for the health check. The tags `mysql` and `database` are always used as default and combined with the user input. You can provide additional tags to group or filter the health checks.

Expand All @@ -55,9 +45,11 @@ The configuration looks like this:
{
..., // other configuration
"HealthChecks": {
"AddMySql<name>": {
"ConnectionString": "<connection string>",
"Timeout": "<timeout>" // optional, default is 100 milliseconds
"MySql": {
"<name>": {
"ConnectionString": "<connection string>",
"Timeout": "<timeout>" // optional, default is 100 milliseconds
}
}
}
}
Expand Down
Loading

0 comments on commit 229470a

Please sign in to comment.