Repository navigation
fix(attributes): Handle special float values correctly #8067
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import pytest | ||
|
|
||
| import sentry_sdk | ||
|
|
||
|
|
||
|
|
@@ -221,3 +223,32 @@ | |
| assert span["attributes"]["user.name"] == "username" | ||
| assert span["attributes"]["user.email"] == "[email protected]" | ||
| assert span["attributes"]["user.ip_address"] == "127.0.0.1" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_value, expected", | ||
| [ | ||
| (float("inf"), "inf"), | ||
| (float("-inf"), "-inf"), | ||
| (float("nan"), "nan"), | ||
| ], | ||
|
Check warning on line 234 in tests/test_attributes.py
|
||
| ) | ||
| def test_unserializable_attributes(sentry_init, capture_items, bad_value, expected): | ||
| sentry_init( | ||
| traces_sample_rate=1.0, | ||
| trace_lifecycle="stream", | ||
| ) | ||
|
|
||
| items = capture_items("span") | ||
|
|
||
| with sentry_sdk.traces.start_span(name="login", attributes={"bad_attr": bad_value}): | ||
| pass | ||
|
|
||
| sentry_sdk.flush() | ||
|
|
||
| assert items | ||
|
|
||
| (span,) = [item.payload for item in items] | ||
|
|
||
| assert "bad_attr" in span["attributes"] | ||
| assert span["attributes"]["bad_attr"] == expected | ||
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.
Non-finite float-array attributes still break envelope JSON serialization
Homogeneous float lists and tuples containing NaN or infinity pass through
format_attributeunchanged.serialize_attributethen emits them as array values, but envelope payloads are encoded withjson_dumps(allow_nan=False), which raisesValueError. Normalize non-finite elements in float arrays before serialization, or otherwise ensure the resulting payload is valid JSON.Evidence
format_attributehandles non-finite values only in its scalar-float branch; its homogeneous list and tuple branches return float elements unchanged.serialize_attributeemits homogeneous lists as array values, so a value such as[float("nan")]remains in the payload.PayloadRef.get_bytes()encodes them withjson_dumps, which setsallow_nan=Falseand raisesValueErrorfor NaN or infinity.Also found at 1 additional location
tests/test_attributes.py:228-234Identified by Warden · code-review · D6L-C4X