Skip to content

Commit 1ef07e3

Browse files
authored
Merge pull request #3 from PandaTechAM/development
health check changes @HaikAsatryan HaikAsatryan committed 1
2 parents f79b677 + 6e1e42f commit 1ef07e3

File tree

7 files changed

+85
-100
lines changed

7 files changed

+85
-100
lines changed

Readme.md

Lines changed: 38 additions & 41 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
@@ -120,13 +120,12 @@ Follow this example to set up your project with all the features provided by thi
120120
}
121121
}
122122
},
123+
"ResponseCrafterVisibility": "Private",
124+
"DefaultTimeZone": "Caucasus Standard Time",
123125
"RepositoryName": "be-lib-sharedkernel",
124126
"ConnectionStrings": {
125127
"Redis": "localhost:6379",
126128
"PersistentStorage": "/persistence"
127-
},
128-
"Security": {
129-
"AllowedCorsOrigins": "https://example.com,https://api.example.com"
130129
}
131130
}
132131
```
@@ -163,8 +162,9 @@ app
163162
.UseResponseCrafter()
164163
.UseCors()
165164
.MapMinimalApis()
166-
.MapDefaultEndpoints()
167165
.EnsureHealthy()
166+
.MapHealthCheckEndpoints()
167+
.MapPrometheusExporterEndpoints()
168168
.ClearAssemblyRegistry()
169169
.UseOpenApi()
170170
.MapControllers();
@@ -422,12 +422,11 @@ The package includes extension methods to simplify common validation scenarios:
422422

423423
- File Validations:
424424
- HasMaxFileSize(maxFileSizeInMb): Validates that an uploaded file does not exceed the specified maximum size.
425-
- FileTypeIsOneOf(allowedFileExtensions): Validates that the uploaded file has one of the allowed file
426-
extensions.
425+
- FileTypeIsOneOf(allowedFileExtensions): Validates that the uploaded file has one of the allowed file
426+
extensions.
427427
- String Validations:
428428
- IsValidJson(): Validates that a string is a valid JSON.
429-
- IsXssSanitized(): Validates that a string is sanitized against XSS attacks.
430-
-
429+
- IsXssSanitized(): Validates that a string is sanitized against XSS attacks.
431430

432431
## Cors
433432

@@ -547,45 +546,43 @@ app.MapControllers();
547546
app.Run();
548547
```
549548

550-
## OpenTelemetry
551-
552-
Use `builder.AddOpenTelemetry()` to add OpenTelemetry to your project. This will track metrics, traces, and logging at
553-
runtime, including ASP.NET Core and `HttpClient` operations.
554-
555-
Example:
549+
## Telemetry Integration
556550

557-
```csharp
558-
var builder = WebApplication.CreateBuilder(args);
559-
builder.AddOpenTelemetry();
560-
```
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
561569

562570
## HealthChecks
563-
564-
The `app.EnsureHealthy()` extension method performs a health check at startup and will terminate the application if it
565-
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`
566577

567578
Example:
568-
569579
```csharp
570580
var app = builder.Build();
571-
app.EnsureHealthy();
572-
```
573-
574-
### Default Endpoints
575-
576-
To map default endpoints, use `app.MapDefaultEndpoints()`. This will add the following endpoints:
577581

578-
- Ping Endpoint: `url/above-board/ping`
579-
- Health Check Endpoint: `url/above-board/health`
580-
- Prometheus Metrics Endpoint: `url/above-board/metrics`
581-
- Prometheus Health Metrics Endpoint: `url/above-board/metrics/health`
582+
app.EnsureHealthy(); // Startup validation
583+
app.MapHealthCheckEndpoints(); // Map health check routes
582584
583-
> Note: To use Prometheus endpoints, you need to apply `OpenTelemetry` as well.
584-
585-
Example:
586-
587-
```csharp
588-
app.MapDefaultEndpoints();
585+
app.Run();
589586
```
590587

591588
## Additional Extensions and NuGet Packages
@@ -611,4 +608,4 @@ This package includes various extensions and utilities to aid development:
611608

612609
## License
613610

614-
MIT License
611+
MIT License
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)