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

#283: Improve health endpoint with missing required conditions #291

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

RobyBen
Copy link

@RobyBen RobyBen commented Nov 24, 2024

Description

Improve health endpoint with missing required conditions

Commit type

  • feat: indicates the addition of a new feature or functionality to the project.

Issue

#283

Solution

Add health end points

Proposed Changes

  • Add database ping check
  • Add song service check
  • Add auth check
  • Add tests

Potential Impact

info about problems in project

Screenshots

None

Additional Tasks

Tests.

Assigned

@AntonioMrtz

@AntonioMrtz
Copy link
Owner

Hi @RobyBen can you check failing pipelines? I think you don't have your development environment configured with linting and pre-commit too. For more info check global set up docshttps://antoniomrtz.github.io/SpotifyElectron/developer/SETUP/ and set tup backend docs

Copy link
Owner

@AntonioMrtz AntonioMrtz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check pipelines and comments. Remember to follow setup and backend docs. I will check the tests when code it's already OK :)


class HealthCheckResponse(BaseModel):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move model class to schema file instead of having it into the controller + class docstring


class HealthCheckResponse(BaseModel):
status: str
details: Dict[str, Dict[str, str]]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use dict instead of Dict. See docs. This applies to list, dict, tuple type hints

"""Validates if the app has launched correctly
async def check_database_connection() -> Dict[str, str]:
try:
db_client = DatabaseConnectionManager.get_database_client()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a method inside db connection client that handles the ping and use DatabasePingFailed exception already declared at database_schema

@router.get("/", summary="Health Check Endpoint")
def get_health() -> Response:
"""Validates if the app has launched correctly
async def check_database_connection() -> Dict[str, str]:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic belongs to a service layer more than a controller one. Move functions that handle logic to health_service

def check_auth_config() -> Dict[str, str]:
try:
if not all([
hasattr(AuthConfig, 'SIGNING_SECRET_KEY') and AuthConfig.SIGNING_SECRET_KEY,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PropertiesManager has all the names of environment attributes in env_variables. Try not hardcode keys like that because it's better to only have one source of truth instead of multiple around the code

)
async def get_health() -> JSONResponse:
"""Validates if the app has launched correctly and all critical components are healthy

Returns
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invalid docs format, check stream_controller. Docs have to follow the following format:

"""Streams song audio

    Args:
        name (str): song name
        request (Request): incoming request
    """

service_health = check_song_service()
auth_health = check_auth_config()

health_details = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a class

status_code = HTTP_200_OK if is_healthy else HTTP_503_SERVICE_UNAVAILABLE

if not is_healthy:
logger.warning(f"Health check failed: {health_details}")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this is needed, we're going to return 503 if healthcheck failed

await db_client.admin.command('ping')
return {"status": "healthy", "message": "Database connection is active"}
except Exception as e:
logger.error(f"Database health check failed: {str(e)}")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use loggin exception instead of error. More info here

from app.database.DatabaseConnectionManager import DatabaseConnectionManager
from app.spotify_electron.song.providers.song_service_provider import SongServiceProvider
from app.logging.logging_schema import SpotifyElectronLogger
from app.logging.logging_constants import LOGGING_HEALTH
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this tested? This imports appear to be non existing

@AntonioMrtz AntonioMrtz changed the title #283: improve health endpoint with missing required conditions #283: Improve health endpoint with missing required conditions Nov 24, 2024
@AntonioMrtz AntonioMrtz linked an issue Nov 26, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve health endpoint with missing required conditions
2 participants