Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(agents-api): Add /healthz endpoint #975

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions agents-api/agents_api/routers/healthz/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ruff: noqa: F401
from .check_health import check_health
from .router import router
18 changes: 18 additions & 0 deletions agents-api/agents_api/routers/healthz/check_health.py
Original file line number Diff line number Diff line change
@@ -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)}

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information flows to this location and may be exposed to an external user.

Copilot Autofix AI 2 months ago

To fix the problem, we need to ensure that detailed error information is not exposed to the user. Instead, we should log the detailed error message on the server side and return a generic error message to the user. This can be achieved by modifying the exception handling block to log the exception and return a generic error message.

  1. Import the logging module to enable logging of exceptions.
  2. Modify the exception handling block to log the exception using the logging module.
  3. Return a generic error message to the user.
Suggested changeset 1
agents-api/agents_api/routers/healthz/check_health.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/agents-api/agents_api/routers/healthz/check_health.py b/agents-api/agents_api/routers/healthz/check_health.py
--- a/agents-api/agents_api/routers/healthz/check_health.py
+++ b/agents-api/agents_api/routers/healthz/check_health.py
@@ -5,3 +5,3 @@
 from .router import router
-
+import logging
 
@@ -15,3 +15,4 @@
     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."}
 
EOF
@@ -5,3 +5,3 @@
from .router import router

import logging

@@ -15,3 +15,4 @@
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."}

Copilot is powered by AI and may make mistakes. Always verify output.
@creatorrr creatorrr committed this autofix suggestion 2 months ago.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options

return {"status": "ok"}
3 changes: 3 additions & 0 deletions agents-api/agents_api/routers/healthz/router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from fastapi import APIRouter

router: APIRouter = APIRouter()
2 changes: 2 additions & 0 deletions agents-api/agents_api/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
agents,
docs,
files,
healthz,
internal,
jobs,
sessions,
Expand Down Expand Up @@ -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
#
Expand Down
13 changes: 11 additions & 2 deletions gateway/traefik.yml.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading