-
Notifications
You must be signed in to change notification settings - Fork 801
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: HealthChecks Dapr CD | ||
|
||
on: | ||
push: | ||
tags: | ||
- release-dapr-* | ||
- release-all-* | ||
|
||
jobs: | ||
build: | ||
env: | ||
BUILD_CONFIG: Release | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v3 | ||
with: | ||
dotnet-version: | | ||
6.0.x | ||
7.0.x | ||
- name: Restore | ||
run: dotnet restore ./src/HealthChecks.Dapr/HealthChecks.Dapr.csproj | ||
- name: Build | ||
run: dotnet build --no-restore ./src/HealthChecks.Dapr/HealthChecks.Dapr.csproj -c $BUILD_CONFIG | ||
- name: Pack | ||
run: dotnet pack --no-build ./src/HealthChecks.Dapr/HealthChecks.Dapr.csproj -c $BUILD_CONFIG -o ./artifacts | ||
- name: Publish | ||
run: dotnet nuget push ./artifacts/AspNetCore.HealthChecks.Dapr.*.nupkg -k ${{secrets.NUGET_API_KEY}} -s https://api.nuget.org/v3/index.json --skip-duplicate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: HealthChecks Dapr Preview CD | ||
|
||
on: | ||
push: | ||
tags: | ||
- preview-dapr-* | ||
- preview-all-* | ||
|
||
jobs: | ||
build: | ||
env: | ||
BUILD_CONFIG: Release | ||
VERSION_SUFFIX: rc2.${{ github.run_number }} | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v3 | ||
with: | ||
dotnet-version: | | ||
6.0.x | ||
7.0.x | ||
- name: Restore | ||
run: dotnet restore ./src/HealthChecks.Dapr/HealthChecks.Dapr.csproj | ||
- name: Build | ||
run: dotnet build --no-restore ./src/HealthChecks.Dapr/HealthChecks.Dapr.csproj -c $BUILD_CONFIG | ||
- name: Pack | ||
run: dotnet pack --no-build ./src/HealthChecks.Dapr/HealthChecks.Dapr.csproj --version-suffix $VERSION_SUFFIX -c $BUILD_CONFIG -o ./artifacts | ||
- name: Publish | ||
run: dotnet nuget push ./artifacts/AspNetCore.HealthChecks.Dapr.*.nupkg -k ${{secrets.NUGET_API_KEY}} -s https://api.nuget.org/v3/index.json --skip-duplicate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
name: HealthChecks Dapr CI | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: [ master ] | ||
paths: | ||
- src/HealthChecks.Dapr/** | ||
- test/HealthChecks.Dapr.Tests/** | ||
- test/_SHARED/** | ||
- .github/workflows/healthchecks_dapr_ci.yml | ||
- Directory.Build.props | ||
- Directory.Build.targets | ||
tags-ignore: | ||
- release-* | ||
- preview-* | ||
|
||
pull_request: | ||
branches: [ master ] | ||
paths: | ||
- src/HealthChecks.Dapr/** | ||
- test/HealthChecks.Dapr.Tests/** | ||
- test/_SHARED/** | ||
- .github/workflows/healthchecks_dapr_ci.yml | ||
- Directory.Build.props | ||
- Directory.Build.targets | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v3 | ||
with: | ||
dotnet-version: | | ||
6.0.x | ||
7.0.x | ||
- name: Restore | ||
run: | | ||
dotnet restore ./src/HealthChecks.Dapr/HealthChecks.Dapr.csproj && | ||
dotnet restore ./test/HealthChecks.Dapr.Tests/HealthChecks.Dapr.Tests.csproj | ||
- name: Check formatting | ||
run: | | ||
dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.Dapr/HealthChecks.Dapr.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) && | ||
dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.Dapr.Tests/HealthChecks.Dapr.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) | ||
- name: Build | ||
run: | | ||
dotnet build --no-restore ./src/HealthChecks.Dapr/HealthChecks.Dapr.csproj && | ||
dotnet build --no-restore ./test/HealthChecks.Dapr.Tests/HealthChecks.Dapr.Tests.csproj | ||
- name: Test | ||
run: > | ||
dotnet test | ||
./test/HealthChecks.Dapr.Tests/HealthChecks.Dapr.Tests.csproj | ||
--no-restore | ||
--no-build | ||
--collect "XPlat Code Coverage" | ||
--results-directory .coverage | ||
-- | ||
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover | ||
- name: Upload Coverage | ||
uses: codecov/codecov-action@v3 | ||
with: | ||
flags: Dapr | ||
directory: .coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Dapr.Client; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace HealthChecks.Dapr; | ||
|
||
public class DaprHealthCheck : IHealthCheck | ||
{ | ||
private readonly DaprClient _daprClient; | ||
|
||
public DaprHealthCheck(DaprClient daprClient) | ||
{ | ||
_daprClient = daprClient ?? throw new ArgumentNullException(nameof(daprClient)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
return await _daprClient.CheckHealthAsync(cancellationToken).ConfigureAwait(false) | ||
? HealthCheckResult.Healthy() | ||
: new HealthCheckResult(context.Registration.FailureStatus); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/HealthChecks.Dapr/DependencyInjection/DaprHealthCheckBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Dapr.Client; | ||
using HealthChecks.Dapr; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extension methods to configure <see cref="DaprHealthCheck"/>. | ||
/// </summary> | ||
public static class DaprHealthCheckBuilderExtensions | ||
{ | ||
private const string DAPR_NAME = "dapr"; | ||
|
||
/// <summary> | ||
/// Add a health check for Dapr using provided <see cref="DaprClient"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param> | ||
/// <param name="daprClient">The <see cref="DaprClient"/> to be used.</param> | ||
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'dapr' 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> | ||
/// <param name="timeout">An <see cref="TimeSpan"/> representing the timeout of the check. Optional.</param> | ||
/// <returns>The <see cref="IHealthChecksBuilder"/>.</returns> | ||
public static IHealthChecksBuilder AddDapr( | ||
this IHealthChecksBuilder builder, | ||
DaprClient daprClient, | ||
string? name = default, | ||
HealthStatus? failureStatus = default, | ||
IEnumerable<string>? tags = default, | ||
TimeSpan? timeout = default) | ||
{ | ||
return builder.Add(new HealthCheckRegistration( | ||
name ?? DAPR_NAME, | ||
new DaprHealthCheck(daprClient), | ||
failureStatus, | ||
tags, | ||
timeout)); | ||
} | ||
|
||
/// <summary> | ||
/// Add a health check for Dapr using <see cref="DaprClient"/> from service provider. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param> | ||
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'dapr' 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> | ||
/// <param name="timeout">An <see cref="TimeSpan"/> representing the timeout of the check. Optional.</param> | ||
/// <returns>The <see cref="IHealthChecksBuilder"/>.</returns> | ||
public static IHealthChecksBuilder AddDapr( | ||
this IHealthChecksBuilder builder, | ||
string? name = default, | ||
HealthStatus? failureStatus = default, | ||
IEnumerable<string>? tags = default, | ||
TimeSpan? timeout = default) | ||
{ | ||
return builder.Add(new HealthCheckRegistration( | ||
name ?? DAPR_NAME, | ||
sp => new DaprHealthCheck(sp.GetRequiredService<DaprClient>()), | ||
failureStatus, | ||
tags, | ||
timeout)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net7.0</TargetFrameworks> | ||
<PackageTags>$(PackageTags);Dapr</PackageTags> | ||
<Description>HealthChecks.Dapr is the health check package for Dapr.</Description> | ||
<VersionPrefix>$(HealthCheckDapr)</VersionPrefix> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="7.0.9" /> | ||
<PackageReference Include="Dapr.Client" Version="1.11.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
63 changes: 63 additions & 0 deletions
63
test/HealthChecks.Dapr.Tests/DependencyInjection/DaprRegistrationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using Dapr.Client; | ||
|
||
namespace HealthChecks.Dapr.Tests.DependencyInjection; | ||
|
||
public class dapr_registration_should | ||
{ | ||
private const string _defaultCheckName = "dapr"; | ||
|
||
[Fact] | ||
public void add_health_check_when_properly_configured_using_di() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddSingleton(new DaprClientBuilder().Build()); | ||
services.AddHealthChecks() | ||
.AddDapr(); | ||
|
||
using var serviceProvider = services.BuildServiceProvider(); | ||
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>(); | ||
|
||
var registration = options.Value.Registrations.First(); | ||
var check = registration.Factory(serviceProvider); | ||
|
||
registration.Name.ShouldBe(_defaultCheckName); | ||
check.ShouldBeOfType<DaprHealthCheck>(); | ||
} | ||
|
||
[Fact] | ||
public void add_health_check_when_properly_configured_using_arguments() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddHealthChecks() | ||
.AddDapr(daprClient: new DaprClientBuilder().Build()); | ||
|
||
using var serviceProvider = services.BuildServiceProvider(); | ||
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>(); | ||
|
||
var registration = options.Value.Registrations.First(); | ||
var check = registration.Factory(serviceProvider); | ||
|
||
registration.Name.ShouldBe(_defaultCheckName); | ||
check.ShouldBeOfType<DaprHealthCheck>(); | ||
} | ||
|
||
[Fact] | ||
public void add_named_health_check_when_properly_configured() | ||
{ | ||
var services = new ServiceCollection(); | ||
var customCheckName = "my-" + _defaultCheckName; | ||
|
||
services.AddSingleton(new DaprClientBuilder().Build()); | ||
services.AddHealthChecks() | ||
.AddDapr(name: customCheckName); | ||
|
||
using var serviceProvider = services.BuildServiceProvider(); | ||
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>(); | ||
|
||
var registration = options.Value.Registrations.First(); | ||
var check = registration.Factory(serviceProvider); | ||
|
||
registration.Name.ShouldBe(customCheckName); | ||
check.ShouldBeOfType<DaprHealthCheck>(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
test/HealthChecks.Dapr.Tests/HealthChecks.Dapr.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net7.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\HealthChecks.Dapr\HealthChecks.Dapr.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
16 changes: 16 additions & 0 deletions
16
test/HealthChecks.Dapr.Tests/HealthChecks.Dapr.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace HealthChecks.Dapr | ||
{ | ||
public class DaprHealthCheck : Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck | ||
{ | ||
public DaprHealthCheck(Dapr.Client.DaprClient daprClient) { } | ||
public System.Threading.Tasks.Task<Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckResult> CheckHealthAsync(Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckContext context, System.Threading.CancellationToken cancellationToken = default) { } | ||
} | ||
} | ||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
public static class DaprHealthCheckBuilderExtensions | ||
{ | ||
public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddDapr(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable<string>? tags = null, System.TimeSpan? timeout = default) { } | ||
public static Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder AddDapr(this Microsoft.Extensions.DependencyInjection.IHealthChecksBuilder builder, Dapr.Client.DaprClient daprClient, string? name = null, Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus? failureStatus = default, System.Collections.Generic.IEnumerable<string>? tags = null, System.TimeSpan? timeout = default) { } | ||
} | ||
} |