GraphDB – Add Health Check for Neo4j in ASP.NET Core API

In this article, we shall check how to implement GraphDB – Add Health Check for Neo4j in ASP.NET Core API or MVC applications.

Today in this article, we will cover below aspects,

In our previous Neo4j GraphDB tutorial series, we looked at the basics of GraphDB and also learned how to get started with Graph Cipher query.

In several stages of the application development life cycle, a health check is required.

It allows you to keep real-time track of the health of a variety of resources, databases, and services that your business application relies on.

You can swiftly troubleshoot and resolve technical or environmental concerns as a result of this.

These simple and easy-to-use feature lets you check the status of your resources and dependencies.

GraphDB – Add Health Check for Neo4j

If you already have an API, then please follow below two steps,

Create GraphDB health checks using IHealthCheck interface

Let’s create Create GraphDB health checks using the IHealthCheck interface.

If your API had a dependency on other services or databases then, you can use a similar health check approach or combine them together with different health checks as required.

Step1 – Implement GraphDB IHealthCheck interface

Here please implement the IHealthCheck interface and override the CheckHealthAsync method.

Here we will probe Graph Health check using IGraphClient which is dependency injected in the constructor as below.

GraphHealthCheck check using IHealthCheck where we will implement the interface method as explained below

public class GraphHealthCheck : IHealthCheck
    {
        private readonly IGraphClient _client;
        public GraphHealthCheck(IGraphClient client)
        {
            _client = client;
        }
..
}

 

Let’s now define the CheckHealthAsync which is the IHealthCheck interface method implemented for Graph connection.

Below is the implementation of the CheckHealthAsync interface method,

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
         
            var healthCheckResultHealthy = await CheckNeo4jGraphConnectionAsync();
            if (healthCheckResultHealthy)
            {
                return HealthCheckResult.Healthy("neo4j graph db health check success");
            }
            return HealthCheckResult.Unhealthy("neo4j graph db health check success"); ;
        }

HealthCheckResult lets you return health as HealthyDegraded, or Unhealthy statuses.

CheckNeo4jGraphConnectionAsync method is defined as below,

private async Task<bool> CheckNeo4jGraphConnectionAsync()
        {
            try
            {
                await _client.ConnectAsync();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

In the above CheckNeo4jGraphConnectionAsync method, for any connection failure, you shall get an exception that can be used to return the failure status of the method.

Step2 – Register GraphDB health check services

In Startup.ConfigureServices() please add the HealthChecks as below,

Below is a sample Configure method,

 
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseHttpsRedirection();
            app.UseHealthChecks("/healthcheck");
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

Let’s execute the route “/healthcheck” and we shall get the Neo4j Health check success as below,

You shall be able to verify HealthyDegraded, or Unhealthy status accordingly.

GraphDB Add Health Check for Neo4j

References:

That’s all! Happy coding!

Does this help you fix your issue?

Do you have any better solutions or suggestions? Please sound off your comments below.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



2 thoughts on “GraphDB – Add Health Check for Neo4j in ASP.NET Core API

    1. Hello HoangHac- AddCheck Adds a new health check with the specified name. You can specify name of health check like Example, you can have mongodb, or sql etc. and differentiate those health check with names else you can keep it empty.

Leave a Reply

Your email address will not be published. Required fields are marked *