Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 38 additions & 41 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
}
}
```
Expand Down Expand Up @@ -163,8 +162,9 @@ app
.UseResponseCrafter()
.UseCors()
.MapMinimalApis()
.MapDefaultEndpoints()
.EnsureHealthy()
.MapHealthCheckEndpoints()
.MapPrometheusExporterEndpoints()
.ClearAssemblyRegistry()
.UseOpenApi()
.MapControllers();
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -611,4 +608,4 @@ This package includes various extensions and utilities to aid development:

## License

MIT License
MIT License
7 changes: 7 additions & 0 deletions src/SharedKernel/Constants/EndpointConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SharedKernel.Constants;

internal static class EndpointConstants
{
internal const string TagName = "above-board";
internal const string BasePath = $"/{TagName}";
}
54 changes: 0 additions & 54 deletions src/SharedKernel/Extensions/DefaultEndpoints.cs

This file was deleted.

23 changes: 22 additions & 1 deletion src/SharedKernel/Extensions/HealthCheckExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<string>()
.WithTags(EndpointConstants.TagName)
.WithOpenApi();

app.MapHealthChecks($"{EndpointConstants.BasePath}/health",
new HealthCheckOptions
{
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});

return app;
}
}
13 changes: 13 additions & 0 deletions src/SharedKernel/Extensions/OpenTelemetryExtension.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
}
}
6 changes: 3 additions & 3 deletions src/SharedKernel/SharedKernel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Authors>Pandatech</Authors>
<Copyright>MIT</Copyright>
<Version>1.0.2</Version>
<Version>1.0.3</Version>
<PackageId>Pandatech.SharedKernel</PackageId>
<Title>Pandatech Shared Kernel Library</Title>
<PackageTags>Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar</PackageTags>
<Description>Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document.</Description>
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-sharedkernel</RepositoryUrl>
<PackageReleaseNotes>Added redis and signalR utils</PackageReleaseNotes>
<PackageReleaseNotes>Healthcheck implementation changes</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -42,7 +42,7 @@
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0"/>
<PackageReference Include="Pandatech.Crypto" Version="4.0.0" />
<PackageReference Include="Pandatech.DistributedCache" Version="3.0.0" />
<PackageReference Include="Pandatech.DistributedCache" Version="3.0.1" />
<PackageReference Include="Pandatech.FluentMinimalApiMapper" Version="2.0.1" />
<PackageReference Include="Pandatech.PandaVaultClient" Version="4.0.0" />
<PackageReference Include="Pandatech.RegexBox" Version="3.0.0" />
Expand Down
3 changes: 2 additions & 1 deletion test/SharedKernel.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
.UseResponseCrafter()
.UseCors()
.MapMinimalApis()
.MapDefaultEndpoints()
.MapHealthCheckEndpoints()
.MapPrometheusExporterEndpoints()
.EnsureHealthy()
.ClearAssemblyRegistry()
.UseOpenApi()
Expand Down