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

feat: log routes when the router is populated #609

Merged
merged 7 commits into from
Sep 27, 2023

Conversation

carlosm27
Copy link
Contributor

Description
This PR is addressing the feature request of adding logs to the endpoints when the app starts.

image

This PR fixes #599

@netlify
Copy link

netlify bot commented Sep 18, 2023

👷 Deploy request for robyn pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 8d4b74e

@codspeed-hq
Copy link

codspeed-hq bot commented Sep 18, 2023

CodSpeed Performance Report

Merging #609 will not alter performance

Comparing carlosm27:logging_routes (8d4b74e) with main (565caa9)

Summary

✅ 106 untouched benchmarks

@@ -173,6 +173,16 @@ def start(self, url: str = "127.0.0.1", port: int = 8080):
logger.info(f"Robyn version: {__version__}")
logger.info(f"Starting server at {url}:{port}")

endpoint = {}

routes = self.router.get_routes()
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should write the logs in the add_route method, instead of doing the get_routes here - it's minor, but it slows down the startup time

endpoint["method"] = route[0]
endpoint["route"] = route[1]

logger.info("Logging endpoint: {}".format(endpoint))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the wording here is not very clear, maybe change the word Logging to Adding.
Also, I think using f-strings is nicer and more consistent with the rest of the code base

@IdoKendo
Copy link
Contributor

Hey @carlosm27 👋 Great work! Added a few comments, let me know if something is unclear!


routes = self.router.get_routes()

for route in routes:
Copy link
Member

@sansyrox sansyrox Sep 19, 2023

Choose a reason for hiding this comment

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

Hey @carlosm27 👋

Thank you for the PR 😄
We should be using something like this

Suggested change
for route in routes:
for method, route, _ in routes:
logger.info("Logging endpoint: {method: %s, route: %s}", method, route)

As the logging library is optimized for deferred string evaluation https://docs.python.org/3/howto/logging.html#optimization

Copy link
Member

Choose a reason for hiding this comment

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

Thank you for the reviews @IdoKendo 😄

The logging routes is in the `add_route` method instead of in the `start` method. Also using f-string to format the logging
@carlosm27
Copy link
Contributor Author

carlosm27 commented Sep 20, 2023

Thank you both @IdoKendo and @sansyrox for their reviews and suggestions. I'm going to apply them.

Have a nice day.

@carlosm27
Copy link
Contributor Author

carlosm27 commented Sep 20, 2023

I tried this approach:

logger.info("Logging endpoint: {method: %s, route: %s}", method, route)

But I receive the following error message:

image

I used f-string instead for the logging:

logger.info(f"Logging endpoint: method: {route[0]}, route: {route[1]}")

I will continue researching to solve this.

@@ -99,6 +99,11 @@ def add_route(
}
route_type = http_methods[route_type]

routes = self.router.get_routes()

for route in routes:
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to do the for loop here, this function runs for every route separately and has the endpoint and method as input parameters.

Copy link
Contributor Author

@carlosm27 carlosm27 Sep 23, 2023

Choose a reason for hiding this comment

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

Sorry, I misunderstand when you said to write the logs in add_route method. I tried this without using a for loop, but it shows one route when running the base_routes.py file. What I'm trying to do is that it shows all the routes in the list of routes.

if len(routes) > 2:
            logger.info(f"Logging endpoint: method={routes[0][0]}, route={routes[0][1]}")

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad. Sorry, yes I misunderstand.

logger.info(f"Logging endpoint: method={route_type}, route={endpoint}")

image

Remove `for loop` from `add_route` method to log routes
@vercel
Copy link

vercel bot commented Sep 24, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
robyn ❌ Failed (Inspect) Sep 24, 2023 0:02am

@sansyrox
Copy link
Member

logger.info("Logging endpoint: {method: %s, route: %s}", method, route)

@carlosm27 maybe try this Logging endpoint: method: %s, route: %s

@carlosm27
Copy link
Contributor Author

logger.info("Logging endpoint: {method: %s, route: %s}", method, route)

@carlosm27 maybe try this Logging endpoint: method: %s, route: %s

Hey @sansyrox I tried this: logger.info("Logging endpoint: method: %s, route: %s", route_type, endpoint). But I got the same error message.

image

@sansyrox
Copy link
Member

@carlosm27 , the issue is not with your code but adding that is breaking something else. I can have a look tomorrow 😄

@sansyrox
Copy link
Member

@carlosm27 , I understand what you mean. Our abstraction is not the best right now. We can merge your PR and address that later.

Thank you for the great work! 😄

@sansyrox sansyrox changed the title Logging routes feat: log routes when the router is populated Sep 27, 2023
@sansyrox sansyrox merged commit f6fbf7a into sparckles:main Sep 27, 2023
48 of 49 checks passed
@carlosm27
Copy link
Contributor Author

@carlosm27 , I understand what you mean. Our abstraction is not the best right now. We can merge your PR and address that later.

Thank you for the great work! 😄

Hey @sansyrox thank you for your review and feedback.

@carlosm27 carlosm27 deleted the logging_routes branch October 3, 2023 00:40
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.

Log the endpoints when app starts
3 participants