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: replace standard logging with structured logging #122

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

therealvio
Copy link

Purpose 🎯

These changes replace the standard library logging with structured logging using structlog. Structured logging provides easier-to-parse capabilities for toolings and humans when going through logs.

Context 🧠

  • Part 1 to satisfy Suggestion: Add Structured logging #76
    • I intend to contribute a follow-up PR that adds logging all connection test events for the purpose of fostering greater support for SLIs, as mentioned in this comment. For those unfamiliar, calculating the SLI is a matter of bad events/all valid events, and right now aws-alternat exercises a kind of "no news is good news" reporting.

Notes 📓

  • I have performed an in-place replacement of the default logger - no changes to the message contents have occurred (though if I missed something, let me know!)
  • The standard logger is still present because as far as I know, there doesn't seem to be an easy way to wrap third party dependency logs with structlog
  • Example screenshot of debug log messages through structlog
    image

Comment on lines 31 to 33
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logging.getLogger('boto3').setLevel(logging.CRITICAL)
logging.getLogger('botocore').setLevel(logging.CRITICAL)
Copy link
Author

Choose a reason for hiding this comment

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

I found this guide with regards to working with the standard library, but I wasn't sure how I could make this applicable here. Otherwise, third party deps will continue using the standard logger unless someone can figure out if wrapping the logger is possible.

Comment on lines +14 to +15
# use structlog's production-ready, performant example config
# ref: https://www.structlog.org/en/stable/performance.html#example
Copy link
Author

Choose a reason for hiding this comment

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

Talking to this particular config, when I ran into the example in this document, it matched what I had in my head as a starting point. The company I work at have some opinions on things, though I wasn't going to impose them here.

structlog.processors.add_log_level,
structlog.processors.format_exc_info,
structlog.processors.TimeStamper(fmt="iso", utc=True),
structlog.processors.EventRenamer("message"),
Copy link
Author

Choose a reason for hiding this comment

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

By default, this value is "event" though that term seems to be loaded since the Lambda accepts "events" and I thought that maybe message would be a good compromise. Though I am wary of long-term implications of this choice.

Either way, I don't have a strong opinion on whether it stays as this, we leave it as the default event, or we use a different name.

Comment on lines 189 to 193
slogger.error("Unknown event", eventPayload=event)
return

if event.get("source") != "aws.events":
logger.error(f"Unable to handle unknown event type: {json.dumps(event)}")
slogger.error("Unable to handle unknown event type", eventPayload=json.dumps(event))
Copy link
Author

Choose a reason for hiding this comment

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

As the diff indicates, this changed from a string interpolation to a key. As this comment alludes to, the term event is loaded.

I am inclined to drop this key and keep the string interpolation like I did everywhere else, or do a combination of both, or even leave that decision to another PR.

Copy link
Author

Choose a reason for hiding this comment

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

I decided to preserve the string interpolation in a later commit and will leave changes to the log message contents themselves to a later PR.

@therealvio therealvio marked this pull request as ready for review January 15, 2025 07:59
@therealvio therealvio requested a review from a team as a code owner January 15, 2025 07:59
@therealvio therealvio changed the title feat: use structured logging with structlog feat: replace standard logging with structured logging Jan 15, 2025
This changes removes the addition of the eventPayload key that was
imposed in the previous commit. I would rather leave the decision to the
maintainers.

Whether it be reverting *this* commit, or accepting this one, or leaving
it to the future PR.
@bwhaley
Copy link
Member

bwhaley commented Jan 21, 2025

Thank you for the PR! I am a bit busy atm, but I hope to review & discuss in the coming days. Appreciate the patience!

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.

2 participants