def _assertSerialized(self, start, expected, safe_repr=True, safelist=None, skip_id_check=False): serializable = SerializableTransform(safe_repr=safe_repr, safelist_types=safelist) result = transforms.transform(start, serializable) """ #print start print result print expected """ if not skip_id_check: self.assertNotEqual(id(result), id(expected)) self.assertEqual(type(expected), type(result)) if isinstance(result, Mapping): self.assertDictEqual(result, expected) elif isinstance(result, tuple): self.assertTupleEqual(result, expected) elif isinstance(result, (list, set)): self.assertListEqual(result, expected) else: self.assertEqual(result, expected)
def test_encode_Infinity(self): start = float('inf') serializable = SerializableTransform() result = transforms.transform(start, serializable) self.assertTrue(math.isinf(result))
def test_encode_NaN(self): start = float('nan') serializable = SerializableTransform() result = transforms.transform(start, serializable) self.assertTrue(math.isnan(result))
def test_encode_with_bad_repr_doesnt_die(self): class CustomRepr(object): def __repr__(self): assert False start = {'hello': 'world', 'custom': CustomRepr()} serializable = SerializableTransform(whitelist_types=[CustomRepr]) result = transforms.transform(start, serializable) self.assertRegex(result['custom'], "<AssertionError.*CustomRepr.*>")
def test_encode_with_custom_repr_returns_object(self): class CustomRepr(object): def __repr__(self): return {'hi': 'there'} start = {'hello': 'world', 'custom': CustomRepr()} serializable = SerializableTransform(whitelist_types=[CustomRepr]) result = transforms.transform(start, serializable) self.assertRegex(result['custom'], "<class '.*CustomRepr'>")
def test_encode_with_custom_repr_returns_bytes(self): class CustomRepr(object): def __repr__(self): return b'hello' start = {'hello': 'world', 'custom': CustomRepr()} serializable = SerializableTransform(whitelist_types=[CustomRepr]) result = transforms.transform(start, serializable) if python_major_version() < 3: self.assertEqual(result['custom'], b'hello') else: self.assertRegex(result['custom'], "<class '.*CustomRepr'>")
def test_encode_with_bad_str_doesnt_die(self): class UnStringableException(Exception): def __str__(self): raise Exception('asdf') class CustomRepr(object): def __repr__(self): raise UnStringableException() start = {'hello': 'world', 'custom': CustomRepr()} serializable = SerializableTransform(safelist_types=[CustomRepr]) result = transforms.transform(start, serializable) self.assertRegex(result['custom'], "<UnStringableException.*Exception.*str.*>")
def init(access_token, environment='production', **kw): """ Saves configuration variables in this module's SETTINGS. access_token: project access token. Get this from the Rollbar UI: - click "Settings" in the top nav - click "Projects" in the left nav - copy-paste the appropriate token. environment: environment name. Can be any string; suggestions: 'production', 'development', 'staging', 'yourname' **kw: provided keyword arguments will override keys in SETTINGS. """ global SETTINGS, agent_log, _initialized, _transforms, _serialize_transform, _threads # Merge the extra config settings into SETTINGS SETTINGS = dict_merge(SETTINGS, kw) if _initialized: # NOTE: Temp solution to not being able to re-init. # New versions of pyrollbar will support re-initialization # via the (not-yet-implemented) configure() method. if not SETTINGS.get('suppress_reinit_warning'): log.warning('Rollbar already initialized. Ignoring re-init.') return SETTINGS['access_token'] = access_token SETTINGS['environment'] = environment if SETTINGS.get('allow_logging_basic_config'): logging.basicConfig() if SETTINGS.get('handler') == 'agent': agent_log = _create_agent_log() # We will perform these transforms in order: # 1. Serialize the payload to be all python built-in objects # 2. Scrub the payloads based on the key suffixes in SETTINGS['scrub_fields'] # 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(safe_repr=SETTINGS['locals']['safe_repr'], whitelist_types=SETTINGS['locals']['whitelisted_types']) _transforms = [ ScrubRedactTransform(), _serialize_transform, ScrubTransform(suffixes=[(field,) for field in SETTINGS['scrub_fields']], redact_char='*'), ScrubUrlTransform(suffixes=[(field,) for field in SETTINGS['url_fields']], params_to_scrub=SETTINGS['scrub_fields']) ] # A list of key prefixes to apply our shortener transform to. The request # being included in the body key is old behavior and is being retained for # backwards compatibility. shortener_keys = [ ('request', 'POST'), ('request', 'json'), ('body', 'request', 'POST'), ('body', 'request', 'json'), ] if SETTINGS['locals']['enabled']: shortener_keys.append(('body', 'trace', 'frames', '*', 'code')) shortener_keys.append(('body', 'trace', 'frames', '*', 'args', '*')) shortener_keys.append(('body', 'trace', 'frames', '*', 'kwargs', '*')) shortener_keys.append(('body', 'trace', 'frames', '*', 'locals', '*')) shortener_keys.extend(SETTINGS['shortener_keys']) shortener = ShortenerTransform(safe_repr=SETTINGS['locals']['safe_repr'], keys=shortener_keys, **SETTINGS['locals']['sizes']) _transforms.append(shortener) _threads = queue.Queue() events.reset() filters.add_builtin_filters(SETTINGS) _initialized = True