def test_transform_int_subclass(): class X(int): pass result = transform(X()) assert type(result) == int assert result == 0
def test_int_subclass(self): class X(int): pass result = transform(X()) self.assertEquals(type(result), int) self.assertEquals(result, 0)
def test_transform_dict_keys_utf8_as_unicode(): x = {compat.text_type("\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df"): "bar"} result = transform(x) keys = list(result.keys()) assert len(keys) == 1 assert type(keys[0]), str assert keys[0] == "\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df"
def test_transform_correct_unicode(): x = "רונית מגן" if compat.PY2: x = x.decode("utf-8") result = transform(x) assert type(result) == compat.text_type assert result == x
def test_correct_unicode(self): x = 'רונית מגן' if six.PY2: x = x.decode('utf-8') result = transform(x) self.assertEquals(type(result), six.text_type) self.assertEquals(result, x)
def test_dict_keys(self): x = {'foo': 'bar'} result = transform(x) self.assertEquals(type(result), dict) keys = list(result.keys()) self.assertEquals(len(keys), 1) self.assertTrue(type(keys[0]), str) self.assertEquals(keys[0], 'foo')
def test_transform_custom_repr(): class Foo(object): def __elasticapm__(self): return "example" x = Foo() result = transform(x) assert result == "example"
def test_custom_repr(self): class Foo(object): def __elasticapm__(self): return 'example' x = Foo() result = transform(x) self.assertEquals(result, 'example')
def test_transform_dict_keys(): x = {"foo": "bar"} result = transform(x) assert type(result) == dict keys = list(result.keys()) assert len(keys) == 1 assert type(keys[0]), str assert keys[0] == "foo"
def test_dict_keys_utf8_as_str(self): x = {'רונית מגן': 'bar'} result = transform(x) self.assertEquals(type(result), dict) keys = list(result.keys()) self.assertEquals(len(keys), 1) self.assertTrue(type(keys[0]), six.binary_type) if six.PY3: self.assertEquals(keys[0], 'רונית מגן') else: self.assertEquals(keys[0], u'רונית מגן')
def test_dict_keys_utf8_as_unicode(self): x = { six.text_type('\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df'): 'bar' } result = transform(x) keys = list(result.keys()) self.assertEquals(len(keys), 1) self.assertTrue(type(keys[0]), str) self.assertEquals(keys[0], '\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df')
def test_transform_dict_keys_utf8_as_str(): x = {"רונית מגן": "bar"} result = transform(x) assert type(result) == dict keys = list(result.keys()) assert len(keys) == 1 assert type(keys[0]), compat.binary_type if compat.PY3: assert keys[0] == "רונית מגן" else: assert keys[0] == u"רונית מגן"
def test_broken_repr(self): class Foo(object): def __repr__(self): raise ValueError x = Foo() result = transform(x) if six.PY2: expected = u"<BadRepr: <class 'tests.utils.encoding.tests.Foo'>>" else: expected = "<BadRepr: <class 'tests.utils.encoding.tests.TransformTest.test_broken_repr.<locals>.Foo'>>" self.assertEquals(result, expected)
def test_transform_broken_repr(): class Foo(object): def __repr__(self): raise ValueError x = Foo() result = transform(x) if compat.PY2: expected = u"<BadRepr: <class 'tests.utils.encoding.tests.Foo'>>" else: expected = "<BadRepr: <class 'tests.utils.encoding.tests.test_transform_broken_repr.<locals>.Foo'>>" assert result == expected
def _build_msg_for_logging(self, event_type, date=None, context=None, custom=None, stack=None, handled=True, **kwargs): """ Captures, processes and serializes an event into a dict object """ transaction = get_transaction() if transaction: transaction_context = deepcopy(transaction.context) else: transaction_context = {} event_data = {} if custom is None: custom = {} if not date: date = datetime.datetime.utcnow() if stack is None: stack = self.config.auto_log_stacks if context: transaction_context.update(context) context = transaction_context else: context = transaction_context event_data['context'] = context if transaction and transaction.tags: context['tags'] = deepcopy(transaction.tags) # if '.' not in event_type: # Assume it's a builtin event_type = 'elasticapm.events.%s' % event_type handler = self.get_handler(event_type) result = handler.capture(self, **kwargs) if self._filter_exception_type(result): return # data (explicit) culprit takes over auto event detection culprit = result.pop('culprit', None) if custom.get('culprit'): culprit = custom.pop('culprit') for k, v in compat.iteritems(result): if k not in event_data: event_data[k] = v log = event_data.get('log', {}) if stack and 'stacktrace' not in log: if stack is True: frames = stacks.iter_stack_frames(skip=3) else: frames = stack frames = stacks.get_stack_info( frames, with_locals=self.config.collect_local_variables in ('errors', 'all'), library_frame_context_lines=self.config. source_lines_error_library_frames, in_app_frame_context_lines=self.config. source_lines_error_app_frames, include_paths_re=self.include_paths_re, exclude_paths_re=self.exclude_paths_re, locals_processor_func=lambda local_var: varmap( lambda k, v: shorten( v, list_length=self.config.local_var_list_max_length, string_length=self.config.local_var_max_length, ), local_var)) log['stacktrace'] = frames if 'stacktrace' in log and not culprit: culprit = stacks.get_culprit(log['stacktrace'], self.config.include_paths, self.config.exclude_paths) if 'level' in log and isinstance(log['level'], compat.integer_types): log['level'] = logging.getLevelName(log['level']).lower() if log: event_data['log'] = log if culprit: event_data['culprit'] = culprit if 'custom' in context: context['custom'].update(custom) else: context['custom'] = custom # Run the data through processors for processor in self.processors: event_data = processor(self, event_data) # Make sure all data is coerced event_data = transform(event_data) if 'exception' in event_data: event_data['exception']['handled'] = bool(handled) event_data.update({ 'timestamp': date.strftime(constants.TIMESTAMP_FORMAT), }) transaction = get_transaction() if transaction: event_data['transaction'] = {'id': transaction.id} return self._build_msg({'errors': [event_data]})
def build_msg_for_logging(self, event_type, data=None, date=None, extra=None, stack=None, **kwargs): """ Captures, processes and serializes an event into a dict object """ if data is None: data = {} if extra is None: extra = {} if not date: date = datetime.datetime.utcnow() if stack is None: stack = self.auto_log_stacks if 'context' not in data: data['context'] = context = {} else: context = data['context'] # if '.' not in event_type: # Assume it's a builtin event_type = 'elasticapm.events.%s' % event_type handler = self.get_handler(event_type) result = handler.capture(self, data=data, **kwargs) if self._filter_exception_type(result): return # data (explicit) culprit takes over auto event detection culprit = result.pop('culprit', None) if data.get('culprit'): culprit = data['culprit'] for k, v in six.iteritems(result): if k not in data: data[k] = v log = data.get('log', {}) if stack and 'stacktrace' not in log: if stack is True: frames = iter_stack_frames() else: frames = stack frames = varmap( lambda k, v: shorten(v, string_length=self.string_max_length, list_length=self.list_max_length), stacks.get_stack_info(frames)) log['stacktrace'] = frames if 'stacktrace' in log and not culprit: culprit = get_culprit(log['stacktrace'], self.include_paths, self.exclude_paths) if 'level' in log and isinstance(log['level'], six.integer_types): log['level'] = logging.getLevelName(log['level']).lower() if log: data['log'] = log if culprit: data['culprit'] = culprit context['custom'] = extra # Run the data through processors for processor in self.processors: data = processor(self, data) # Make sure all data is coerced data = transform(data) data.update({ 'timestamp': date.strftime(defaults.TIMESTAMP_FORMAT), }) return self.build_msg({'errors': [data]})
def test_transform_bool(): result = transform(True) assert type(result) == bool assert result == True
def test_transform_float(): result = transform(13.0) assert type(result) == float assert result == 13.0
def test_transform_bad_string(): x = compat.b("The following character causes problems: \xd4") result = transform(x) assert type(result) == compat.binary_type assert result == compat.b("(Error decoding value)")
def test_bad_string(self): x = six.b('The following character causes problems: \xd4') result = transform(x) self.assertEquals(type(result), six.binary_type) self.assertEquals(result, six.b('(Error decoding value)'))
def test_transform_incorrect_unicode(): x = "רונית מגן" result = transform(x) assert type(result) == str assert result == "רונית מגן"
def test_transform_recursive(): x = [] x.append(x) result = transform(x) assert result == ["<...>"]
def test_float(self): result = transform(13.0) self.assertEquals(type(result), float) self.assertEquals(result, 13.0)
def test_transform_uuid(): x = uuid.uuid4() result = transform(x) assert result == repr(x) assert type(result), str
def test_recursive(self): x = [] x.append(x) result = transform(x) self.assertEquals(result, ['<...>'])
def _build_msg_for_logging(self, event_type, date=None, context=None, custom=None, stack=None, handled=True, **kwargs): """ Captures, processes and serializes an event into a dict object """ transaction = get_transaction() if transaction: transaction_context = deepcopy(transaction.context) else: transaction_context = {} event_data = {} if custom is None: custom = {} if date is not None: warnings.warn( "The date argument is no longer evaluated and will be removed in a future release", DeprecationWarning) date = time.time() if stack is None: stack = self.config.auto_log_stacks if context: transaction_context.update(context) context = transaction_context else: context = transaction_context event_data["context"] = context if transaction and transaction.tags: context["tags"] = deepcopy(transaction.tags) # if '.' not in event_type: # Assume it's a builtin event_type = "elasticapm.events.%s" % event_type handler = self.get_handler(event_type) result = handler.capture(self, **kwargs) if self._filter_exception_type(result): return # data (explicit) culprit takes over auto event detection culprit = result.pop("culprit", None) if custom.get("culprit"): culprit = custom.pop("culprit") for k, v in compat.iteritems(result): if k not in event_data: event_data[k] = v log = event_data.get("log", {}) if stack and "stacktrace" not in log: if stack is True: frames = stacks.iter_stack_frames(skip=3) else: frames = stack frames = stacks.get_stack_info( frames, with_locals=self.config.collect_local_variables in ("errors", "all"), library_frame_context_lines=self.config. source_lines_error_library_frames, in_app_frame_context_lines=self.config. source_lines_error_app_frames, include_paths_re=self.include_paths_re, exclude_paths_re=self.exclude_paths_re, locals_processor_func=lambda local_var: varmap( lambda k, v: shorten( v, list_length=self.config.local_var_list_max_length, string_length=self.config.local_var_max_length, ), local_var, ), ) log["stacktrace"] = frames if "stacktrace" in log and not culprit: culprit = stacks.get_culprit(log["stacktrace"], self.config.include_paths, self.config.exclude_paths) if "level" in log and isinstance(log["level"], compat.integer_types): log["level"] = logging.getLevelName(log["level"]).lower() if log: event_data["log"] = log if culprit: event_data["culprit"] = culprit if "custom" in context: context["custom"].update(custom) else: context["custom"] = custom # Make sure all data is coerced event_data = transform(event_data) if "exception" in event_data: event_data["exception"]["handled"] = bool(handled) event_data["timestamp"] = int(date * 1000000) transaction = get_transaction() if transaction: if transaction.trace_parent: event_data["trace_id"] = transaction.trace_parent.trace_id event_data["parent_id"] = transaction.id event_data["transaction_id"] = transaction.id return event_data
def get_frame_info(frame, lineno, with_source_context=True, with_locals=True, include_paths_re=None, exclude_paths_re=None): # Support hidden frames f_locals = getattr(frame, 'f_locals', {}) if _getitem_from_frame(f_locals, '__traceback_hide__'): return None f_globals = getattr(frame, 'f_globals', {}) loader = f_globals.get('__loader__') module_name = f_globals.get('__name__') f_code = getattr(frame, 'f_code', None) if f_code: abs_path = frame.f_code.co_filename function = frame.f_code.co_name else: abs_path = None function = None if lineno: lineno -= 1 # Try to pull a relative file path # This changes /foo/site-packages/baz/bar.py into baz/bar.py try: base_filename = sys.modules[module_name.split('.', 1)[0]].__file__ filename = abs_path.split(base_filename.rsplit('/', 2)[0], 1)[-1][1:] except Exception: filename = abs_path if not filename: filename = abs_path frame_result = { 'abs_path': abs_path, 'filename': filename, 'module': module_name, 'function': function, 'lineno': lineno + 1, 'library_frame': _is_library_frame(module_name, include_paths_re, exclude_paths_re) } if with_source_context: if lineno is not None and abs_path: pre_context, context_line, post_context = get_lines_from_file( abs_path, lineno, 3, loader, module_name) else: pre_context, context_line, post_context = [], None, [] if context_line: frame_result.update({ 'pre_context': pre_context, 'context_line': context_line, 'post_context': post_context, }) if with_locals: if f_locals is not None and not isinstance(f_locals, dict): # XXX: Genshi (and maybe others) have broken implementations of # f_locals that are not actually dictionaries try: f_locals = to_dict(f_locals) except Exception: f_locals = '<invalid local scope>' frame_result['vars'] = transform(f_locals) return frame_result
def test_bool(self): result = transform(True) self.assertEquals(type(result), bool) self.assertEquals(result, True)
def get_frame_info( frame, lineno, with_locals=True, library_frame_context_lines=None, in_app_frame_context_lines=None, include_paths_re=None, exclude_paths_re=None, locals_processor_func=None, ): # Support hidden frames f_locals = getattr(frame, "f_locals", {}) if _getitem_from_frame(f_locals, "__traceback_hide__"): return None f_globals = getattr(frame, "f_globals", {}) loader = f_globals.get("__loader__") module_name = f_globals.get("__name__") f_code = getattr(frame, "f_code", None) if f_code: abs_path = frame.f_code.co_filename function = frame.f_code.co_name else: abs_path = None function = None # Try to pull a relative file path # This changes /foo/site-packages/baz/bar.py into baz/bar.py try: base_filename = sys.modules[module_name.split(".", 1)[0]].__file__ filename = abs_path.split(base_filename.rsplit(os.path.sep, 2)[0], 1)[-1].lstrip(os.path.sep) except Exception: filename = abs_path if not filename: filename = abs_path frame_result = { "abs_path": abs_path, "filename": filename, "module": module_name, "function": function, "lineno": lineno, "library_frame": is_library_frame(abs_path, include_paths_re, exclude_paths_re), } context_lines = library_frame_context_lines if frame_result[ "library_frame"] else in_app_frame_context_lines if context_lines and lineno is not None and abs_path: pre_context, context_line, post_context = get_lines_from_file( abs_path, lineno, int(context_lines / 2), loader, module_name) else: pre_context, context_line, post_context = [], None, [] if context_line: frame_result["pre_context"] = pre_context frame_result["context_line"] = context_line frame_result["post_context"] = post_context if with_locals: if f_locals is not None and not isinstance(f_locals, dict): # XXX: Genshi (and maybe others) have broken implementations of # f_locals that are not actually dictionaries try: f_locals = to_dict(f_locals) except Exception: f_locals = "<invalid local scope>" if locals_processor_func: f_locals = { varname: locals_processor_func(var) for varname, var in compat.iteritems(f_locals) } frame_result["vars"] = transform(f_locals) return frame_result
def test_uuid(self): x = uuid.uuid4() result = transform(x) self.assertEquals(result, repr(x)) self.assertTrue(type(result), str)