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

Logging fixes and enhancements #421

Merged
merged 6 commits into from
Feb 12, 2024
Merged

Conversation

rsbowman-striveworks
Copy link
Contributor

  • fix structlog endpoint logging
  • turn off uvicorn default endpoint logging
  • don't log /ready and /health, these are spammy

Structlog logging wasn't working because the BackgroundTask in the custom route handler was never run. Fix by attaching the task to the response. FastAPI doesn't make this easy, see here for discussion.

Uvicorn still logs every request. We don't need this now with the custom endpoint logger, so turn up the log level with --log-level warning. I have never seen a uvicorn warning, and maybe they're important, so I kept those in. I'm open to other ways to do this, the command line flag worked and was convenient so I did that.

Copy link
Contributor

@jyono jyono left a comment

Choose a reason for hiding this comment

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

great, thank you

Comment on lines 11 to 15
def log_request(
request: Request, ignore_paths=frozenset(["/health", "/ready"])
):
if request.url.path in ignore_paths:
return
Copy link
Contributor

Choose a reason for hiding this comment

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

i'd think we want to log requests if health or ready aren't 200?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Currently we only log the request, but the uvicorn logging included status code. We probably want that also. I'll work on it.

@rsbowman-striveworks
Copy link
Contributor Author

Ideally we could replace uvicorn logging with something similar but structured. I haven't been able to do that. In particular, this PR does no logging for requests to endpoints that don't exist, whereas uvicorn shows something like

128.0.0.1:1234 "GET - /doesnt_exist HTTP/1.1" 404 Not Found

I think the best way forward would be to enhance the uvicorn logging instead of replacing it (like this uvicorn PR), but that doesn't seem possible right now.

I'm not very happy with this PR but it's better than what we have currently.

@rsbowman-striveworks rsbowman-striveworks merged commit 819d4e0 into main Feb 12, 2024
11 checks passed
@rsbowman-striveworks rsbowman-striveworks deleted the rsb-logging-fixes branch February 12, 2024 19:02
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.

4 participants