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

Add safe_repr option to serializer and expose serializer's whitelist_… #87

Merged
merged 2 commits into from
Dec 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ Default: ```thread```
<dd>Default 100</dd>
</dl>
</dd>
<dt>whitelisted_types</dt>
<dd>A list of `type` objects, (e.g. `type(my_class_instance)` or `MyClass`) that will be serialized using
`repr()`. Default `[]`</dd>
</dl>
</dd>
<dt>root</dt>
Expand Down
6 changes: 4 additions & 2 deletions rollbar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ def _get_pylons_request():
'locals': {
'enabled': True,
'safe_repr': True,
'sizes': DEFAULT_LOCALS_SIZES
'sizes': DEFAULT_LOCALS_SIZES,
'whitelisted_types': []
Copy link
Member

Choose a reason for hiding this comment

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

Uh, is it whitelist_types or whitelisted_types? Looks like the param that SerializableTransform takes is the former. Any reason for them to be different?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

'whitelisted_types' is what I want it to be however SerializableTransform uses 'whitelist_types'. I didn't want to cause a breaking change so I kept the later but used the new name for a new config option.

},
'verify_https': True
}
Expand Down Expand Up @@ -331,7 +332,8 @@ def init(access_token, environment='production', **kw):
# 3. Scrub URLs in the payload for keys that end with 'url'
# 4. Optional - If local variable gathering is enabled, transform the
# trace frame values using the ShortReprTransform.
_serialize_transform = SerializableTransform()
_serialize_transform = SerializableTransform(safe_repr=SETTINGS['locals']['safe_repr'],
whitelist_types=SETTINGS['locals']['whitelisted_types'])
_transforms = [
_serialize_transform,
ScrubTransform(suffixes=[(field,) for field in SETTINGS['scrub_fields']], redact_char='*'),
Expand Down
11 changes: 10 additions & 1 deletion rollbar/lib/transforms/serializable.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@


class SerializableTransform(Transform):
def __init__(self, whitelist_types=None):
def __init__(self, safe_repr=True, whitelist_types=None):
super(SerializableTransform, self).__init__()
self.safe_repr = safe_repr
self.whitelist = set(whitelist_types or [])

def transform_circular_reference(self, o, key=None, ref_key=None):
Expand Down Expand Up @@ -80,6 +81,14 @@ def transform_custom(self, o, key=None):
except TypeError:
pass

# If self.safe_repr is False, use repr() to serialize the object
if not self.safe_repr:
try:
return repr(o)
except TypeError:
pass

# Otherwise, just use the type name
return str(type(o))


Expand Down
2 changes: 1 addition & 1 deletion rollbar/test/test_rollbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def setUp(self):
rollbar.init(_test_access_token, locals={'enabled': True}, dummy_key='asdf', handler='blocking', timeout=12345)

def test_merged_settings(self):
expected = {'enabled': True, 'sizes': rollbar.DEFAULT_LOCALS_SIZES, 'safe_repr': True}
expected = {'enabled': True, 'sizes': rollbar.DEFAULT_LOCALS_SIZES, 'safe_repr': True, 'whitelisted_types': []}
self.assertDictEqual(rollbar.SETTINGS['locals'], expected)
self.assertEqual(rollbar.SETTINGS['timeout'], 12345)
self.assertEqual(rollbar.SETTINGS['dummy_key'], 'asdf')
Expand Down
26 changes: 23 additions & 3 deletions rollbar/test/test_serializable_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@


class SerializableTransformTest(BaseTest):
def _assertSerialized(self, start, expected, whitelist=None, skip_id_check=False):
serializable = SerializableTransform(whitelist_types=whitelist)
def _assertSerialized(self, start, expected, safe_repr=True, whitelist=None, skip_id_check=False):
serializable = SerializableTransform(safe_repr=safe_repr, whitelist_types=whitelist)
result = transforms.transform(start, [serializable])

"""
Expand Down Expand Up @@ -152,7 +152,27 @@ def test_encode_dict_with_bytes_key(self):
del expected[invalid]
self._assertSerialized(start, expected)

def test_encode_with_custom_repr(self):
def test_encode_with_custom_repr_no_whitelist(self):
class CustomRepr(object):
def __repr__(self):
return 'hello'

start = {'hello': 'world', 'custom': CustomRepr()}
expected = copy.deepcopy(start)
expected['custom'] = str(CustomRepr)
self._assertSerialized(start, expected)

def test_encode_with_custom_repr_no_whitelist_no_safe_repr(self):
class CustomRepr(object):
def __repr__(self):
return 'hello'

start = {'hello': 'world', 'custom': CustomRepr()}
expected = copy.deepcopy(start)
expected['custom'] = 'hello'
self._assertSerialized(start, expected, safe_repr=False)

def test_encode_with_custom_repr_whitelist(self):
class CustomRepr(object):
def __repr__(self):
return 'hello'
Expand Down