def _build_person_data(request): """ Returns a dictionary describing the logged-in user using data from `request. Try request.rollbar_person first, then 'user', then 'user_id' """ if hasattr(request, 'rollbar_person'): rollbar_person_prop = request.rollbar_person try: person = rollbar_person_prop() except TypeError: person = rollbar_person_prop if person and isinstance(person, dict): return person else: return None if hasattr(request, 'user'): user_prop = request.user try: user = user_prop() except TypeError: user = user_prop if not user: return None elif isinstance(user, dict): return user else: retval = {} if getattr(user, 'id', None): retval['id'] = text(user.id) elif getattr(user, 'user_id', None): retval['id'] = text(user.user_id) # id is required, so only include username/email if we have an id if retval.get('id'): username = getattr(user, 'username', None) email = getattr(user, 'email', None) retval.update({ 'username': username, 'email': email }) return retval if hasattr(request, 'user_id'): user_id_prop = request.user_id try: user_id = user_id_prop() except TypeError: user_id = user_id_prop if not user_id: return None return {'id': text(user_id)}
def _trace_data(cls, exc, trace): # exception info # most recent call last raw_frames = traceback.extract_tb(trace) frames = [{ 'filename': f[0], 'lineno': f[1], 'method': f[2], 'code': f[3] } for f in raw_frames] if SETTINGS.get('filename_regex_sub'): for frame in frames: for pattern, replacement in SETTINGS['filename_regex_sub']: filename = re.sub(pattern, replacement, frame['filename']) frame['filename'] = filename trace_data = { 'frames': frames, 'exception': { 'class': getattr(cls, '__name__', cls.__class__.__name__), 'message': text(exc), } } _add_locals_data(trace_data, (cls, exc, trace)) return trace_data
def _shorten_other(self, obj): if obj is None: return None if self.safe_repr: obj = text(obj) return self._repr.repr(obj)
def transform_namedtuple(self, o, key=None): tuple_dict = o._asdict() transformed_dict = self.transform_dict(tuple_dict, key=key) new_vals = [] for field in tuple_dict: new_vals.append(transformed_dict[field]) return '<%s>' % text(o._make(new_vals))
def _report_exc_info(exc_info, request, extra_data, payload_data, level=None): """ Called by report_exc_info() wrapper """ # check if exception is marked ignored cls, exc, trace = exc_info if getattr(exc, '_rollbar_ignore', False) or _is_ignored(exc): return if not _check_config(): return data = _build_base_data(request) filtered_level = _filtered_level(exc) if filtered_level: data['level'] = filtered_level # explicitly override the level with provided level if level: data['level'] = level # exception info # most recent call last raw_frames = traceback.extract_tb(trace) frames = [{'filename': f[0], 'lineno': f[1], 'method': f[2], 'code': f[3]} for f in raw_frames] data['body'] = { 'trace': { 'frames': frames, 'exception': { 'class': cls.__name__, 'message': text(exc), } } } if extra_data: extra_data = extra_data if isinstance(extra_data, dict): data['custom'] = extra_data else: data['custom'] = {'value': extra_data} _add_locals_data(data, exc_info) _add_request_data(data, request) _add_person_data(data, request) data['server'] = _build_server_data() if payload_data: data = dict_merge(data, payload_data) payload = _build_payload(data) send_payload(payload, data.get('access_token')) return data['uuid']
def redact(self, val): if self.randomize_len: _len = random.randint(3, 20) else: try: _len = len(val) except: _len = len(text(val)) return self.redact_char * _len
def _shorten_other(self, obj): if obj is None: return None if isinstance(obj, float): if math.isinf(obj): return 'Infinity' if math.isnan(obj): return 'NaN' if self.safe_repr: obj = text(obj) return self._repr.repr(obj)
def _build_base_data(request, level='error'): data = { 'timestamp': int(time.time()), 'environment': SETTINGS['environment'], 'level': level, 'language': 'python %s' % '.'.join(str(x) for x in sys.version_info[:3]), 'notifier': SETTINGS['notifier'], 'uuid': text(uuid.uuid4()), } if SETTINGS.get('code_version'): data['code_version'] = SETTINGS['code_version'] if BASE_DATA_HOOK: BASE_DATA_HOOK(request, data) return data
def _build_base_data(self, request, level='error'): data = { 'timestamp': int(time.time()), 'environment': self.settings['environment'], 'level': level, 'language': 'python %s' % '.'.join(str(x) for x in sys.version_info[:3]), 'notifier': self.settings['notifier'], 'uuid': text(uuid.uuid4()), } if self.settings.get('code_version'): data['code_version'] = self.settings['code_version'] if self.base_data_hook: self.base_data_hook(request, data) return data
def _trace_data(cls, exc, trace): # exception info # most recent call last raw_frames = traceback.extract_tb(trace) frames = [{'filename': f[0], 'lineno': f[1], 'method': f[2], 'code': f[3]} for f in raw_frames] trace_data = { 'frames': frames, 'exception': { 'class': getattr(cls, '__name__', cls.__class__.__name__), 'message': text(exc), } } _add_locals_data(trace_data, (cls, exc, trace)) return trace_data
def transform_dict(self, o, key=None): ret = {} for k, v in iteritems(o): if isinstance(k, string_types) or isinstance(k, binary_type): if python_major_version() < 3: if isinstance(k, unicode): new_k = self.transform_unicode(k) else: new_k = self.transform_py2_str(k) else: if isinstance(k, bytes): new_k = self.transform_py3_bytes(k) else: new_k = self.transform_unicode(k) else: new_k = text(k) ret[new_k] = v return super(SerializableTransform, self).transform_dict(ret, key=key)
from rollbar.lib import text, transforms from rollbar.lib.transforms.scrub_redact import ScrubRedactTransform, REDACT_REF from rollbar.test import BaseTest class NotRedactRef(): pass NOT_REDACT_REF = NotRedactRef() try: SCRUBBED = '*' * len(REDACT_REF) except: SCRUBBED = '*' * len(text(REDACT_REF)) class ScrubRedactTransformTest(BaseTest): def _assertScrubbed(self, start, expected, redact_char='*', skip_id_check=False): scrubber = ScrubRedactTransform(redact_char=redact_char, randomize_len=False) result = transforms.transform(start, scrubber) if not skip_id_check: self.assertNotEqual(id(result), id(expected)) self.assertEqual(type(result), type(expected)) if isinstance(result, Mapping): self.assertDictEqual(result, expected) elif isinstance(result, tuple):
from rollbar.lib import text, transforms from rollbar.lib.transforms.scrub_redact import ScrubRedactTransform, REDACT_REF from rollbar.test import BaseTest class NotRedactRef: pass NOT_REDACT_REF = NotRedactRef() try: SCRUBBED = "*" * len(REDACT_REF) except: SCRUBBED = "*" * len(text(REDACT_REF)) class ScrubRedactTransformTest(BaseTest): def _assertScrubbed(self, start, expected, redact_char="*", skip_id_check=False): scrubber = ScrubRedactTransform(redact_char=redact_char, randomize_len=False) result = transforms.transform(start, scrubber) if not skip_id_check: self.assertNotEqual(id(result), id(expected)) self.assertEqual(type(result), type(expected)) if isinstance(result, collections.Mapping): self.assertDictEqual(result, expected) elif isinstance(result, tuple):
def _shorten_basic(self, obj, max_len): val = text(obj) if len(val) <= max_len: return obj return self._repr.repr(val)
def _shorten_basic(self, obj, max_len): val = text(obj) if len(val) <= max_len: return obj return self._repr.repr(obj)