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

Add structured logging for the API #4263

Merged
merged 16 commits into from
May 7, 2024
Merged

Add structured logging for the API #4263

merged 16 commits into from
May 7, 2024

Conversation

dhruvkb
Copy link
Member

@dhruvkb dhruvkb commented May 3, 2024

Fixes

Fixes #3432 by @sarayourfriend

Description

This PR

  • adds structured logging support for Django using
  • refactors the logging setup to remove cruft and dead code
  • removes django-log-request-id as that functionality is included in django-structlog
  • updates all loggers to use structlog

Please don't be daunted by the file count, most of these are loggingstructlog migrations. These changes was required to make the tests pass, else I would've made them a separate PR. The main files to review are the 5 or so changed files inside api/conf/.

The PR is marked as 🟧 high-priority because it blocks all the work on monitoring metrics associated with content moderation.

Testing Instructions

  1. Check out the branch and run the dev server: just api/up.
  2. Tail the server logs: just logs web.
  3. Access some endpoints, perform some tasks and observe these points.
    • All logs for a particular request have the ID of that request (no separate package required).
    • Each request has a request_started event logging the URL, HTTP method and user agent.
    • Each request also has a request_finished event logging the URL, HTTP method and response status code.
    • There is no need to getChild logger as each log entry records the logger name, file name, func name and even line where it was called.
    • Logs with only a text message become JSON like {"event": "<message>"}.
    • Other loggers (like those for Uvicorn and Elasticsearch) are not affected and continue to be unstructured.

Checklist

  • My pull request has a descriptive title (not a vague title likeUpdate index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.
  • I ran the DAG documentation generator (just catalog/generate-docs for catalog
    PRs) or the media properties generator (just catalog/generate-docs media-props
    for the catalog or just api/generate-docs for the API) where applicable.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@dhruvkb dhruvkb added 🟧 priority: high Stalls work on the project or its dependents 🌟 goal: addition Addition of new feature 💻 aspect: code Concerns the software code in the repository 🧱 stack: api Related to the Django API labels May 3, 2024
@openverse-bot openverse-bot added the 🤖 aspect: dx Concerns developers' experience with the codebase label May 3, 2024
@dhruvkb dhruvkb marked this pull request as ready for review May 4, 2024 13:01
@dhruvkb dhruvkb requested a review from a team as a code owner May 4, 2024 13:01
@dhruvkb dhruvkb requested review from krysal and stacimc May 4, 2024 13:01
Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

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

The logs now look awesome, and you can follow a request through all of the log statements.

Copy link
Collaborator

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

Awesome! Two small blockers:

  • The issue requests For ease of reading in local development, add a switch to default to flat line style logs when ENVIRONMENT == "local"., and indeed, the JSON logs are very hard to read with my human eyes. I've left a suggestion for implementing this, but would be happy with anything that makes it configurable, with sensible defaults per environment.
  • We have a documentation website page about logging, that doesn't necessarily need to be completely re-written now (some parts of the app still use that approach, until they are covered by the other structured logging issues), but at least a reference to structlog in the API for now would be good to put there in case it is referenced.

These are small blockers, but I feel they're blockers because the first is an important devex problem that the issue anticipated and requested; the second is a documentation issue that if left unaddressed would contradict the state of the code after this PR is merged.

except ConnectionError:
cache_fetch_failed = True
sources = None
log.warning("Redis connect failed, cannot get cached sources.")
logger.warning("Redis connect failed, cannot get cached sources.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if it would be nice to change these to structured logs too, maybe in a future issue. Something like logger.warning("Redis connect failed", when="get-cached-sources"), and so forth. Ideally these logs never matter though, and would never be meaningful to query, so probably not worth it!

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we should definitely update existing logs to be more "structured" but that, like you said, will have to be a separate issue and PR.

api/conf/settings/logging.py Outdated Show resolved Hide resolved
@dhruvkb dhruvkb requested a review from a team as a code owner May 7, 2024 01:43
@dhruvkb dhruvkb requested a review from sarayourfriend May 7, 2024 01:46
Copy link

github-actions bot commented May 7, 2024

Full-stack documentation: https://docs.openverse.org/_preview/4263

Please note that GitHub pages takes a little time to deploy newly pushed code, if the links above don't work or you see old versions, wait 5 minutes and try again.

You can check the GitHub pages deployment action list to see the current status of the deployments.

Changed files 🔄:

Copy link
Collaborator

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

Excellent!

@dhruvkb dhruvkb merged commit c92d484 into main May 7, 2024
45 checks passed
@dhruvkb dhruvkb deleted the dj_struct_log branch May 7, 2024 04:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💻 aspect: code Concerns the software code in the repository 🤖 aspect: dx Concerns developers' experience with the codebase 🌟 goal: addition Addition of new feature 🟧 priority: high Stalls work on the project or its dependents 🧱 stack: api Related to the Django API
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

API structured logging
4 participants