GitLab health endpoint before integrating code

Viewed 385

I’m new to deploying ML models and I want to deploy a model that contains several modules, each of which consist of “folders” containing some data files, .py scripts and a Python notebook.

I created a project in GitLab and I’m trying to follow tutorials on FastAPI since this is what I’m gonna be using. But I’ve been told that before I start integrating the code, I need to set up a health endpoint.

I know about the request curl "https://gitlab.example.com/-/health", but do I need to set up anything? Is there anything else I need to do for the project setup before doing the requirements.txt, building the skeleton of the application etc.?

1 Answers

It depend totaly of your needs, there is no health endpoint implemented natively in fastapi.

But I’ve been told that before I start integrating the code, I need to set up a health endpoint.

not necessarly a bad practice, you could start by listing all your futur health checks and build your route from there.

update from comment:

But I don’t know how to implement this. I need a config file? I’m very new to this.

From what i understand you are very new to python api so you should start by following the official fastapi user guide. You can also follow fastapi first steps from this.

Very basic one file project that run as is:

# main.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/health")
async def root():
    return {"message": "Alive!"}

Remember that the above is not suitable for production, only for testing/learning purposes, to make a production api you should follow the official advanced user guide and implement something like the following.

more advanced router:

You have this health lib for fastapi that is nice.

You can make basic checks like this:

# app.routers.health.py
from fastapi import APIRouter, status, Depends
from fastapi_health import health

from app.internal.health import healthy_condition, sick_condition

router = APIRouter(
    tags=["healthcheck"],
    responses={404: {"description": "not found"}},
)


@router.get('/health', status_code=status.HTTP_200_OK)
def perform_api_healthcheck(health_endpoint=Depends(health([healthy_condition, sick_condition]))):
    return health_endpoint
# app.internal.health.py
def healthy_condition():  # just for testing puposes
    return {"database": "online"}


def sick_condition():  # just for testing puposes
    return True
Related