Repository navigation
pkg/tracing: handle error and typed-nil Stringer attributes - #14013
Conversation
Log fields which contain an error currently fall through to JSON
marshaling, which may produce an unhelpful value such as "{}" instead
of the error message.
Handle error values explicitly and use fmt.Sprint when formatting error
and fmt.Stringer values. Besides preserving their textual
representation, fmt handles typed-nil implementations without
propagating a panic from their Error or String method.
Also simplify the final fmt.Sprintf("%v", v) fallback to fmt.Sprint(v).
Signed-off-by: Sebastiaan van Stijn <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR improves how pkg/tracing converts arbitrary log/trace field values into OpenTelemetry attributes, ensuring error values and typed-nil error/fmt.Stringer values produce a useful, non-panicking string representation.
Changes:
- Handle
errorvalues explicitly inkeyValueto avoid falling through to JSON marshaling (which can yield{}for errors). - Use
fmt.Sprintforerrorandfmt.Stringerformatting to safely handle typed-nil implementations. - Add unit tests covering
nil,error, typed-nilerror,fmt.Stringer, typed-nilfmt.Stringer, and JSON-marshaled values.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pkg/tracing/helpers.go | Adds explicit error handling and switches string formatting to fmt.Sprint for safer, more helpful attribute strings. |
| pkg/tracing/helpers_test.go | Introduces tests validating correct formatting and non-panicking behavior for typed-nil error/Stringer values. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
/cherry-pick release/2.3 |
|
@thaJeztah: new pull request created: #14049 DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
noticed this code, and recently had a similar issue in logrus;
Log fields which contain an error currently fall through to JSON marshaling, which may produce an unhelpful value such as "{}" instead of the error message.
Handle error values explicitly and use fmt.Sprint when formatting error and fmt.Stringer values. Besides preserving their textual representation, fmt handles typed-nil implementations without propagating a panic from their Error or String method.
Also simplify the final fmt.Sprintf("%v", v) fallback to fmt.Sprint(v).