🐛 bug: prevent panics on non-string log keys#4046
Conversation
Summary of ChangesHello @gaby, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical bug in the logging system where passing non-string values as keys to structured logging functions could lead to application panics. By introducing a type-safe approach to key handling, the change ensures that all key types are gracefully converted to their string representation, significantly enhancing the stability and reliability of the logging infrastructure. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
WalkthroughStructured logging was changed to handle non-string keys in log output. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4046 +/- ##
==========================================
- Coverage 91.21% 91.20% -0.01%
==========================================
Files 119 119
Lines 11117 11121 +4
==========================================
+ Hits 10140 10143 +3
- Misses 620 621 +1
Partials 357 357
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request effectively resolves a panic caused by non-string keys in structured logging functions by replacing a risky type assertion with a safe type switch. The added test case confirms the fix. My review includes a suggestion to refactor the new test to use sub-tests, which will improve test isolation and maintainability. Overall, this is a solid improvement to the logging utility's robustness.
There was a problem hiding this comment.
Pull request overview
Prevents panics in structured logging (*w helpers) when callers pass non-string keys by safely stringifying keys before writing them to the log buffer.
Changes:
- Replaced the forced
.(string)key assertion indefaultLogger.privateLogwwith safe handling via a type switch (string keys written directly; other key types formatted). - Added a regression test covering non-string keys for
Tracew/Infow, asserting no panics and deterministic output.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
log/default.go |
Makes privateLogw robust to non-string keys by avoiding a panic-causing type assertion. |
log/default_test.go |
Adds coverage ensuring non-string keys do not panic and produce stable log output. |
Motivation
Tracew,Infow) by making key handling safe and stable.Description
.(string)assertion inlog/default.gowith a type switch that writes string keys directly and formats non-string keys viafmt.Fprint, and addTest_DefaultLoggerNonStringKeystolog/default_test.goto exerciseTracew/Infowwith non-string keys and assert no panics and stable output.