Skip to content

Commit 6e1e42f

Browse files
committed
health check changes
1 parent 926f1fc commit 6e1e42f

File tree

7 files changed

+80
-93
lines changed

7 files changed

+80
-93
lines changed

Readme.md

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ This package currently supports:
2424
- **Resilience Pipelines** for `HttpClient` operations.
2525
- **Controller Extensions** for mapping old-style MVC controllers.
2626
- **SignalR Extensions** for adding simple SignalR or distributed SignalR backed with Redis.
27-
- **OpenTelemetry Integration** for tracking metrics, traces, and logging.
28-
- **Health Checks** with default endpoints and startup validation.
27+
- **OpenTelemetry**: Metrics, traces, and logs with Prometheus support.
28+
- **Health Checks**: Startup validation and endpoints for monitoring.
2929
- Various **Extensions and Utilities**, including enumerable, string, and queryable extensions.
3030

3131
## Prerequisites
@@ -162,8 +162,9 @@ app
162162
.UseResponseCrafter()
163163
.UseCors()
164164
.MapMinimalApis()
165-
.MapDefaultEndpoints()
166165
.EnsureHealthy()
166+
.MapHealthCheckEndpoints()
167+
.MapPrometheusExporterEndpoints()
167168
.ClearAssemblyRegistry()
168169
.UseOpenApi()
169170
.MapControllers();
@@ -422,7 +423,7 @@ The package includes extension methods to simplify common validation scenarios:
422423
- File Validations:
423424
- HasMaxFileSize(maxFileSizeInMb): Validates that an uploaded file does not exceed the specified maximum size.
424425
- FileTypeIsOneOf(allowedFileExtensions): Validates that the uploaded file has one of the allowed file
425-
extensions.
426+
extensions.
426427
- String Validations:
427428
- IsValidJson(): Validates that a string is a valid JSON.
428429
- IsXssSanitized(): Validates that a string is sanitized against XSS attacks.
@@ -545,45 +546,43 @@ app.MapControllers();
545546
app.Run();
546547
```
547548

548-
## OpenTelemetry
549+
## Telemetry Integration
549550

550-
Use `builder.AddOpenTelemetry()` to add OpenTelemetry to your project. This will track metrics, traces, and logging at
551-
runtime, including ASP.NET Core and `HttpClient` operations.
552-
553-
Example:
554-
555-
```csharp
556-
var builder = WebApplication.CreateBuilder(args);
557-
builder.AddOpenTelemetry();
558-
```
551+
Integrate OpenTelemetry for observability, including metrics, traces, and logging:
552+
1. Setup:
553+
```csharp
554+
var builder = WebApplication.CreateBuilder(args);
555+
builder.AddOpenTelemetry();
556+
var app = builder.Build();
557+
app.MapPrometheusExporterEndpoints();
558+
app.Run();
559+
```
560+
2. Prometheus Endpoints:
561+
- Metrics: `url/above-board/metrics`
562+
- Health Metrics: `url/above-board/metrics/health`
563+
3. Included Features:
564+
- ASP.NET Core metrics
565+
- HTTP client telemetry
566+
- Distributed tracing
567+
- Logging
568+
- Prometheus exporter
559569

560570
## HealthChecks
561-
562-
The `app.EnsureHealthy()` extension method performs a health check at startup and will terminate the application if it
563-
is not healthy.
571+
- **Startup Validation:** `app.EnsureHealthy()` performs a health check at startup and terminates the application if it
572+
is not healthy.
573+
- **Endpoints Mapping:** `app.MapHealthCheckEndpoints()` maps default health check endpoints to the application.
574+
- **Mapped Endpoints:**
575+
- Ping Endpoint: `url/above-board/ping`
576+
- Health Check Endpoint: `url/above-board/health`
564577

565578
Example:
566-
567579
```csharp
568580
var app = builder.Build();
569-
app.EnsureHealthy();
570-
```
571-
572-
### Default Endpoints
573-
574-
To map default endpoints, use `app.MapDefaultEndpoints()`. This will add the following endpoints:
575-
576-
- Ping Endpoint: `url/above-board/ping`
577-
- Health Check Endpoint: `url/above-board/health`
578-
- Prometheus Metrics Endpoint: `url/above-board/metrics`
579-
- Prometheus Health Metrics Endpoint: `url/above-board/metrics/health`
580581

581-
> Note: To use Prometheus endpoints, you need to apply `OpenTelemetry` as well.
582+
app.EnsureHealthy(); // Startup validation
583+
app.MapHealthCheckEndpoints(); // Map health check routes
582584
583-
Example:
584-
585-
```csharp
586-
app.MapDefaultEndpoints();
585+
app.Run();
587586
```
588587

589588
## Additional Extensions and NuGet Packages
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace SharedKernel.Constants;
2+
3+
internal static class EndpointConstants
4+
{
5+
internal const string TagName = "above-board";
6+
internal const string BasePath = $"/{TagName}";
7+
}

src/SharedKernel/Extensions/DefaultEndpoints.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/SharedKernel/Extensions/HealthCheckExtensions.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
using Microsoft.AspNetCore.Builder;
1+
using HealthChecks.UI.Client;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
4+
using Microsoft.AspNetCore.Http;
25
using Microsoft.Extensions.DependencyInjection;
36
using Microsoft.Extensions.Diagnostics.HealthChecks;
47
using ResponseCrafter.HttpExceptions;
8+
using SharedKernel.Constants;
59

610
namespace SharedKernel.Extensions;
711

@@ -48,4 +52,21 @@ public static WebApplicationBuilder AddHealthChecks(this WebApplicationBuilder b
4852
builder.Services.AddHealthChecks();
4953
return builder;
5054
}
55+
56+
public static WebApplication MapHealthCheckEndpoints(this WebApplication app)
57+
{
58+
app
59+
.MapGet($"{EndpointConstants.BasePath}/ping", () => "pong")
60+
.Produces<string>()
61+
.WithTags(EndpointConstants.TagName)
62+
.WithOpenApi();
63+
64+
app.MapHealthChecks($"{EndpointConstants.BasePath}/health",
65+
new HealthCheckOptions
66+
{
67+
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
68+
});
69+
70+
return app;
71+
}
5172
}

src/SharedKernel/Extensions/OpenTelemetryExtension.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
using System.Net;
12
using Microsoft.AspNetCore.Builder;
23
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Diagnostics.HealthChecks;
35
using Microsoft.Extensions.Logging;
46
using OpenTelemetry.Metrics;
57
using OpenTelemetry.Resources;
68
using OpenTelemetry.Trace;
9+
using SharedKernel.Constants;
710

811
namespace SharedKernel.Extensions;
912

@@ -35,4 +38,14 @@ public static WebApplicationBuilder AddOpenTelemetry(this WebApplicationBuilder
3538

3639
return builder;
3740
}
41+
42+
public static WebApplication MapPrometheusExporterEndpoints(this WebApplication app)
43+
{
44+
app.MapPrometheusScrapingEndpoint($"{EndpointConstants.BasePath}/metrics");
45+
46+
app.UseHealthChecksPrometheusExporter($"{EndpointConstants.BasePath}/metrics/health",
47+
options => options.ResultStatusCodes[HealthStatus.Unhealthy] = (int)HttpStatusCode.OK);
48+
49+
return app;
50+
}
3851
}

src/SharedKernel/SharedKernel.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>1.0.2</Version>
11+
<Version>1.0.3</Version>
1212
<PackageId>Pandatech.SharedKernel</PackageId>
1313
<Title>Pandatech Shared Kernel Library</Title>
1414
<PackageTags>Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar</PackageTags>
1515
<Description>Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-sharedkernel</RepositoryUrl>
17-
<PackageReleaseNotes>Added redis and signalR utils</PackageReleaseNotes>
17+
<PackageReleaseNotes>Healthcheck implementation changes</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>
@@ -42,7 +42,7 @@
4242
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0"/>
4343
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0"/>
4444
<PackageReference Include="Pandatech.Crypto" Version="4.0.0" />
45-
<PackageReference Include="Pandatech.DistributedCache" Version="3.0.0" />
45+
<PackageReference Include="Pandatech.DistributedCache" Version="3.0.1" />
4646
<PackageReference Include="Pandatech.FluentMinimalApiMapper" Version="2.0.1" />
4747
<PackageReference Include="Pandatech.PandaVaultClient" Version="4.0.0" />
4848
<PackageReference Include="Pandatech.RegexBox" Version="3.0.0" />

test/SharedKernel.Demo/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
.UseResponseCrafter()
4242
.UseCors()
4343
.MapMinimalApis()
44-
.MapDefaultEndpoints()
44+
.MapHealthCheckEndpoints()
45+
.MapPrometheusExporterEndpoints()
4546
.EnsureHealthy()
4647
.ClearAssemblyRegistry()
4748
.UseOpenApi()

0 commit comments

Comments
 (0)