Browse Source

Merge pull request #268 from abpframework/HealthCheck

Add `EShopOnAbpHealthChecks`.
azure-deployment
selman koc 2 years ago
committed by GitHub
parent
commit
b25c665e2f
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 3
      apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbp.PublicWeb.csproj
  2. 7
      apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs
  3. 32
      apps/public-web/src/EShopOnAbp.PublicWeb/HealthChecks/EShopOnAbpHealthCheck.cs
  4. 77
      apps/public-web/src/EShopOnAbp.PublicWeb/HealthChecks/HealthChecksBuilderExtensions.cs

3
apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbp.PublicWeb.csproj

@ -11,6 +11,9 @@
<PackageReference Include="Volo.CmsKit.Public.Web" Version="8.3.1" />
<PackageReference Include="Yarp.ReverseProxy" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery.Yarp" Version="8.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.UI" Version="8.0.2.0" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1.0" />
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="8.0.1.0" />
</ItemGroup>
<ItemGroup>

7
apps/public-web/src/EShopOnAbp.PublicWeb/EShopOnAbpPublicWebModule.cs

@ -25,6 +25,7 @@ using System.Linq;
using System.Net.Http.Headers;
using System.Security.Claims;
using EShopOnAbp.PublicWeb.Components.Toolbar.Footer;
using EShopOnAbp.PublicWeb.HealthChecks;
using Microsoft.AspNetCore.Authentication.OAuth.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
@ -110,7 +111,7 @@ public class EShopOnAbpPublicWebModule : AbpModule
{
Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;
var configuration = context.Services.GetConfiguration();
context.Services.AddHttpForwarderWithServiceDiscovery();
ConfigureBasketHttpClient(context);
@ -278,6 +279,8 @@ public class EShopOnAbpPublicWebModule : AbpModule
);
});
});
context.Services.AddEShopOnAbpHealthChecks();
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
@ -383,4 +386,4 @@ public class EShopOnAbpPublicWebModule : AbpModule
IsEmailVerified = isEmailVerified
};
}
}
}

32
apps/public-web/src/EShopOnAbp.PublicWeb/HealthChecks/EShopOnAbpHealthCheck.cs

@ -0,0 +1,32 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Identity;
namespace EShopOnAbp.PublicWeb.HealthChecks;
public class EShopOnAbpHealthCheck : IHealthCheck, ITransientDependency
{
protected readonly IIdentityUserAppService IdentityUserAppService;
public EShopOnAbpHealthCheck(IIdentityUserAppService identityUserAppService)
{
IdentityUserAppService = identityUserAppService;
}
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{
await IdentityUserAppService.GetListAsync(new GetIdentityUsersInput { MaxResultCount = 1 });
return HealthCheckResult.Healthy($"Could connect to database and get record.");
}
catch (Exception e)
{
return HealthCheckResult.Unhealthy($"Error when trying to get database record. ", e);
}
}
}

77
apps/public-web/src/EShopOnAbp.PublicWeb/HealthChecks/HealthChecksBuilderExtensions.cs

@ -0,0 +1,77 @@
using System;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
namespace EShopOnAbp.PublicWeb.HealthChecks;
public static class HealthChecksBuilderExtensions
{
public static void AddEShopOnAbpHealthChecks(this IServiceCollection services)
{
// Add your health checks here
var healthChecksBuilder = services.AddHealthChecks();
healthChecksBuilder.AddCheck<EShopOnAbpHealthCheck>("EShopOnAbp Health Check");
services.ConfigureHealthCheckEndpoint("/health-status");
// If you don't want to add HealthChecksUI, remove following configurations.
var configuration = services.GetConfiguration();
var healthCheckUrl = configuration["App:HealthCheckUrl"];
if (string.IsNullOrEmpty(healthCheckUrl))
{
healthCheckUrl = "/health-status";
}
var healthChecksUiBuilder = services.AddHealthChecksUI(settings =>
{
settings.AddHealthCheckEndpoint("EShopOnAbp Health Status", healthCheckUrl);
});
// Set your HealthCheck UI Storage here
healthChecksUiBuilder.AddInMemoryStorage();
services.MapHealthChecksUiEndpoints(options =>
{
options.UIPath = "/health-ui";
options.ApiPath = "/health-api";
});
}
private static IServiceCollection ConfigureHealthCheckEndpoint(this IServiceCollection services, string path)
{
services.Configure<AbpEndpointRouterOptions>(options =>
{
options.EndpointConfigureActions.Add(endpointContext =>
{
endpointContext.Endpoints.MapHealthChecks(
new PathString(path.EnsureStartsWith('/')),
new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse,
AllowCachingResponses = false,
});
});
});
return services;
}
private static IServiceCollection MapHealthChecksUiEndpoints(this IServiceCollection services, Action<global::HealthChecks.UI.Configuration.Options>? setupOption = null)
{
services.Configure<AbpEndpointRouterOptions>(routerOptions =>
{
routerOptions.EndpointConfigureActions.Add(endpointContext =>
{
endpointContext.Endpoints.MapHealthChecksUI(setupOption);
});
});
return services;
}
}
Loading…
Cancel
Save