-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
659 Add Postgres and Redis to health endpoint (#690)
* adds db and redis to health endpoint, untested * updates typing, suppresses sqlalchemy and alembic logs for healthchecks * adds to makefile, updates docs and tests * format * adds check for cache enabled, update tests * format * standardize health endpoint structure
- Loading branch information
1 parent
2f63382
commit ba28896
Showing
7 changed files
with
71 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,62 @@ | ||
from typing import Dict | ||
import logging | ||
from typing import Dict, Optional, Union | ||
|
||
from alembic import migration, script | ||
from fastapi import APIRouter | ||
from redis.exceptions import ResponseError | ||
from sqlalchemy import create_engine | ||
|
||
from fidesops.api.v1.urn_registry import HEALTH | ||
from fidesops.common_exceptions import RedisConnectionError | ||
from fidesops.core.config import config | ||
from fidesops.db.database import get_alembic_config | ||
from fidesops.util.cache import get_cache | ||
|
||
router = APIRouter(tags=["Public"]) | ||
|
||
logger = logging.getLogger(__name__) | ||
# stops polluting logs with sqlalchemy / alembic info-level logs | ||
logging.getLogger("sqlalchemy.engine").setLevel(logging.ERROR) | ||
logging.getLogger("alembic").setLevel(logging.WARNING) | ||
|
||
@router.get(HEALTH, response_model=Dict[str, bool]) | ||
def health_check() -> Dict[str, bool]: | ||
|
||
@router.get(HEALTH, response_model=Dict[str, Union[bool, str]]) | ||
def health_check() -> Dict[str, Union[bool, str]]: | ||
return { | ||
"healthy": True, | ||
"webserver": "healthy", | ||
"database": get_db_health(config.database.SQLALCHEMY_DATABASE_URI), | ||
"cache": get_cache_health(), | ||
} | ||
|
||
|
||
def get_db_health(database_url: Optional[str]) -> str: | ||
"""Checks if the db is reachable and up to date in alembic migrations""" | ||
if not database_url or not config.database.ENABLED: | ||
return "no db configured" | ||
try: | ||
engine = create_engine(database_url) | ||
alembic_config = get_alembic_config(database_url) | ||
alembic_script_directory = script.ScriptDirectory.from_config(alembic_config) | ||
with engine.begin() as conn: | ||
context = migration.MigrationContext.configure(conn) | ||
if ( | ||
context.get_current_revision() | ||
!= alembic_script_directory.get_current_head() | ||
): | ||
return "needs migration" | ||
return "healthy" | ||
except Exception as error: # pylint: disable=broad-except | ||
logger.error(f"Unable to reach the database: {error}") | ||
return "unhealthy" | ||
|
||
|
||
def get_cache_health() -> str: | ||
"""Checks if the cache is reachable""" | ||
if not config.redis.ENABLED: | ||
return "no cache configured" | ||
try: | ||
get_cache() | ||
return "healthy" | ||
except (RedisConnectionError, ResponseError) as e: | ||
logger.error(f"Unable to reach cache: {e}") | ||
return "unhealthy" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters