-
-
Notifications
You must be signed in to change notification settings - Fork 522
Handle more extra attribute types when logging #2815
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
base: master
Are you sure you want to change the base?
Conversation
b1ad657 to
368e00b
Compare
368e00b to
5f7f29a
Compare
| { value: JSON.generate(value), type: "string" } | ||
| rescue | ||
| { value: value, type: "string" } | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The rescue block in attribute_hash incorrectly returns the original non-serializable object, causing a crash later during final payload serialization.
Severity: CRITICAL | Confidence: High
🔍 Detailed Analysis
When a non-serializable object is passed as an attribute to a LogEvent, the attribute_hash method attempts to serialize it with JSON.generate. This fails and is caught by a rescue block. However, the rescue block then returns the original, non-serializable object within the attribute hash. This hash is embedded in the event payload. When the transport layer later attempts to serialize the entire event envelope, JSON.generate is called again on the full payload, which now contains the non-serializable object. This second serialization attempt fails with an unhandled exception, preventing the event from being sent.
💡 Suggested Fix
In the rescue block of the attribute_hash method, instead of returning the raw non-serializable value, convert it to a string representation using a method like value.to_s or value.inspect. This ensures the value is always serializable before being added to the event payload.
🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: sentry-ruby/lib/sentry/log_event.rb#L187-L190
Potential issue: When a non-serializable object is passed as an attribute to a
`LogEvent`, the `attribute_hash` method attempts to serialize it with `JSON.generate`.
This fails and is caught by a `rescue` block. However, the `rescue` block then returns
the original, non-serializable object within the attribute hash. This hash is embedded
in the event payload. When the transport layer later attempts to serialize the entire
event envelope, `JSON.generate` is called again on the full payload, which now contains
the non-serializable object. This second serialization attempt fails with an unhandled
exception, preventing the event from being sent.
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 8030622
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine, previously it would crash too but at the transport level which now works when we can convert to JSON.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
If you use JSON.parse with symbolize_names: true than there is no need to change that many lines in the tests, not sure what's best there.
Is there a way to support better nested attributes in the UI, is any SDK capable of this @sl0thentr0py ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure JSON.generate is the right thing to do here, python just does the equivalent of object.to_s in the fallback case. I'll get back on this after investigating.
🤔 |
|
@sl0thentr0py I thought Python has identical behavior (and IIRC javascript too) |
I decided to stick to just generate as it's probably faster than symbolizing too.
Unfortunately it is not supported yet, from our docs:
|
|
thanks |
Follow up after #2803
Before, this would not be logged with extra hash attributes, now it works:
Flat hash
Nested hash
Closes #2801