def _test(): # Force intrinsics txn = current_transaction() current_trace().guid = sentinel_guid txn.guid = guid txn._priority = priority txn._sampled = txn_sampled function()
def test_sync_nr_disabled(httpx, server): global CAT_RESPONSE_CODE CAT_RESPONSE_CODE = 200 with httpx.Client() as client: trace = current_trace() response = client.get("http://localhost:%s" % server.port) assert response.status_code == 200 assert trace is None
def dynamic_wrapper(wrapped, instance, args, kwargs): wrapper = async_wrapper(wrapped) if not wrapper: parent = current_trace() if not parent: return wrapped(*args, **kwargs) else: parent = None if callable(name): if instance is not None: _name = name(instance, *args, **kwargs) else: _name = name(*args, **kwargs) elif name is None: _name = callable_name(wrapped) else: _name = name if callable(group): if instance is not None: _group = group(instance, *args, **kwargs) else: _group = group(*args, **kwargs) else: _group = group if callable(label): if instance is not None: _label = label(instance, *args, **kwargs) else: _label = label(*args, **kwargs) else: _label = label if callable(params): if instance is not None: _params = params(instance, *args, **kwargs) else: _params = params(*args, **kwargs) else: _params = params trace = FunctionTrace(_name, _group, _label, _params, terminal, rollup, parent=parent, source=wrapped) if wrapper: # pylint: disable=W0125,W0126 return wrapper(wrapped, trace)(*args, **kwargs) with trace: return wrapped(*args, **kwargs)
async def wrap_run_in_threadpool(wrapped, instance, args, kwargs): transaction = current_transaction() trace = current_trace() if not transaction or not trace: return await wrapped(*args, **kwargs) func, args, kwargs = bind_run_in_threadpool(*args, **kwargs) func = context_wrapper(func, trace) return await wrapped(func, *args, **kwargs)
def _nr_message_trace_wrapper_(wrapped, instance, args, kwargs): wrapper = async_wrapper(wrapped) if not wrapper: parent = current_trace() if not parent: return wrapped(*args, **kwargs) else: parent = None if callable(library): if instance is not None: _library = library(instance, *args, **kwargs) else: _library = library(*args, **kwargs) else: _library = library if callable(operation): if instance is not None: _operation = operation(instance, *args, **kwargs) else: _operation = operation(*args, **kwargs) else: _operation = operation if callable(destination_type): if instance is not None: _destination_type = destination_type(instance, *args, **kwargs) else: _destination_type = destination_type(*args, **kwargs) else: _destination_type = destination_type if callable(destination_name): if instance is not None: _destination_name = destination_name(instance, *args, **kwargs) else: _destination_name = destination_name(*args, **kwargs) else: _destination_name = destination_name trace = MessageTrace(_library, _operation, _destination_type, _destination_name, params={}, parent=parent, source=wrapped) if wrapper: # pylint: disable=W0125,W0126 return wrapper(wrapped, trace)(*args, **kwargs) with trace: return wrapped(*args, **kwargs)
def __init__(self, ignore_errors=[], **kwargs): parent = None if kwargs: if len(kwargs) > 1: raise TypeError("Invalid keyword arguments:", kwargs) parent = kwargs['parent'] if parent is None: parent = current_trace() self._transaction = parent and parent.transaction self._ignore_errors = ignore_errors
def wrap_run_endpoint_function(wrapped, instance, args, kwargs): trace = current_trace() if trace and trace.transaction: dependant = kwargs["dependant"] name = callable_name(dependant.call) trace.transaction.set_transaction_name(name) dependant = kwargs["dependant"] = copy(dependant) dependant.call = FunctionTraceWrapper(dependant.call) return wrapped(*args, **kwargs) return wrapped(*args, **kwargs)
def wrapper(wrapped, instance, args, kwargs): parent = current_trace() if parent is None: return wrapped(*args, **kwargs) with ErrorTrace(ignore_errors, ignore, expected, status_code, parent=parent): return wrapped(*args, **kwargs)
def test_async_nr_disabled(httpx, server, loop): global CAT_RESPONSE_CODE CAT_RESPONSE_CODE = 200 async def _test(): async with httpx.AsyncClient() as client: response = await client.get("http://localhost:%s" % server.port) return response trace = current_trace() response = loop.run_until_complete(_test()) assert response.status_code == 200 assert trace is None
def wrap_execute_operation(wrapped, instance, args, kwargs): transaction = current_transaction() trace = current_trace() if not transaction: return wrapped(*args, **kwargs) if not isinstance(trace, GraphQLOperationTrace): _logger.warning( "Runtime instrumentation warning. GraphQL operation found without active GraphQLOperationTrace." ) return wrapped(*args, **kwargs) try: operation = bind_operation_v3(*args, **kwargs) except TypeError: try: operation = bind_operation_v2(*args, **kwargs) except TypeError: return wrapped(*args, **kwargs) if graphql_version() < (3, 0): execution_context = args[0] else: execution_context = instance trace.operation_name = get_node_value(operation, "name") or "<anonymous>" trace.operation_type = get_node_value(operation, "operation", "name").lower() or "<unknown>" if operation.selection_set is not None: fields = operation.selection_set.selections # Ignore transactions for introspection queries for field in fields: if get_node_value(field, "name") in GRAPHQL_INTROSPECTION_FIELDS: ignore_transaction() fragments = execution_context.fragments trace.deepest_path = ".".join(traverse_deepest_unique_path(fields, fragments)) or "" transaction.set_transaction_name(callable_name(wrapped), "GraphQL", priority=11) result = wrapped(*args, **kwargs) if not execution_context.errors: if hasattr(trace, "set_transaction_name"): # Operation trace sets transaction name trace.set_transaction_name(priority=14) return result
def wrap_run_endpoint_function(wrapped, instance, args, kwargs): trace = current_trace() if trace and trace.transaction: dependant = kwargs["dependant"] name = callable_name(dependant.call) trace.transaction.set_transaction_name(name) if not kwargs["is_coroutine"]: dependant = kwargs["dependant"] = copy(dependant) dependant.call = use_context(trace)(FunctionTraceWrapper( dependant.call)) return wrapped(*args, **kwargs) else: return FunctionTraceWrapper(wrapped, name=name)(*args, **kwargs) return wrapped(*args, **kwargs)
def _nr_graphql_trace_wrapper_(wrapped, instance, args, kwargs): wrapper = async_wrapper(wrapped) if not wrapper: parent = current_trace() if not parent: return wrapped(*args, **kwargs) else: parent = None trace = GraphQLResolverTrace(parent=parent, source=wrapped) if wrapper: # pylint: disable=W0125,W0126 return wrapper(wrapped, trace)(*args, **kwargs) with trace: return wrapped(*args, **kwargs)
def literal_wrapper(wrapped, instance, args, kwargs): wrapper = async_wrapper(wrapped) if not wrapper: parent = current_trace() if not parent: return wrapped(*args, **kwargs) else: parent = None trace = ExternalTrace(library, url, method, parent=parent) wrapper = async_wrapper(wrapped) if wrapper: return wrapper(wrapped, trace)(*args, **kwargs) with trace: return wrapped(*args, **kwargs)
def literal_wrapper(wrapped, instance, args, kwargs): wrapper = async_wrapper(wrapped) if not wrapper: parent = current_trace() if not parent: return wrapped(*args, **kwargs) else: parent = None _name = name or callable_name(wrapped) trace = FunctionTrace(_name, group, label, params, terminal, rollup, parent=parent, source=wrapped) if wrapper: # pylint: disable=W0125,W0126 return wrapper(wrapped, trace)(*args, **kwargs) with trace: return wrapped(*args, **kwargs)
def _nr_datastore_trace_wrapper_(wrapped, instance, args, kwargs): wrapper = async_wrapper(wrapped) if not wrapper: parent = current_trace() if not parent: return wrapped(*args, **kwargs) else: parent = None if callable(product): if instance is not None: _product = product(instance, *args, **kwargs) else: _product = product(*args, **kwargs) else: _product = product if callable(target): if instance is not None: _target = target(instance, *args, **kwargs) else: _target = target(*args, **kwargs) else: _target = target if callable(operation): if instance is not None: _operation = operation(instance, *args, **kwargs) else: _operation = operation(*args, **kwargs) else: _operation = operation trace = DatastoreTrace(_product, _target, _operation, parent=parent, source=wrapped) if wrapper: # pylint: disable=W0125,W0126 return wrapper(wrapped, trace)(*args, **kwargs) with trace: return wrapped(*args, **kwargs)
def _generator(generator): _gname = '%s (generator)' % _name try: value = None exc = None while True: parent = current_trace() params = {} frame = generator.gi_frame params['filename'] = frame.f_code.co_filename params['lineno'] = frame.f_lineno with FunctionTrace(_gname, _group, params=params, parent=parent): try: if exc is not None: yielded = generator.throw(*exc) exc = None else: yielded = generator.send(value) except StopIteration: break except Exception: raise try: value = yield yielded except Exception: exc = sys.exc_info() finally: generator.close()
def __init__( self, ignore_errors=[], ignore=None, expected=None, status_code=None, parent=None, ): if parent is None: parent = current_trace() self._transaction = parent and parent.transaction self._ignore = ignore if ignore is not None else ignore_errors self._expected = expected self._status_code = status_code if ignore_errors: warnings.warn( ("The ignore_errors argument is deprecated. Please use the " "new ignore argument instead."), DeprecationWarning, )
def dynamic_wrapper(wrapped, instance, args, kwargs): wrapper = async_wrapper(wrapped) if not wrapper: parent = current_trace() if not parent: return wrapped(*args, **kwargs) else: parent = None if callable(url): if instance is not None: _url = url(instance, *args, **kwargs) else: _url = url(*args, **kwargs) else: _url = url if callable(method): if instance is not None: _method = method(instance, *args, **kwargs) else: _method = method(*args, **kwargs) else: _method = method trace = ExternalTrace(library, _url, _method, parent=parent, source=wrapped) if wrapper: # pylint: disable=W0125,W0126 return wrapper(wrapped, trace)(*args, **kwargs) with trace: return wrapped(*args, **kwargs)
def _nr_wrapper_memcache_trace_(wrapped, instance, args, kwargs): wrapper = async_wrapper(wrapped) if not wrapper: parent = current_trace() if not parent: return wrapped(*args, **kwargs) else: parent = None if callable(command): if instance is not None: _command = command(instance, *args, **kwargs) else: _command = command(*args, **kwargs) else: _command = command trace = MemcacheTrace(_command, parent=parent) if wrapper: return wrapper(wrapped, trace)(*args, **kwargs) with trace: return wrapped(*args, **kwargs)
def _nr_database_trace_wrapper_(wrapped, instance, args, kwargs): wrapper = async_wrapper(wrapped) if not wrapper: parent = current_trace() if not parent: return wrapped(*args, **kwargs) else: parent = None if callable(sql): if instance is not None: _sql = sql(instance, *args, **kwargs) else: _sql = sql(*args, **kwargs) else: _sql = sql trace = DatabaseTrace(_sql, dbapi2_module, parent=parent) if wrapper: return wrapper(wrapped, trace)(*args, **kwargs) with trace: return wrapped(*args, **kwargs)
def test_current_span_id_inside_transaction(): span_id = current_span_id() assert span_id == current_trace().guid
def main_wrap_wrapper(wrapped, instance, args, kwargs): awaitable = wrapped(*args, **kwargs) return context_wrapper_async(awaitable, current_trace(), strict=False)
def __call__(self, frame, event, arg): if event not in [ 'call', 'c_call', 'return', 'c_return', 'exception', 'c_exception' ]: return parent = current_trace() if not parent: return # Not sure if setprofile() is reliable in the face of # coroutine systems based on greenlets so don't run # if we detect may be using greenlets. if (hasattr(sys, '_current_frames') and parent.thread_id not in sys._current_frames()): return co = frame.f_code func_name = co.co_name func_line_no = frame.f_lineno func_filename = co.co_filename def _callable_name(): # A stack frame doesn't provide any information about the # original callable object. We thus need to try and # deduce what it is by searching through the stack # frame globals. This will still not work in many # cases, including lambdas, generator expressions, # and decorated attributes such as properties of # classes. try: if func_name in frame.f_globals: if frame.f_globals[func_name].func_code is co: return callable_name(frame.f_globals[func_name]) except Exception: pass for name, obj in six.iteritems(frame.f_globals): try: if obj.__dict__[func_name].func_code is co: return callable_name(obj.__dict__[func_name]) except Exception: pass if event in ['call', 'c_call']: # Skip the outermost as we catch that with the root # function traces for the profile trace. if len(self.function_traces) == 0: self.function_traces.append(None) return if self.current_depth >= self.maximum_depth: self.function_traces.append(None) return if func_filename.startswith(AGENT_PACKAGE_DIRECTORY): self.function_traces.append(None) return if event == 'call': name = _callable_name() if not name: name = '%s:%s#%s' % (func_filename, func_name, func_line_no) else: name = callable_name(arg) if not name: name = '%s:@%s#%s' % (func_filename, func_name, func_line_no) function_trace = FunctionTrace(name=name, parent=parent) function_trace.__enter__() self.function_traces.append(function_trace) self.current_depth += 1 elif event in ['return', 'c_return', 'c_exception']: if not self.function_traces: return try: function_trace = self.function_traces.pop() except IndexError: pass else: if function_trace: function_trace.__exit__(None, None, None) self.current_depth -= 1
def wrapper(wrapped, instance, args, kwargs): parent = current_trace() if parent is None: return wrapped(*args, **kwargs) if callable(name): if instance is not None: _name = name(instance, *args, **kwargs) else: _name = name(*args, **kwargs) elif name is None: _name = callable_name(wrapped) else: _name = name if callable(group): if instance is not None: _group = group(instance, *args, **kwargs) else: _group = group(*args, **kwargs) else: _group = group if callable(label): if instance is not None: _label = label(instance, *args, **kwargs) else: _label = label(*args, **kwargs) else: _label = label if callable(params): if instance is not None: _params = params(instance, *args, **kwargs) else: _params = params(*args, **kwargs) else: _params = params with FunctionTrace(_name, _group, _label, _params, parent=parent): if not hasattr(sys, 'getprofile'): return wrapped(*args, **kwargs) profiler = sys.getprofile() if profiler: return wrapped(*args, **kwargs) sys.setprofile(ProfileTrace(depth)) try: return wrapped(*args, **kwargs) finally: sys.setprofile(None)
def wrap_request(wrapped, instance, args, kwargs): result = wrapped(*args, **kwargs) instance._nr_trace = current_trace() return result
def wrapper(wrapped, instance, args, kwargs): parent = current_trace() if parent is None: return wrapped(*args, **kwargs) if callable(name): if instance is not None: _name = name(instance, *args, **kwargs) else: _name = name(*args, **kwargs) elif name is None: _name = callable_name(wrapped) else: _name = name if callable(group): if instance is not None: _group = group(instance, *args, **kwargs) else: _group = group(*args, **kwargs) else: _group = group if callable(label): if instance is not None: _label = label(instance, *args, **kwargs) else: _label = label(*args, **kwargs) else: _label = label if callable(params): if instance is not None: _params = params(instance, *args, **kwargs) else: _params = params(*args, **kwargs) else: _params = params def _generator(generator): _gname = '%s (generator)' % _name try: value = None exc = None while True: parent = current_trace() params = {} frame = generator.gi_frame params['filename'] = frame.f_code.co_filename params['lineno'] = frame.f_lineno with FunctionTrace(_gname, _group, params=params, parent=parent): try: if exc is not None: yielded = generator.throw(*exc) exc = None else: yielded = generator.send(value) except StopIteration: break except Exception: raise try: value = yield yielded except Exception: exc = sys.exc_info() finally: generator.close() with FunctionTrace(_name, _group, _label, _params, parent=parent): try: result = wrapped(*args, **kwargs) except: # Catch all raise else: if isinstance(result, types.GeneratorType): return _generator(result) else: return result
def function(): current_trace().guid = function_guid child()