This repository has been archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
659 Add Postgres and Redis to health endpoint #690
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
64fb11b
adds db and redis to health endpoint, untested
eastandwestwind 54a5955
updates typing, suppresses sqlalchemy and alembic logs for healthchecks
eastandwestwind d331358
adds to makefile, updates docs and tests
eastandwestwind 3c5a481
format
eastandwestwind 7f3d9d8
pull latest, fix conflicts
eastandwestwind 684cfb1
adds check for cache enabled, update tests
eastandwestwind 356bef8
format
eastandwestwind 55447f1
standardize health endpoint structure
eastandwestwind edf84b6
pull latest, fix conflicts
eastandwestwind de265b4
Merge branch 'main' into 659-add-db-redis-health-endpoint
eastandwestwind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, | ||
"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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be great if we could change this response to
"webserver": "healthy"
to standardise the format across the services. Or alternatively something like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea, I've updated to
"webserver": "healthy"