From d0296834acc2b28ecdaaa31ef3e0f38a13d16040 Mon Sep 17 00:00:00 2001 From: Haik Date: Fri, 22 Nov 2024 01:48:15 +0400 Subject: [PATCH 1/3] updated readme.md --- Readme.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index dee8c8b..efff5b3 100644 --- a/Readme.md +++ b/Readme.md @@ -120,13 +120,12 @@ Follow this example to set up your project with all the features provided by thi } } }, + "ResponseCrafterVisibility": "Private", + "DefaultTimeZone": "Caucasus Standard Time", "RepositoryName": "be-lib-sharedkernel", "ConnectionStrings": { "Redis": "localhost:6379", "PersistentStorage": "/persistence" - }, - "Security": { - "AllowedCorsOrigins": "https://example.com,https://api.example.com" } } ``` From 926f1fccd31a96619e54c657a8888c81ffc128fe Mon Sep 17 00:00:00 2001 From: Haik <106117370+HaikAsatryan@users.noreply.github.com> Date: Fri, 22 Nov 2024 01:51:08 +0400 Subject: [PATCH 2/3] Update Readme.md Signed-off-by: Haik <106117370+HaikAsatryan@users.noreply.github.com> --- Readme.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index efff5b3..f944993 100644 --- a/Readme.md +++ b/Readme.md @@ -421,12 +421,11 @@ The package includes extension methods to simplify common validation scenarios: - File Validations: - HasMaxFileSize(maxFileSizeInMb): Validates that an uploaded file does not exceed the specified maximum size. - - FileTypeIsOneOf(allowedFileExtensions): Validates that the uploaded file has one of the allowed file + - FileTypeIsOneOf(allowedFileExtensions): Validates that the uploaded file has one of the allowed file extensions. - String Validations: - IsValidJson(): Validates that a string is a valid JSON. - - IsXssSanitized(): Validates that a string is sanitized against XSS attacks. - - + - IsXssSanitized(): Validates that a string is sanitized against XSS attacks. ## Cors @@ -610,4 +609,4 @@ This package includes various extensions and utilities to aid development: ## License -MIT License \ No newline at end of file +MIT License From 6e1e42f21742641536eb2fa5773971f0a7886893 Mon Sep 17 00:00:00 2001 From: Haik Date: Fri, 22 Nov 2024 18:39:16 +0400 Subject: [PATCH 3/3] health check changes --- Readme.md | 67 +++++++++---------- .../Constants/EndpointConstants.cs | 7 ++ .../Extensions/DefaultEndpoints.cs | 54 --------------- .../Extensions/HealthCheckExtensions.cs | 23 ++++++- .../Extensions/OpenTelemetryExtension.cs | 13 ++++ src/SharedKernel/SharedKernel.csproj | 6 +- test/SharedKernel.Demo/Program.cs | 3 +- 7 files changed, 80 insertions(+), 93 deletions(-) create mode 100644 src/SharedKernel/Constants/EndpointConstants.cs delete mode 100644 src/SharedKernel/Extensions/DefaultEndpoints.cs diff --git a/Readme.md b/Readme.md index f944993..04cf399 100644 --- a/Readme.md +++ b/Readme.md @@ -24,8 +24,8 @@ This package currently supports: - **Resilience Pipelines** for `HttpClient` operations. - **Controller Extensions** for mapping old-style MVC controllers. - **SignalR Extensions** for adding simple SignalR or distributed SignalR backed with Redis. -- **OpenTelemetry Integration** for tracking metrics, traces, and logging. -- **Health Checks** with default endpoints and startup validation. +- **OpenTelemetry**: Metrics, traces, and logs with Prometheus support. +- **Health Checks**: Startup validation and endpoints for monitoring. - Various **Extensions and Utilities**, including enumerable, string, and queryable extensions. ## Prerequisites @@ -162,8 +162,9 @@ app .UseResponseCrafter() .UseCors() .MapMinimalApis() - .MapDefaultEndpoints() .EnsureHealthy() + .MapHealthCheckEndpoints() + .MapPrometheusExporterEndpoints() .ClearAssemblyRegistry() .UseOpenApi() .MapControllers(); @@ -422,7 +423,7 @@ The package includes extension methods to simplify common validation scenarios: - File Validations: - HasMaxFileSize(maxFileSizeInMb): Validates that an uploaded file does not exceed the specified maximum size. - FileTypeIsOneOf(allowedFileExtensions): Validates that the uploaded file has one of the allowed file - extensions. + extensions. - String Validations: - IsValidJson(): Validates that a string is a valid JSON. - IsXssSanitized(): Validates that a string is sanitized against XSS attacks. @@ -545,45 +546,43 @@ app.MapControllers(); app.Run(); ``` -## OpenTelemetry +## Telemetry Integration -Use `builder.AddOpenTelemetry()` to add OpenTelemetry to your project. This will track metrics, traces, and logging at -runtime, including ASP.NET Core and `HttpClient` operations. - -Example: - -```csharp -var builder = WebApplication.CreateBuilder(args); -builder.AddOpenTelemetry(); -``` +Integrate OpenTelemetry for observability, including metrics, traces, and logging: +1. Setup: + ```csharp + var builder = WebApplication.CreateBuilder(args); + builder.AddOpenTelemetry(); + var app = builder.Build(); + app.MapPrometheusExporterEndpoints(); + app.Run(); + ``` +2. Prometheus Endpoints: + - Metrics: `url/above-board/metrics` + - Health Metrics: `url/above-board/metrics/health` +3. Included Features: + - ASP.NET Core metrics + - HTTP client telemetry + - Distributed tracing + - Logging + - Prometheus exporter ## HealthChecks - -The `app.EnsureHealthy()` extension method performs a health check at startup and will terminate the application if it -is not healthy. +- **Startup Validation:** `app.EnsureHealthy()` performs a health check at startup and terminates the application if it + is not healthy. +- **Endpoints Mapping:** `app.MapHealthCheckEndpoints()` maps default health check endpoints to the application. +- **Mapped Endpoints:** + - Ping Endpoint: `url/above-board/ping` + - Health Check Endpoint: `url/above-board/health` Example: - ```csharp var app = builder.Build(); -app.EnsureHealthy(); -``` - -### Default Endpoints - -To map default endpoints, use `app.MapDefaultEndpoints()`. This will add the following endpoints: - -- Ping Endpoint: `url/above-board/ping` -- Health Check Endpoint: `url/above-board/health` -- Prometheus Metrics Endpoint: `url/above-board/metrics` -- Prometheus Health Metrics Endpoint: `url/above-board/metrics/health` -> Note: To use Prometheus endpoints, you need to apply `OpenTelemetry` as well. +app.EnsureHealthy(); // Startup validation +app.MapHealthCheckEndpoints(); // Map health check routes -Example: - -```csharp -app.MapDefaultEndpoints(); +app.Run(); ``` ## Additional Extensions and NuGet Packages diff --git a/src/SharedKernel/Constants/EndpointConstants.cs b/src/SharedKernel/Constants/EndpointConstants.cs new file mode 100644 index 0000000..133fcdb --- /dev/null +++ b/src/SharedKernel/Constants/EndpointConstants.cs @@ -0,0 +1,7 @@ +namespace SharedKernel.Constants; + +internal static class EndpointConstants +{ + internal const string TagName = "above-board"; + internal const string BasePath = $"/{TagName}"; +} \ No newline at end of file diff --git a/src/SharedKernel/Extensions/DefaultEndpoints.cs b/src/SharedKernel/Extensions/DefaultEndpoints.cs deleted file mode 100644 index d147c4f..0000000 --- a/src/SharedKernel/Extensions/DefaultEndpoints.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System.Net; -using HealthChecks.UI.Client; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Diagnostics.HealthChecks; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Diagnostics.HealthChecks; - -namespace SharedKernel.Extensions; - -public static class DefaultEndpoints -{ - private const string TagName = "above-board"; - private const string BasePath = $"/{TagName}"; - - public static WebApplication MapDefaultEndpoints(this WebApplication app) - { - app - .MapPingEndpoint() - .MapPrometheusEndpoints() - .MapHealthEndpoint(); - return app; - } - - private static WebApplication MapPrometheusEndpoints(this WebApplication app) - { - app.MapPrometheusScrapingEndpoint($"{BasePath}/metrics"); - - app.UseHealthChecksPrometheusExporter($"{BasePath}/metrics/health", - options => options.ResultStatusCodes[HealthStatus.Unhealthy] = (int)HttpStatusCode.OK); - - return app; - } - - private static WebApplication MapPingEndpoint(this WebApplication app) - { - app - .MapGet($"{BasePath}/ping", () => "pong") - .Produces() - .WithTags(TagName) - .WithOpenApi(); - return app; - } - - private static WebApplication MapHealthEndpoint(this WebApplication app) - { - app.MapHealthChecks($"{BasePath}/health", - new HealthCheckOptions - { - ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse - }); - - return app; - } -} \ No newline at end of file diff --git a/src/SharedKernel/Extensions/HealthCheckExtensions.cs b/src/SharedKernel/Extensions/HealthCheckExtensions.cs index fc66850..a4bab0f 100644 --- a/src/SharedKernel/Extensions/HealthCheckExtensions.cs +++ b/src/SharedKernel/Extensions/HealthCheckExtensions.cs @@ -1,7 +1,11 @@ -using Microsoft.AspNetCore.Builder; +using HealthChecks.UI.Client; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using ResponseCrafter.HttpExceptions; +using SharedKernel.Constants; namespace SharedKernel.Extensions; @@ -48,4 +52,21 @@ public static WebApplicationBuilder AddHealthChecks(this WebApplicationBuilder b builder.Services.AddHealthChecks(); return builder; } + + public static WebApplication MapHealthCheckEndpoints(this WebApplication app) + { + app + .MapGet($"{EndpointConstants.BasePath}/ping", () => "pong") + .Produces() + .WithTags(EndpointConstants.TagName) + .WithOpenApi(); + + app.MapHealthChecks($"{EndpointConstants.BasePath}/health", + new HealthCheckOptions + { + ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse + }); + + return app; + } } \ No newline at end of file diff --git a/src/SharedKernel/Extensions/OpenTelemetryExtension.cs b/src/SharedKernel/Extensions/OpenTelemetryExtension.cs index 38cf67f..21384b2 100644 --- a/src/SharedKernel/Extensions/OpenTelemetryExtension.cs +++ b/src/SharedKernel/Extensions/OpenTelemetryExtension.cs @@ -1,9 +1,12 @@ +using System.Net; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Logging; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Trace; +using SharedKernel.Constants; namespace SharedKernel.Extensions; @@ -35,4 +38,14 @@ public static WebApplicationBuilder AddOpenTelemetry(this WebApplicationBuilder return builder; } + + public static WebApplication MapPrometheusExporterEndpoints(this WebApplication app) + { + app.MapPrometheusScrapingEndpoint($"{EndpointConstants.BasePath}/metrics"); + + app.UseHealthChecksPrometheusExporter($"{EndpointConstants.BasePath}/metrics/health", + options => options.ResultStatusCodes[HealthStatus.Unhealthy] = (int)HttpStatusCode.OK); + + return app; + } } \ No newline at end of file diff --git a/src/SharedKernel/SharedKernel.csproj b/src/SharedKernel/SharedKernel.csproj index bc7222a..bcf1b20 100644 --- a/src/SharedKernel/SharedKernel.csproj +++ b/src/SharedKernel/SharedKernel.csproj @@ -8,13 +8,13 @@ Readme.md Pandatech MIT - 1.0.2 + 1.0.3 Pandatech.SharedKernel Pandatech Shared Kernel Library Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document. https://github.com/PandaTechAM/be-lib-sharedkernel - Added redis and signalR utils + Healthcheck implementation changes @@ -42,7 +42,7 @@ - + diff --git a/test/SharedKernel.Demo/Program.cs b/test/SharedKernel.Demo/Program.cs index bf7af46..34442b7 100644 --- a/test/SharedKernel.Demo/Program.cs +++ b/test/SharedKernel.Demo/Program.cs @@ -41,7 +41,8 @@ .UseResponseCrafter() .UseCors() .MapMinimalApis() - .MapDefaultEndpoints() + .MapHealthCheckEndpoints() + .MapPrometheusExporterEndpoints() .EnsureHealthy() .ClearAssemblyRegistry() .UseOpenApi()