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

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 :)

@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


class HealthCheckResponse(BaseModel):
status: str
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of status:str we can create a Enum that encapsulates different status. With that we can make the code more reusable and concise

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

@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
@AntonioMrtz
Copy link
Owner

Hi @RobyBen , let me know if you need something :). Feel free to request a review again whenever you feel ready.

@RobyBen
Copy link
Author

RobyBen commented Dec 9, 2024

Can you check my commit?

@AntonioMrtz
Copy link
Owner

Can you check my commit?

Hi @RobyBen I will check it ASAP. Thanks for your work :)

@AntonioMrtz AntonioMrtz self-requested a review December 9, 2024 18:09
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.

@AntonioMrtz
Copy link
Owner

The OpenAPI part I mentioned here is not completed :(. It should generate changes in openapi.json. The pipeline is running compare_openapi_schema.py under the hood

@AntonioMrtz AntonioMrtz self-requested a review December 11, 2024 15:08
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 OpenAPI generation mentioned

@RobyBen
Copy link
Author

RobyBen commented Dec 13, 2024

I commited the change, please take a look

@AntonioMrtz
Copy link
Owner

Perfect @RobyBen , pipelines are working correctly. I will check the code ASAP :)

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.

There're still some adjustments to be done. I also noticed that some older comments were not solve. Let me know if you need anything or you have any questions :)

environment (AppEnvironmentMode): The current environment mode.
connection_uri (str): The URI for connecting to the database.

Returns:
Copy link
Owner

Choose a reason for hiding this comment

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

There's no need to add docs for returning None. The extension njpwerner.autodocstring handles it like that and I think Pycharm too

"""
from pymongo import MongoClient
Copy link
Owner

Choose a reason for hiding this comment

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

The purpose of this class is not having any initialization of the Database until we know if tests will be run or we're in production mode. database_connection_class contains the logic associated to a concrete database, being a mock connectior or a real one. We're also abstracting the logic behind the DatabaseConnection classes, DatabaseConnectionManager is only managing that instance and showing it to the rest of the app, concrete logic should be inside DatabaseConnection classes :). Feel free to ask if something isn't explained correctly

cls.client = MongoClient(connection_uri)

@classmethod
def ping_database(cls) -> bool:
Copy link
Owner

Choose a reason for hiding this comment

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

We should move this to the concrete implementations of database connection, such as Prod and Testing, included in the same folder

logger = SpotifyElectronLogger(LOGGING_HEALTH).getLogger()


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 can return None if the if statement is not met. We should change ping database to throw an exception if ping couldn't be done instead of returning a bool. I still feel like this func belongs more to a service layer.

"message": f"SongServiceProvider check failed: {str(exception)}",
}
else:
if service is None:
Copy link
Owner

Choose a reason for hiding this comment

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

This can be done in a single return x if y else z statement

@AntonioMrtz AntonioMrtz closed this Mar 8, 2025
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