From 483340f21e38e9730ba516b14d7813f87829569b Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Fri, 18 Jan 2019 11:48:26 -0500 Subject: [PATCH] Adding API Health Check (#191) --- src/Web/HealthChecks/ApiHealthCheck.cs | 40 +++++++++++++++++++++ src/Web/HealthChecks/HomePageHealthCheck.cs | 4 ++- src/Web/Properties/launchSettings.json | 2 +- src/Web/Startup.cs | 28 +++++++++++++-- 4 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 src/Web/HealthChecks/ApiHealthCheck.cs diff --git a/src/Web/HealthChecks/ApiHealthCheck.cs b/src/Web/HealthChecks/ApiHealthCheck.cs new file mode 100644 index 0000000..989fe99 --- /dev/null +++ b/src/Web/HealthChecks/ApiHealthCheck.cs @@ -0,0 +1,40 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; + +namespace Microsoft.eShopWeb.Web.HealthChecks +{ + public class ApiHealthCheck : IHealthCheck + { + private readonly IHttpContextAccessor _httpContextAccessor; + private readonly LinkGenerator _linkGenerator; + + public ApiHealthCheck(IHttpContextAccessor httpContextAccessor, LinkGenerator linkGenerator) + { + _httpContextAccessor = httpContextAccessor; + _linkGenerator = linkGenerator; + } + + public async Task CheckHealthAsync( + HealthCheckContext context, + CancellationToken cancellationToken = default(CancellationToken)) + { + var request = _httpContextAccessor.HttpContext.Request; + + string apiLink = _linkGenerator.GetPathByAction("List", "Catalog"); + string myUrl = request.Scheme + "://" + request.Host.ToString() + apiLink; + var client = new HttpClient(); + var response = await client.GetAsync(myUrl); + var pageContents = await response.Content.ReadAsStringAsync(); + if (pageContents.Contains(".NET Bot Black Sweatshirt")) + { + return HealthCheckResult.Healthy("The check indicates a healthy result."); + } + + return HealthCheckResult.Unhealthy("The check indicates an unhealthy result."); + } + } +} diff --git a/src/Web/HealthChecks/HomePageHealthCheck.cs b/src/Web/HealthChecks/HomePageHealthCheck.cs index a8f322a..f858a55 100644 --- a/src/Web/HealthChecks/HomePageHealthCheck.cs +++ b/src/Web/HealthChecks/HomePageHealthCheck.cs @@ -19,7 +19,9 @@ namespace Microsoft.eShopWeb.Web.HealthChecks HealthCheckContext context, CancellationToken cancellationToken = default(CancellationToken)) { - string myUrl = _httpContextAccessor.HttpContext.Request.Host.ToString(); + var request = _httpContextAccessor.HttpContext.Request; + string myUrl = request.Scheme + "://" + request.Host.ToString(); + var client = new HttpClient(); var response = await client.GetAsync(myUrl); var pageContents = await response.Content.ReadAsStringAsync(); diff --git a/src/Web/Properties/launchSettings.json b/src/Web/Properties/launchSettings.json index b1b4320..652d021 100644 --- a/src/Web/Properties/launchSettings.json +++ b/src/Web/Properties/launchSettings.json @@ -1,4 +1,4 @@ -{ +{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, diff --git a/src/Web/Startup.cs b/src/Web/Startup.cs index 2e34f80..e66d563 100644 --- a/src/Web/Startup.cs +++ b/src/Web/Startup.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; @@ -18,8 +19,12 @@ using Microsoft.eShopWeb.Web.Interfaces; using Microsoft.eShopWeb.Web.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Newtonsoft.Json; using Swashbuckle.AspNetCore.Swagger; using System; +using System.Linq; +using System.Net.Mime; using System.Text; namespace Microsoft.eShopWeb.Web @@ -121,7 +126,8 @@ namespace Microsoft.eShopWeb.Web }); services.AddHealthChecks() - .AddCheck("home_page_health_check"); + .AddCheck("home_page_health_check") + .AddCheck("api_health_check"); _services = services; // used to debug registered services } @@ -151,7 +157,25 @@ namespace Microsoft.eShopWeb.Web public void Configure(IApplicationBuilder app, IHostingEnvironment env, LinkGenerator linkGenerator) { //app.UseDeveloperExceptionPage(); - app.UseHealthChecks("/health"); + app.UseHealthChecks("/health", + new HealthCheckOptions + { + ResponseWriter = async (context, report) => + { + var result = JsonConvert.SerializeObject( + new + { + status = report.Status.ToString(), + errors = report.Entries.Select(e => new + { + key = e.Key, + value = Enum.GetName(typeof(HealthStatus), e.Value.Status) + }) + }); + context.Response.ContentType = MediaTypeNames.Application.Json; + await context.Response.WriteAsync(result); + } + }); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage();