Skip to content

Fix Health Check on the --health comment #11054

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 16 additions & 3 deletions litellm/proxy/proxy_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import click
import httpx
from dotenv import load_dotenv

import urllib.parse
if TYPE_CHECKING:
from fastapi import FastAPI
else:
Expand Down Expand Up @@ -55,7 +55,14 @@ def _echo_litellm_version():
@staticmethod
def _run_health_check(host, port):
print("\nLiteLLM: Health Testing models in config") # noqa
response = httpx.get(url=f"http://{host}:{port}/health")
headers = {
"x-litellm-api-key":os.getenv("LITELLM_MASTER_KEY")
}
response = httpx.get(url=f"http://{host}:{port}/health" , headers=headers)
if response.status_code != 200:
raise Exception(
f"LiteLLM: Health check failed with status code {response.status_code}"
)
print(json.dumps(response.json(), indent=4)) # noqa

@staticmethod
Expand Down Expand Up @@ -664,8 +671,14 @@ def run_server( # noqa: PLR0915
and database_password
and database_name
):
# Handle the problem of special character escaping in the database URL
database_username_enc = urllib.parse.quote_plus(database_username)
database_password_enc = urllib.parse.quote_plus(database_password)
database_name_enc = urllib.parse.quote_plus(database_name)

# Construct DATABASE_URL from the provided variables
database_url = f"postgresql://{database_username}:{database_password}@{database_host}/{database_name}"
database_url = f"postgresql://{database_username_enc}:{database_password_enc}@{database_host}/{database_name_enc}"

os.environ["DATABASE_URL"] = database_url
db_connection_pool_limit = general_settings.get(
"database_connection_pool_limit",
Expand Down
5 changes: 4 additions & 1 deletion tests/litellm/proxy/test_proxy_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from fastapi import FastAPI
from fastapi.testclient import TestClient

from litellm.llms.custom_httpx.http_handler import headers

sys.path.insert(
0, os.path.abspath("../../..")
) # Adds the parent directory to the system-path
Expand Down Expand Up @@ -42,14 +44,15 @@ def test_run_health_check(self, mock_dumps, mock_print, mock_get):
# Setup
mock_response = MagicMock()
mock_response.json.return_value = {"status": "healthy"}
mock_response.status_code = 200
mock_get.return_value = mock_response
mock_dumps.return_value = '{"status": "healthy"}'

# Execute
ProxyInitializationHelpers._run_health_check("localhost", 8000)

# Assert
mock_get.assert_called_once_with(url="http://localhost:8000/health")
mock_get.assert_called_once_with(url="http://localhost:8000/health", headers = {"x-litellm-api-key":os.getenv("LITELLM_MASTER_KEY")})
mock_response.json.assert_called_once()
mock_dumps.assert_called_once_with({"status": "healthy"}, indent=4)

Expand Down
Loading