DEV Community

Luca
Luca

Posted on

1

Greatly improve your Health Endpoint with these tips!

Every API should have a /health endpoint, to make sure their app is ready and running without any problems.

Normally /health endpoints only answer with a STATUS_200, but that does not give us that much info as we can think it does.

Here are the main reasons why:

Your API may not be crashing but could be having very slow downtimes
This approach doesn’t let you check on other services you may use like: Third Party API’s, remote databases, your frontend.
So to fix this, I am going to give you some ideas on what your /health endpoint responses should look like!

Here is a coded example
As you can see, this is for Django but it can be applied for any API.

res = {
        "database":’unknown’,
        "stripe":’unknown’,
        ‘server’:’unknown’,
    }
    try:
        #* check database connection 
        start = time.time()
        users = User.objects.all()
        products = Product.objects.all()
        user = User.objects.filter(email=os.getenv(‘EMAIL_HOST_USER’))
        product = Product.objects.filter(id=1)

        # do some "modification" to the database queries, to force django to query them
        str(users)
        str(products)
        str(user)
        str(product)
        res[‘database’] = f"{round((time.time() - start) * 1000, 5)} ms to make 4 queries."


        #* check stripe connection 
        start = time.time()
        stripe.Customer.list(limit=20)
        res['stripe'] = f"{round((time.time() - start) * 1000, 5)} ms to query 20 rows."


        #* check server usage
        load = psutil.cpu_percent(interval=1)
        memory = psutil.virtual_memory().percent
        res['server']  = f"{load}% CPU Usage, {memory}% Memory Usage"


    except Exception as e:
        #* define other services as ready, find the one that caused the error, and leave the others as unknown
        for key in res.keys():
            if res[key] == None:
                res[key] = f"Error: {e}"
                break
    return Response(res, status=200)
Enter fullscreen mode Exit fullscreen mode

In this case, I am checking if my connections to my database and the Stripe API are working and have a low latency. I am also seeing how my server’s resources are working, and if they are too overloaded.

This endpoint of course is protected by authentication, only staff users can access it.

You can apply this example for all the services you may use! If you have any more ideas on what metrics to measure, don’t hesitate to leave them in the comments.

❤️ Thank you for reading this article and please share it if you found it helpful!

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay