Updated 13. Using HealthChecks in eShopOnContainers (markdown)

Eduard Tomàs 2019-01-29 12:01:34 +01:00
parent 6631bdc51f
commit 2769942899

@ -1,11 +1,54 @@
HealthChecks will come as a Nuget package, probably as part of ASP.NET Core 2.1. Still TBD. ASP.NET Core 2.2 Healtchecks package is used in all APIs and applications of eShopOnContainers.
In the meantime, check the following documentation in order to use the HealthChecks library (Alpha code used in eShopOnContainers): All applications and APIs expose two endpoints (`/liveness` and `/hc`) to check the current application and all their dependencies. The `liveness` endpoint is intended to be used as a liveness probe in Kubernetes and the `hc` is intended to be used as a readiness probe in Kubernetes.
**Implementing health checks in ASP.NET Core services** ## Implementing health checks in ASP.NET Core services
Here is the documentation about how implement healthchecks in ASP.NET Core 2.2:
https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/monitor-app-health https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/monitor-app-health
Also, there's a **nice blog post** on HealthChecks by @scottsauber Also, there's a **nice blog post** on HealthChecks by @scottsauber
https://scottsauber.com/2017/05/22/using-the-microsoft-aspnetcore-healthchecks-package/ https://scottsauber.com/2017/05/22/using-the-microsoft-aspnetcore-healthchecks-package/
## Implementation in eShopOnContainers
The readiness endpoint (`/hc`) checks all the dependencies of the API. Let's take the MVC client as an example. This client depends on:
* Web purchasing BFF
* Web marketing BFF
* Identity API
So, following code is added to `ConfigureServices` in `Startup`:
```cs
services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy())
.AddUrlGroup(new Uri(configuration["PurchaseUrlHC"]), name: "purchaseapigw-check", tags: new string[] { "purchaseapigw" })
.AddUrlGroup(new Uri(configuration["MarketingUrlHC"]), name: "marketingapigw-check", tags: new string[] { "marketingapigw" })
.AddUrlGroup(new Uri(configuration["IdentityUrlHC"]), name: "identityapi-check", tags: new string[] { "identityapi" });
return services;
```
Four checkers are added: one named "self" that will return always OK, and three that will check the dependent services. Next step is to add the two endpoints (`/liveness` and `/hc`). Note that the `/liveness` must return always an HTTP 200 (if liveness endpoint can be reached that means that the MVC web is in healthy state (althought it may not be usable if some dependent service is not healthy).
```cs
app.UseHealthChecks("/liveness", new HealthCheckOptions
{
Predicate = r => r.Name.Contains("self")
});
```
The predicate defines wich checkers are executed. In this case for the `/liveness` endpoint we only want to run the checker named "self" (the one that returns always OK).
Next step is to define the `/hc` endpoint:
```cs
app.UseHealthChecks("/hc", new HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
```
In this case we want to run **all checkers defined** (so, the predicate will always return true to select all checkers).