From 62fb581a21152c315ab9a01423d5452561de2071 Mon Sep 17 00:00:00 2001 From: HamadaSalhab Date: Thu, 19 Dec 2024 10:51:11 +0300 Subject: [PATCH 1/4] chore(agents-api): Add `/healthz` endpoint --- .../agents_api/routers/healthz/__init__.py | 3 +++ .../agents_api/routers/healthz/check_health.py | 18 ++++++++++++++++++ .../agents_api/routers/healthz/router.py | 3 +++ agents-api/agents_api/web.py | 2 ++ gateway/traefik.yml.template | 13 +++++++++++-- 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 agents-api/agents_api/routers/healthz/__init__.py create mode 100644 agents-api/agents_api/routers/healthz/check_health.py create mode 100644 agents-api/agents_api/routers/healthz/router.py diff --git a/agents-api/agents_api/routers/healthz/__init__.py b/agents-api/agents_api/routers/healthz/__init__.py new file mode 100644 index 000000000..dcac6579b --- /dev/null +++ b/agents-api/agents_api/routers/healthz/__init__.py @@ -0,0 +1,3 @@ +# ruff: noqa: F401 +from .check_health import check_health +from .router import router diff --git a/agents-api/agents_api/routers/healthz/check_health.py b/agents-api/agents_api/routers/healthz/check_health.py new file mode 100644 index 000000000..08d1da026 --- /dev/null +++ b/agents-api/agents_api/routers/healthz/check_health.py @@ -0,0 +1,18 @@ +from uuid import UUID + +from ...autogen.openapi_model import Agent, ListResponse +from ...models.agent.list_agents import list_agents as list_agents_query +from .router import router + + +@router.get("/healthz", tags=["healthz"]) +async def check_health() -> dict: + try: + # Check if the database is reachable + agents = list_agents_query( + developer_id=UUID("00000000-0000-0000-0000-000000000000"), + ) + except Exception as e: + return {"status": "error", "message": str(e)} + + return {"status": "ok"} \ No newline at end of file diff --git a/agents-api/agents_api/routers/healthz/router.py b/agents-api/agents_api/routers/healthz/router.py new file mode 100644 index 000000000..5c3ec9311 --- /dev/null +++ b/agents-api/agents_api/routers/healthz/router.py @@ -0,0 +1,3 @@ +from fastapi import APIRouter + +router: APIRouter = APIRouter() diff --git a/agents-api/agents_api/web.py b/agents-api/agents_api/web.py index 8e2e7da54..21fd2f1eb 100644 --- a/agents-api/agents_api/web.py +++ b/agents-api/agents_api/web.py @@ -33,6 +33,7 @@ sessions, tasks, users, + healthz, ) if not sentry_dsn: @@ -188,6 +189,7 @@ async def scalar_html(): app.include_router(docs.router, dependencies=[Depends(get_api_key)]) app.include_router(tasks.router, dependencies=[Depends(get_api_key)]) app.include_router(internal.router) +app.include_router(healthz.router) # TODO: CORS should be enabled only for JWT auth # diff --git a/gateway/traefik.yml.template b/gateway/traefik.yml.template index 28ed51de8..c5eb5654b 100644 --- a/gateway/traefik.yml.template +++ b/gateway/traefik.yml.template @@ -46,8 +46,17 @@ http: middlewares: - agents-api-strip-prefix-api service: service-agents-api - priority: 2 - + priority: 2 + + agents-api-healthz: + entryPoints: + - web + rule: Path(`/api/healthz`) + middlewares: + - agents-api-strip-prefix-api + service: service-agents-api + priority: 3 + agents-api-redirect-to-docs: entryPoints: - web From da9dd5b140e2c1faefda4f31731e450deaed437a Mon Sep 17 00:00:00 2001 From: HamadaSalhab Date: Thu, 19 Dec 2024 07:52:32 +0000 Subject: [PATCH 2/4] refactor: Lint agents-api (CI) --- agents-api/agents_api/routers/healthz/check_health.py | 2 +- agents-api/agents_api/web.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/agents-api/agents_api/routers/healthz/check_health.py b/agents-api/agents_api/routers/healthz/check_health.py index 08d1da026..9336609b8 100644 --- a/agents-api/agents_api/routers/healthz/check_health.py +++ b/agents-api/agents_api/routers/healthz/check_health.py @@ -15,4 +15,4 @@ async def check_health() -> dict: except Exception as e: return {"status": "error", "message": str(e)} - return {"status": "ok"} \ No newline at end of file + return {"status": "ok"} diff --git a/agents-api/agents_api/web.py b/agents-api/agents_api/web.py index 21fd2f1eb..fc4b50935 100644 --- a/agents-api/agents_api/web.py +++ b/agents-api/agents_api/web.py @@ -28,12 +28,12 @@ agents, docs, files, + healthz, internal, jobs, sessions, tasks, users, - healthz, ) if not sentry_dsn: From f6a49ec19d0cf83ffbec214163163c0262c8976c Mon Sep 17 00:00:00 2001 From: Diwank Singh Tomer Date: Thu, 19 Dec 2024 15:17:32 +0530 Subject: [PATCH 3/4] Fix code scanning alert no. 4: Information exposure through an exception Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- agents-api/agents_api/routers/healthz/check_health.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/agents-api/agents_api/routers/healthz/check_health.py b/agents-api/agents_api/routers/healthz/check_health.py index 9336609b8..8e96c2b1e 100644 --- a/agents-api/agents_api/routers/healthz/check_health.py +++ b/agents-api/agents_api/routers/healthz/check_health.py @@ -3,7 +3,7 @@ from ...autogen.openapi_model import Agent, ListResponse from ...models.agent.list_agents import list_agents as list_agents_query from .router import router - +import logging @router.get("/healthz", tags=["healthz"]) async def check_health() -> dict: @@ -13,6 +13,7 @@ async def check_health() -> dict: developer_id=UUID("00000000-0000-0000-0000-000000000000"), ) except Exception as e: - return {"status": "error", "message": str(e)} + logging.error("An error occurred while checking health: %s", str(e)) + return {"status": "error", "message": "An internal error has occurred."} return {"status": "ok"} From 54b8ff6a9dcf9e6574a6496230f148233465e26d Mon Sep 17 00:00:00 2001 From: creatorrr Date: Thu, 19 Dec 2024 09:48:19 +0000 Subject: [PATCH 4/4] refactor: Lint agents-api (CI) --- agents-api/agents_api/routers/healthz/check_health.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/agents-api/agents_api/routers/healthz/check_health.py b/agents-api/agents_api/routers/healthz/check_health.py index 8e96c2b1e..74e9f5c6c 100644 --- a/agents-api/agents_api/routers/healthz/check_health.py +++ b/agents-api/agents_api/routers/healthz/check_health.py @@ -1,9 +1,10 @@ +import logging from uuid import UUID from ...autogen.openapi_model import Agent, ListResponse from ...models.agent.list_agents import list_agents as list_agents_query from .router import router -import logging + @router.get("/healthz", tags=["healthz"]) async def check_health() -> dict: