Skip to content

Commit c5416c5

Browse files
authored
Merge pull request #4 from astar-development/features/add-gethealthcheckasync-using-result
Features/add gethealthcheckasync using result
2 parents 807c550 + b88e973 commit c5416c5

10 files changed

+113
-22
lines changed

src/AStar.Dev.Api.HealthChecks/AStar.Dev.Api.HealthChecks.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2222
<PackageProjectUrl>https://github.com/astar-development/astar-dev-api-health-checks</PackageProjectUrl>
2323
<PackageReadmeFile>Readme.md</PackageReadmeFile>
24-
<PackageReleaseNotes>This version only updates the GitHub actions.</PackageReleaseNotes>
24+
<PackageReleaseNotes>This version cleans up the HealthCheck code without affecting the public API.</PackageReleaseNotes>
2525
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
2626
<PackageTags>ASP.Core HealthChecks;HealthChecks</PackageTags>
2727
<RepositoryType>git</RepositoryType>
2828
<RepositoryUrl>https://github.com/astar-development/astar-dev-api-health-checks.git</RepositoryUrl>
2929
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
3030
<TargetFramework>net9.0</TargetFramework>
3131
<Title>AStar API HealthChecks</Title>
32-
<Version>0.3.1</Version>
32+
<Version>0.4.0</Version>
3333
</PropertyGroup>
3434

3535
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -43,6 +43,8 @@
4343
</PropertyGroup>
4444

4545
<ItemGroup>
46+
<PackageReference Include="AStar.Dev.Functional.Extensions" Version="0.2.0"/>
47+
<PackageReference Include="AStar.Dev.Technical.Debt.Reporting" Version="0.1.0"/>
4648
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions" Version="9.0.4"/>
4749
<PackageReference Include="Microsoft.Extensions.Features" Version="9.0.4"/>
4850
</ItemGroup>

src/AStar.Dev.Api.HealthChecks/AStar.Dev.Api.HealthChecks.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AStar.Dev.Api.HealthChecks/HealthCheckExtensions.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using System.Text.Json;
23
using System.Text.Json.Serialization;
34
using Microsoft.Extensions.Diagnostics.HealthChecks;
@@ -13,7 +14,7 @@ public static class HealthCheckExtensions
1314
{
1415
WriteIndented = true,
1516
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
16-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
17+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
1718
};
1819

1920
/// <summary>
@@ -31,9 +32,9 @@ public static WebApplication ConfigureHealthCheckEndpoints(this WebApplication a
3132
{
3233
[HealthStatus.Degraded] = StatusCodes.Status424FailedDependency,
3334
[HealthStatus.Healthy] = StatusCodes.Status200OK,
34-
[HealthStatus.Unhealthy] = StatusCodes.Status500InternalServerError,
35+
[HealthStatus.Unhealthy] = StatusCodes.Status500InternalServerError
3536
},
36-
Predicate = _ => true,
37+
Predicate = _ => true
3738
});
3839

3940
_ = app.MapHealthChecks("/health/ready",
@@ -44,39 +45,40 @@ public static WebApplication ConfigureHealthCheckEndpoints(this WebApplication a
4445
{
4546
[HealthStatus.Degraded] = StatusCodes.Status424FailedDependency,
4647
[HealthStatus.Healthy] = StatusCodes.Status200OK,
47-
[HealthStatus.Unhealthy] = StatusCodes.Status500InternalServerError,
48+
[HealthStatus.Unhealthy] = StatusCodes.Status500InternalServerError
4849
},
49-
Predicate = _ => true,
50+
Predicate = _ => true
5051
});
5152

5253
return app;
5354
}
5455

56+
[ExcludeFromCodeCoverage]
5557
private static Task WriteHealthCheckResponseAsync(
5658
HttpContext httpContext,
5759
HealthReport healthReport)
5860
{
5961
httpContext.Response.ContentType = "application/json; charset=utf-8";
6062

61-
IEnumerable<HealthStatusResponse> dependencyHealthChecks = healthReport.Entries.Select(static entry => new HealthStatusResponse
62-
{
63-
Name = entry.Key,
64-
Description = entry.Value.Description,
65-
Status = entry.Value.Status.ToString(),
66-
DurationInMilliseconds = entry.Value.Duration
67-
.TotalMilliseconds,
68-
Data = entry.Value.Data,
69-
Exception = entry.Value.Exception?.Message,
70-
});
63+
var dependencyHealthChecks = healthReport.Entries.Select(static entry => new HealthStatusResponse
64+
{
65+
Name = entry.Key,
66+
Description = entry.Value.Description,
67+
Status = entry.Value.Status.ToString(),
68+
DurationInMilliseconds = entry.Value.Duration
69+
.TotalMilliseconds,
70+
Data = entry.Value.Data,
71+
Exception = entry.Value.Exception?.Message
72+
});
7173

7274
var healthCheckResponse = new
7375
{
7476
Status = healthReport.Status.ToString(),
7577
TotalCheckExecutionTimeInMilliseconds = healthReport.TotalDuration.TotalMilliseconds,
76-
DependencyHealthChecks = dependencyHealthChecks,
78+
DependencyHealthChecks = dependencyHealthChecks
7779
};
7880

79-
string responseString = JsonSerializer.Serialize(healthCheckResponse, JsonSerializerOptions);
81+
var responseString = JsonSerializer.Serialize(healthCheckResponse, JsonSerializerOptions);
8082

8183
return httpContext.Response.WriteAsync(responseString);
8284
}

src/AStar.Dev.Api.HealthChecks/HealthStatusResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/// <summary>
44
/// </summary>
5-
public sealed class HealthStatusResponse
5+
public sealed record HealthStatusResponse
66
{
77
/// <summary>
88
/// </summary>

src/AStar.Dev.Api.HealthChecks/IApiClient.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using AStar.Dev.Functional.Extensions;
2+
13
namespace AStar.Dev.Api.HealthChecks;
24

35
/// <summary>
@@ -14,4 +16,14 @@ public interface IApiClient
1416
/// Health Status.
1517
/// </returns>
1618
public Task<HealthStatusResponse> GetHealthAsync(CancellationToken cancellationToken = default);
19+
20+
/// <summary>
21+
/// The GetHealthAsync method will return the basic Health Status of the API.
22+
/// </summary>
23+
/// <param name="cancellationToken">The token to optionally use to cancel the operation</param>
24+
/// <returns>
25+
/// An instance of the <see cref="Result{TError,TSuccess}" /> class wrapping an instance of the <see href="HealthStatusResponse"></see> class when successful (containing the text representation of
26+
/// the API Health Status). When the call fails, a string error message will be returned
27+
/// </returns>
28+
public Task<Result<string, HealthStatusResponse>> GetHealthCheckAsync(CancellationToken cancellationToken = default);
1729
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
namespace AStar.Dev.Api.HealthChecks;
1+
using System.Diagnostics.CodeAnalysis;
22

3+
namespace AStar.Dev.Api.HealthChecks;
4+
5+
[ExcludeFromCodeCoverage]
36
internal static class Program
47
{
5-
private static void Main() =>
8+
private static void Main()
9+
{
610
Console.WriteLine("Hello, World!");
11+
}
712
}

test/AStar.Dev.Api.HealthChecks.Tests.Unit/AStar.Dev.Api.HealthChecks.Tests.Unit.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,22 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18+
<PackageReference Include="AStar.Dev.Utilities" Version="1.6.0"/>
1819
<PackageReference Include="coverlet.collector" Version="6.0.4">
1920
<PrivateAssets>all</PrivateAssets>
2021
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2122
</PackageReference>
23+
<PackageReference Include="JetBrains.Annotations" Version="2025.1.0-eap1"/>
24+
<PackageReference Include="AStar.Dev.Technical.Debt.Reporting" Version="0.1.0"/>
25+
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0"/>
26+
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0"/>
27+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.4"/>
28+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.4"/>
29+
<PackageReference Include="Serilog" Version="4.2.0"/>
30+
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.1"/>
31+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="8.1.1"/>
32+
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="9.0.4"/>
33+
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions" Version="9.0.4"/>
2234
<PackageReference Include="Shouldly" Version="4.3.0"/>
2335
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0"/>
2436
<PackageReference Include="xunit" Version="2.9.3"/>
@@ -33,6 +45,10 @@
3345
<Using Include="Shouldly"/>
3446
</ItemGroup>
3547

48+
<ItemGroup>
49+
<ProjectReference Include="..\..\src\AStar.Dev.Api.HealthChecks\AStar.Dev.Api.HealthChecks.csproj"/>
50+
</ItemGroup>
51+
3652
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
3753
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
3854
<NoWarn>1701;1702;IDE0058;</NoWarn>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using JetBrains.Annotations;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Diagnostics.HealthChecks;
5+
6+
namespace AStar.Dev.Api.HealthChecks;
7+
8+
[TestSubject(typeof(HealthCheckExtensions))]
9+
public class HealthCheckExtensionsShould()
10+
{
11+
[Fact]
12+
public void ConfigureTheHealthCheckEndpoints()
13+
{
14+
var webApplication = WebApplication.CreateBuilder();
15+
webApplication.Services.AddHealthChecks();
16+
17+
var sut = webApplication.Build().ConfigureHealthCheckEndpoints();
18+
19+
sut.Services.GetServices< HealthCheckService>().Count().ShouldBe(1);
20+
}
21+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "Test Name",
3+
"description": "Test Description",
4+
"status": "OK",
5+
"durationInMilliseconds": 123,
6+
"data": {},
7+
"exception": "Test Exception"
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using AStar.Dev.Utilities;
2+
using JetBrains.Annotations;
3+
4+
namespace AStar.Dev.Api.HealthChecks;
5+
6+
[TestSubject(typeof(HealthStatusResponse))]
7+
public class HealthStatusResponseShould
8+
{
9+
[Fact]
10+
public void ContainTheExpectedProperties()
11+
{
12+
new HealthStatusResponse(){Name = "Test Name", Description = "Test Description", DurationInMilliseconds = 123, Data = new Dictionary<string, object>(), Exception = "Test Exception", Status = "OK"}
13+
.ToJson().ShouldMatchApproved();
14+
}
15+
}

0 commit comments

Comments
 (0)