diff --git a/Readme.md b/Readme.md index dee8c8b..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 @@ -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" } } ``` @@ -163,8 +162,9 @@ app .UseResponseCrafter() .UseCors() .MapMinimalApis() - .MapDefaultEndpoints() .EnsureHealthy() + .MapHealthCheckEndpoints() + .MapPrometheusExporterEndpoints() .ClearAssemblyRegistry() .UseOpenApi() .MapControllers(); @@ -422,12 +422,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 - extensions. + - 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 @@ -547,45 +546,43 @@ app.MapControllers(); app.Run(); ``` -## OpenTelemetry - -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: +## Telemetry Integration -```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` +app.EnsureHealthy(); // Startup validation +app.MapHealthCheckEndpoints(); // Map health check routes -> Note: To use Prometheus endpoints, you need to apply `OpenTelemetry` as well. - -Example: - -```csharp -app.MapDefaultEndpoints(); +app.Run(); ``` ## Additional Extensions and NuGet Packages @@ -611,4 +608,4 @@ This package includes various extensions and utilities to aid development: ## License -MIT License \ No newline at end of file +MIT License 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()