How to add Health Checks to ASP.NET Core project. A coding story.

TL;DR

Health checks are valuable and it is pretty straightforward to use them in ASP.NET Core. I’ve created a coding story to show you how to add and use them in different scenarios. (database, rabbitmq, downstream services).

Observabili…


This content originally appeared on DEV Community and was authored by Oleksii Nikiforov

TL;DR

Health checks are valuable and it is pretty straightforward to use them in ASP.NET Core. I've created a coding story to show you how to add and use them in different scenarios. (database, rabbitmq, downstream services).

Observability of the system is crucial for successful maintenance and monitoring. Health checks help you with that!

Below, you can find a coding story that describes the process of adding health checks support to ASP.NET Core project.

https://codingstories.io/story/https:%2F%2Fgitlab.com%2FNikiforovAll%2Fhow-to-add-health-checks-to-aspnetcore

Sneak peek the result

By the end of the coding story, you will see something like the following:

public class Startup
{
   public IConfiguration Configuration { get; set; }
   public Startup(IConfiguration configuration)
   {
      this.Configuration = configuration;
   }

   public void ConfigureServices(IServiceCollection services)
   {
      var connectionString = this.Configuration.GetConnectionString("DefaultConnection");
      var rabbitMqConnectionString = this.Configuration.GetConnectionString("RabbitMQ");
      var downstreamServiceUrl = this.Configuration["DownstreamService:BaseUrl"];
      services.AddHealthChecks()
            .AddSqlServer(
               connectionString,
               name: "Database",
               failureStatus: HealthStatus.Degraded,
               timeout: TimeSpan.FromSeconds(1),
               tags: new string[] { "services" })
            .AddRabbitMQ(
               rabbitMqConnectionString,
               name: "RabbitMQ",
               failureStatus: HealthStatus.Degraded,
               timeout: TimeSpan.FromSeconds(1),
               tags: new string[] { "services" })
            .AddUrlGroup(
               new Uri($"{downstreamServiceUrl}/health"),
               name: "Downstream API Health Check",
               failureStatus: HealthStatus.Unhealthy,
               timeout: TimeSpan.FromSeconds(3),
               tags: new string[] { "services" });
   }

   public void Configure(IApplicationBuilder app)
   {
      app.UseRouting();

      app.UseEndpoints(endpoints =>
      {
            endpoints.MapCustomHealthCheck();

            endpoints.MapGet("/{**path}", async context =>
            {
               await context.Response.WriteAsync(
                  "Navigate to /health to see the health status.");
            });
      });
   }
}

public static class EndpointRouteBuilderExtensions
{
   /// <summary>
   /// Adds a Health Check endpoint to the <see cref="IEndpointRouteBuilder"/> with the specified template.
   /// </summary>
   /// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add endpoint to.</param>
   /// <param name="pattern">The URL pattern of the liveness endpoint.</param>
   /// <param name="servicesPattern">The URL pattern of the readiness endpoint.</param>
   /// <returns></returns>
   public static IEndpointRouteBuilder MapCustomHealthCheck(
      this IEndpointRouteBuilder endpoints,
      string pattern = "/health",
      string servicesPattern = "/health/ready")
   {
      if (endpoints == null)
      {
            throw new ArgumentNullException(nameof(endpoints));
      }

      endpoints.MapHealthChecks(pattern, new HealthCheckOptions()
      {
            Predicate = (check) => !check.Tags.Contains("services"),
            AllowCachingResponses = false,
            ResponseWriter = WriteResponse,
      });
      endpoints.MapHealthChecks(servicesPattern, new HealthCheckOptions()
      {
            Predicate = (check) => true,
            AllowCachingResponses = false,
            ResponseWriter = WriteResponse,
      });

      return endpoints;
   }

   private static Task WriteResponse(HttpContext context, HealthReport result)
   {
      context.Response.ContentType = "application/json; charset=utf-8";

      var options = new JsonWriterOptions
      {
            Indented = true
      };

      using var stream = new MemoryStream();
      using (var writer = new Utf8JsonWriter(stream, options))
      {
            writer.WriteStartObject();
            writer.WriteString("status", result.Status.ToString());
            writer.WriteStartObject("results");
            foreach (var entry in result.Entries)
            {
               writer.WriteStartObject(entry.Key);
               writer.WriteString("status", entry.Value.Status.ToString());
               writer.WriteEndObject();
            }
            writer.WriteEndObject();
            writer.WriteEndObject();
      }

      var json = Encoding.UTF8.GetString(stream.ToArray());

      return context.Response.WriteAsync(json);
   }
}

Please let me know what you think about this coding story. Feedback is very much appreciated ?.

Reference


This content originally appeared on DEV Community and was authored by Oleksii Nikiforov


Print Share Comment Cite Upload Translate Updates
APA

Oleksii Nikiforov | Sciencx (2021-07-25T15:34:41+00:00) How to add Health Checks to ASP.NET Core project. A coding story.. Retrieved from https://www.scien.cx/2021/07/25/how-to-add-health-checks-to-asp-net-core-project-a-coding-story/

MLA
" » How to add Health Checks to ASP.NET Core project. A coding story.." Oleksii Nikiforov | Sciencx - Sunday July 25, 2021, https://www.scien.cx/2021/07/25/how-to-add-health-checks-to-asp-net-core-project-a-coding-story/
HARVARD
Oleksii Nikiforov | Sciencx Sunday July 25, 2021 » How to add Health Checks to ASP.NET Core project. A coding story.., viewed ,<https://www.scien.cx/2021/07/25/how-to-add-health-checks-to-asp-net-core-project-a-coding-story/>
VANCOUVER
Oleksii Nikiforov | Sciencx - » How to add Health Checks to ASP.NET Core project. A coding story.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/25/how-to-add-health-checks-to-asp-net-core-project-a-coding-story/
CHICAGO
" » How to add Health Checks to ASP.NET Core project. A coding story.." Oleksii Nikiforov | Sciencx - Accessed . https://www.scien.cx/2021/07/25/how-to-add-health-checks-to-asp-net-core-project-a-coding-story/
IEEE
" » How to add Health Checks to ASP.NET Core project. A coding story.." Oleksii Nikiforov | Sciencx [Online]. Available: https://www.scien.cx/2021/07/25/how-to-add-health-checks-to-asp-net-core-project-a-coding-story/. [Accessed: ]
rf:citation
» How to add Health Checks to ASP.NET Core project. A coding story. | Oleksii Nikiforov | Sciencx | https://www.scien.cx/2021/07/25/how-to-add-health-checks-to-asp-net-core-project-a-coding-story/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.