From 8aa13871707856c1809a37e2d9265d607f30341c Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 19 Jun 2024 09:55:38 -0700 Subject: [PATCH 1/3] feat - support OPTIONS for health endpoints --- .../health_endpoints/_health_endpoints.py | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/litellm/proxy/health_endpoints/_health_endpoints.py b/litellm/proxy/health_endpoints/_health_endpoints.py index fbb1c1a5f2c..c4dec65dbf1 100644 --- a/litellm/proxy/health_endpoints/_health_endpoints.py +++ b/litellm/proxy/health_endpoints/_health_endpoints.py @@ -1,22 +1,18 @@ -from typing import Optional, Literal -import litellm -import os import asyncio -import fastapi +import copy +import os import traceback from datetime import datetime, timedelta -from fastapi import Depends, Request, APIRouter, Header, status -from litellm.proxy.health_check import perform_health_check -from fastapi import HTTPException -import copy +from typing import Literal, Optional + +import fastapi +from fastapi import APIRouter, Depends, Header, HTTPException, Request, status + +import litellm from litellm._logging import verbose_proxy_logger +from litellm.proxy._types import CallInfo, ProxyException, UserAPIKeyAuth, WebhookEvent from litellm.proxy.auth.user_api_key_auth import user_api_key_auth -from litellm.proxy._types import ( - UserAPIKeyAuth, - ProxyException, - WebhookEvent, - CallInfo, -) +from litellm.proxy.health_check import perform_health_check #### Health ENDPOINTS #### @@ -63,9 +59,9 @@ async def health_services_endpoint( """ try: from litellm.proxy.proxy_server import ( - proxy_logging_obj, - prisma_client, general_settings, + prisma_client, + proxy_logging_obj, ) if service is None: @@ -282,9 +278,9 @@ async def health_endpoint( """ from litellm.proxy.proxy_server import ( health_check_results, + llm_model_list, use_background_health_checks, user_model, - llm_model_list, ) try: @@ -361,7 +357,7 @@ async def active_callbacks(): """ Returns a list of active callbacks on litellm.callbacks, litellm.input_callback, litellm.failure_callback, litellm.success_callback """ - from litellm.proxy.proxy_server import proxy_logging_obj, general_settings + from litellm.proxy.proxy_server import general_settings, proxy_logging_obj _alerting = str(general_settings.get("alerting")) # get success callbacks @@ -409,11 +405,16 @@ async def active_callbacks(): tags=["health"], dependencies=[Depends(user_api_key_auth)], ) +@router.options( + "/health/readiness", + tags=["health"], + dependencies=[Depends(user_api_key_auth)], +) async def health_readiness(): """ Unprotected endpoint for checking if worker can receive requests """ - from litellm.proxy.proxy_server import proxy_logging_obj, prisma_client, version + from litellm.proxy.proxy_server import prisma_client, proxy_logging_obj, version try: # get success callback From 4007c6c6e8d421b155af4cf2adc32f394c46aebf Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 19 Jun 2024 12:00:48 -0700 Subject: [PATCH 2/3] add health/readiness OPTIONS --- .../health_endpoints/_health_endpoints.py | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/litellm/proxy/health_endpoints/_health_endpoints.py b/litellm/proxy/health_endpoints/_health_endpoints.py index c4dec65dbf1..8c3af27f321 100644 --- a/litellm/proxy/health_endpoints/_health_endpoints.py +++ b/litellm/proxy/health_endpoints/_health_endpoints.py @@ -405,11 +405,6 @@ async def active_callbacks(): tags=["health"], dependencies=[Depends(user_api_key_auth)], ) -@router.options( - "/health/readiness", - tags=["health"], - dependencies=[Depends(user_api_key_auth)], -) async def health_readiness(): """ Unprotected endpoint for checking if worker can receive requests @@ -477,3 +472,20 @@ async def health_liveliness(): Unprotected endpoint for checking if worker is alive """ return "I'm alive!" + + +@router.options( + "/health/readiness", + tags=["health"], + dependencies=[Depends(user_api_key_auth)], +) +async def health_readiness_options(): + """ + Options endpoint for health/readiness check. + """ + response_headers = { + "Allow": "GET, OPTIONS", + "Access-Control-Allow-Methods": "GET, OPTIONS", + "Access-Control-Allow-Headers": "*", + } + return {"headers": response_headers} From 19f0f66dbc8e697b556dea4d7b0c81c014ff5703 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 19 Jun 2024 12:13:35 -0700 Subject: [PATCH 3/3] add options for /health/readiness and liveliness --- .../health_endpoints/_health_endpoints.py | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/health_endpoints/_health_endpoints.py b/litellm/proxy/health_endpoints/_health_endpoints.py index 8c3af27f321..68778e17749 100644 --- a/litellm/proxy/health_endpoints/_health_endpoints.py +++ b/litellm/proxy/health_endpoints/_health_endpoints.py @@ -6,7 +6,7 @@ from datetime import datetime, timedelta from typing import Literal, Optional import fastapi -from fastapi import APIRouter, Depends, Header, HTTPException, Request, status +from fastapi import APIRouter, Depends, Header, HTTPException, Request, Response, status import litellm from litellm._logging import verbose_proxy_logger @@ -488,4 +488,21 @@ async def health_readiness_options(): "Access-Control-Allow-Methods": "GET, OPTIONS", "Access-Control-Allow-Headers": "*", } - return {"headers": response_headers} + return Response(headers=response_headers, status_code=200) + + +@router.options( + "/health/liveliness", + tags=["health"], + dependencies=[Depends(user_api_key_auth)], +) +async def health_liveliness_options(): + """ + Options endpoint for health/liveliness check. + """ + response_headers = { + "Allow": "GET, OPTIONS", + "Access-Control-Allow-Methods": "GET, OPTIONS", + "Access-Control-Allow-Headers": "*", + } + return Response(headers=response_headers, status_code=200)