Skip to content

Commit

Permalink
Merge pull request #155 from itsharppro/dev
Browse files Browse the repository at this point in the history
(#151) (#152) (#154) healthchecks and opentelemetry stack
  • Loading branch information
DevITSharpPRO authored Dec 17, 2024
2 parents 0766e83 + e66676e commit 0d2916c
Show file tree
Hide file tree
Showing 28 changed files with 963 additions and 0 deletions.
4 changes: 4 additions & 0 deletions scripts/pack-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ declare -a packages=(
"Paralax.WebApi.Security"
"Paralax.WebApi.Swagger"
"Paralax.CQRS.WebApi"

"Paralax.OpenTelemetry"
"Paralax.Diagnostics.HealthChecks"
"Paralax.ServiceDefaults"
)

# Iterate through the defined order of packages
Expand Down
35 changes: 35 additions & 0 deletions src/Paralax.Diagnostics.HealthChecks/scripts/build-and-pack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

echo "Executing post-success scripts for branch $GITHUB_REF_NAME"
echo "Starting build and NuGet package creation for Paralax.Diagnostics.HealthChecks framework..."

cd src/Paralax.Diagnostics.HealthChecks/src/Paralax.Diagnostics.HealthChecks

echo "Restoring NuGet packages..."
dotnet restore

PACKAGE_VERSION="1.0.$GITHUB_RUN_NUMBER"
echo "Building and packing the Paralax.Diagnostics.HealthChecks library..."
dotnet pack -c release /p:PackageVersion=$PACKAGE_VERSION --no-restore -o ./nupkg

PACKAGE_PATH="./nupkg/Paralax.Diagnostics.HealthChecks.$PACKAGE_VERSION.nupkg"

if [ -f "$PACKAGE_PATH" ]; then
echo "Checking if the package is already signed..."
if dotnet nuget verify "$PACKAGE_PATH" | grep -q 'Package is signed'; then
echo "Package is already signed, skipping signing."
else
echo "Signing the NuGet package..."
dotnet nuget sign "$PACKAGE_PATH" \
--certificate-path "$CERTIFICATE_PATH" \
--timestamper http://timestamp.digicert.com
fi

echo "Uploading Paralax.Diagnostics.HealthChecks package to NuGet..."
dotnet nuget push "$PACKAGE_PATH" -k "$NUGET_API_KEY" \
-s https://api.nuget.org/v3/index.json --skip-duplicate
echo "Package uploaded to NuGet."
else
echo "Error: Package $PACKAGE_PATH not found."
exit 1
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

echo "Running tests and collecting coverage for Paralax.Diagnostics.HealthChecks..."

cd src/Paralax.Diagnostics.HealthChecks/tests/Paralax.Diagnostics.HealthChecks

echo "Restoring NuGet packages..."
dotnet restore

echo "Running tests and generating code coverage report..."
dotnet test --collect:"XPlat Code Coverage" --results-directory ./TestResults

# Check if tests succeeded
if [ $? -ne 0 ]; then
echo "Tests failed. Exiting..."
exit 1
fi

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Paralax.Diagnostics.HealthChecks;

public abstract class HealthCheckBase : IHealthCheck
{
public abstract Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context,
CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Paralax.Diagnostics.HealthChecks;

public class HealthCheckConfiguration
{
public string Name { get; set; }
public string Description { get; set; }
public string Endpoint { get; set; }
public string HttpMethod { get; set; }
public string ExpectedResponse { get; set; }
public int Timeout { get; set; }
public int Interval { get; set; }
public int Retries { get; set; }
public bool Enabled { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Paralax.Diagnostics.HealthChecks;

public class HealthCheckContext
{
public string? RegistrationName { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Concurrent;

namespace Paralax.Diagnostics.HealthChecks;

public class HealthCheckRegistry
{
private readonly ConcurrentDictionary<string, IHealthCheck> _healthChecks = new();

public bool Register(string name, IHealthCheck healthCheck)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("Health check name must not be null or whitespace.", nameof(name));

return _healthChecks.TryAdd(name, healthCheck);
}

public bool Unregister(string name)
{
return _healthChecks.TryRemove(name, out var _);
}

public IEnumerable<string> RegisteredChecks => _healthChecks.Keys;

internal ConcurrentDictionary<string, IHealthCheck> HealthChecks => _healthChecks;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Paralax.Diagnostics.HealthChecks;

public class HealthCheckResult
{
public static HealthCheckResult Healthy(string? description = null, IReadOnlyDictionary<string, object>? data = null) =>
new HealthCheckResult(HealthStatus.Healthy, description, data);

public static HealthCheckResult Degraded(string? description = null, IReadOnlyDictionary<string, object>? data = null) =>
new HealthCheckResult(HealthStatus.Degraded, description, data);

public static HealthCheckResult Unhealthy(string? description = null, IReadOnlyDictionary<string, object>? data = null) =>
new HealthCheckResult(HealthStatus.Unhealthy, description, data);

public static HealthCheckResult Busy(string? description = null, IReadOnlyDictionary<string, object>? data = null) =>
new HealthCheckResult(HealthStatus.Busy, description, data);

public static HealthCheckResult Maintenance(string? description = null, IReadOnlyDictionary<string, object>? data = null) =>
new HealthCheckResult(HealthStatus.Maintenance, description, data);

public HealthStatus Status { get; }
public string? Description { get; }
public IReadOnlyDictionary<string, object>? Data { get; }

private HealthCheckResult(HealthStatus status, string? description, IReadOnlyDictionary<string, object>? data)
{
Status = status;
Description = description;
Data = data ?? new Dictionary<string, object>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Paralax.Diagnostics.HealthChecks;

public class HealthCheckService
{
private readonly HealthCheckRegistry _registry;

public HealthCheckService(HealthCheckRegistry registry)
{
_registry = registry;
}

public async Task<Dictionary<string, HealthCheckResult>> RunHealthChecksAsync(CancellationToken cancellationToken = default)
{
var results = new Dictionary<string, HealthCheckResult>();

foreach (var healthCheck in _registry.HealthChecks)
{
var context = new HealthCheckContext { RegistrationName = healthCheck.Key };
results[healthCheck.Key] = await healthCheck.Value.CheckHealthAsync(context, cancellationToken).ConfigureAwait(false);
}

return results;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Extensions.DependencyInjection;

namespace Paralax.Diagnostics.HealthChecks;

public static class HealthChecksServiceCollectionExtensions
{
public static IServiceCollection AddHealthChecks(this IServiceCollection services)
{
var registry = new HealthCheckRegistry();
services.AddSingleton(registry);
services.AddSingleton<HealthCheckService>();

return services;
}

public static IServiceCollection AddCheck<THealthCheck>(this IServiceCollection services, string name, HealthStatus failureStatus = HealthStatus.Unhealthy)
where THealthCheck : class, IHealthCheck
{
var registry = services.BuildServiceProvider().GetService<HealthCheckRegistry>();
var healthCheckInstance = ActivatorUtilities.CreateInstance<THealthCheck>(services.BuildServiceProvider());
registry?.Register(name, healthCheckInstance);

return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Paralax.Diagnostics.HealthChecks;

public enum HealthStatus
{
Healthy,
Degraded,
Unhealthy,
Busy,
Maintenance
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Paralax.Diagnostics.HealthChecks;

public interface IHealthCheck
{
Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Paralax.Diagnostics.HealthChecks - healthcheks abstraction library</Description>
<Authors>ITSharpPro</Authors>
<Company>ITSharpPro</Company>
<PackageProjectUrl>https://itsharppro.com</PackageProjectUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<RepositoryUrl>https://github.com/ITSharpPro/Paralax</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>microservices; monitoring; healthchecks; grpc; ITSharpPro</PackageTags>
<PackageReleaseNotes>
Initial release of Paralax.Diagnostics.HealthChecks for integrating healthchecks abstraction in microservices.
</PackageReleaseNotes>
<Copyright>ITSharpPro © $(Year)</Copyright>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>

<PackageId>Paralax.Diagnostics.HealthChecks</PackageId>
<PackageIcon>Paralax_logo_128.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<IsPackable>true</IsPackable>
</PropertyGroup>

<ItemGroup>
<None Include="../../../../README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>

<None Include="../../../../docs/logo/Paralax_logo_128.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Paralax.Diagnostics.HealthChecks", "Paralax.Diagnostics.HealthChecks.csproj", "{5925B605-69FD-42EF-892B-3A83315AA496}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5925B605-69FD-42EF-892B-3A83315AA496}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5925B605-69FD-42EF-892B-3A83315AA496}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5925B605-69FD-42EF-892B-3A83315AA496}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5925B605-69FD-42EF-892B-3A83315AA496}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {592BE02A-42AB-4B9E-98B4-841442FC0206}
EndGlobalSection
EndGlobal
35 changes: 35 additions & 0 deletions src/Paralax.OpenTelemetry/scripts/build-and-pack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

echo "Executing post-success scripts for branch $GITHUB_REF_NAME"
echo "Starting build and NuGet package creation for Paralax.OpenTelemetry framework..."

cd src/Paralax.OpenTelemetry/src/Paralax.OpenTelemetry

echo "Restoring NuGet packages..."
dotnet restore

PACKAGE_VERSION="1.0.$GITHUB_RUN_NUMBER"
echo "Building and packing the Paralax.OpenTelemetry library..."
dotnet pack -c release /p:PackageVersion=$PACKAGE_VERSION --no-restore -o ./nupkg

PACKAGE_PATH="./nupkg/Paralax.OpenTelemetry.$PACKAGE_VERSION.nupkg"

if [ -f "$PACKAGE_PATH" ]; then
echo "Checking if the package is already signed..."
if dotnet nuget verify "$PACKAGE_PATH" | grep -q 'Package is signed'; then
echo "Package is already signed, skipping signing."
else
echo "Signing the NuGet package..."
dotnet nuget sign "$PACKAGE_PATH" \
--certificate-path "$CERTIFICATE_PATH" \
--timestamper http://timestamp.digicert.com
fi

echo "Uploading Paralax.OpenTelemetry package to NuGet..."
dotnet nuget push "$PACKAGE_PATH" -k "$NUGET_API_KEY" \
-s https://api.nuget.org/v3/index.json --skip-duplicate
echo "Package uploaded to NuGet."
else
echo "Error: Package $PACKAGE_PATH not found."
exit 1
fi
18 changes: 18 additions & 0 deletions src/Paralax.OpenTelemetry/scripts/test-and-collect-coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

echo "Running tests and collecting coverage for Paralax.OpenTelemetry..."

cd src/Paralax.OpenTelemetry/tests/Paralax.OpenTelemetry

echo "Restoring NuGet packages..."
dotnet restore

echo "Running tests and generating code coverage report..."
dotnet test --collect:"XPlat Code Coverage" --results-directory ./TestResults

# Check if tests succeeded
if [ $? -ne 0 ]; then
echo "Tests failed. Exiting..."
exit 1
fi

Loading

0 comments on commit 0d2916c

Please sign in to comment.