def httplib_getresponse_wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) connection = instance tracer = getattr(connection, '_bw_external_tracer', None) if not tracer: return wrapped(*args, **kwargs) response = wrapped(*args, **kwargs) # Make sure we remove the tracer from the connection object so that it # doesn't hold onto objects. Do this after we call the wrapped function so # if an exception occurs the higher library might retry the call again with # the same connection object. Both urllib3 and requests do this in Py2.7 del connection._bw_external_tracer if hasattr(tracer, 'process_response_headers'): tracer.process_response_headers(response.getheaders()) return response
def callback_wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) name = callable_name(wrapped) # Needs to be at a higher priority so that error handler processing # below will not override the web transaction being named after the # actual request handler. transaction.set_transaction_name(name, priority=2) with FunctionTrace(transaction, name): try: return wrapped(*args, **kwargs) except: # Catch all # In most cases this seems like it will never be invoked as # bottle will internally capture the exception before we # get a chance and rather than propagate the exception will # return it instead. This doesn't always seem to be the case # though when plugins are used, although that may depend on # the specific bottle version being used. transaction.record_exception(ignore_errors=should_ignore) raise
def httplib_connect_wrapper(wrapped, instance, args, kwargs, scheme): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) def _connect_unbound(instance, *args, **kwargs): return instance if instance is None: instance = _connect_unbound(*args, **kwargs) connection = instance url = '%s://%s' % (scheme, connection.host) with ExternalTrace(transaction, library='httplib', url=url) as tracer: # Add the tracer to the connection object. The tracer will be # used in getresponse() to add back into the external trace, # after the trace has already completed, details from the # response headers. connection._bw_external_tracer = tracer return wrapped(*args, **kwargs)
def wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() def _wrapped(request, view_func, view_args, view_kwargs): # This strips the view handler wrapper before call. if hasattr(view_func, '_bw_last_object'): view_func = view_func._bw_last_object return wrapped(request, view_func, view_args, view_kwargs) if transaction is None: return _wrapped(*args, **kwargs) before = (transaction.name, transaction.group) with FunctionTrace(transaction, name=name): try: return _wrapped(*args, **kwargs) finally: # We want to name the transaction after this # middleware but only if the transaction wasn't # named from within the middleware itself explicity. after = (transaction.name, transaction.group) if before == after: transaction.set_transaction_name(name, priority=2)
def callback_wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) name = callable_name(wrapped) # Needs to be at a higher priority so that error handler processing # below will not override the web transaction being named after the # actual request handler. transaction.set_transaction_name(name, priority=2) with FunctionTrace(transaction, name): try: return wrapped(*args, **kwargs) except: # Catch all # In most cases this seems like it will never be invoked as # bottle will internally capture the exception before we # get a chance and rather than propagate the exception will # return it instead. This doesn't always seem to be the case # though when plugins are used, although that may depend on # the specific bottle version being used. transaction.record_exception(ignore_errors=should_ignore) raise
def __call__(self, *args): # Temporary work around due to customer calling class method # directly with 'self' as first argument. Need to work out best # practice for dealing with this. if len(args) == 2: # Assume called as unbound method with (self, request). instance, request = args else: # Assume called as bound method with (request). instance = self._bw_instance request = args[-1] assert instance != None transaction = current_transaction() if transaction is None: return self._bw_next_object(*args) # This is wrapping the render() function of the resource. We # name the function node and the web transaction after the name # of the handler function augmented with the method type for the # request. name = "%s.render_%s" % ( callable_name( instance), request.method) transaction.set_transaction_name(name, priority=1) with FunctionTrace(transaction, name): return self._bw_next_object(*args)
def _bw_wrapper_Elasticsearch_method_(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) # When arg_extractor is None, it means there is no target field # associated with this method. Hence this method will only # create an operation metric and no statement metric. This is # handled by setting the target to None when calling the # DatastoreTrace. if arg_extractor is None: index = None else: index = arg_extractor(*args, **kwargs) if prefix: operation = '%s.%s' % (prefix, name) else: operation = name with DatastoreTrace(transaction, product='Elasticsearch', target=index, operation=operation): return wrapped(*args, **kwargs)
def __call__(self, *args, **kwargs): transaction = current_transaction() if transaction and self.__instance: return stream_wrapper(self.__wrapped(*args, **kwargs), self.__instance.filepath) else: return self.__wrapped(*args, **kwargs)
def wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() def _wrapped(request, view_func, view_args, view_kwargs): # This strips the view handler wrapper before call. if hasattr(view_func, '_bw_last_object'): view_func = view_func._bw_last_object return wrapped(request, view_func, view_args, view_kwargs) if transaction is None: return _wrapped(*args, **kwargs) before = (transaction.name, transaction.group) with FunctionTrace(transaction, name=name): try: return _wrapped(*args, **kwargs) finally: # We want to name the transaction after this # middleware but only if the transaction wasn't # named from within the middleware itself explicity. after = (transaction.name, transaction.group) if before == after: transaction.set_transaction_name(name, priority=2)
def wrapper_GearmanConnectionManager_handle_function(wrapped, instance, args, kwargs): def _bind_params(current_connection, *args, **kwargs): return current_connection transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) tracer = transaction.active_node() if not isinstance(tracer, ExternalTrace): return wrapped(*args, **kwargs) # Now override the URL for the external to be the specific server we # ended up communicating with. This could get overridden multiple # times in the context of a single poll_connections_until_stopped() # call and so will be set to the last server data was processed for. # This thus may not necessarily be correct if commnicating with # multiple servers and data from more than one was being handled for # some reason. Can't really do much better than this though but will # be fine for the expected typical use case of a single server. if not tracer.url.startswith('gearman:'): return wrapped(*args, **kwargs) current_connection = _bind_params(*args, **kwargs) tracer.url = 'gearman://%s:%s' % (current_connection.gearman_host, current_connection.gearman_port) return wrapped(*args, **kwargs)
def _bw_wrapper_Elasticsearch_method_(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) # When arg_extractor is None, it means there is no target field # associated with this method. Hence this method will only # create an operation metric and no statement metric. This is # handled by setting the target to None when calling the # DatastoreTrace. if arg_extractor is None: index = None else: index = arg_extractor(*args, **kwargs) if prefix: operation = '%s.%s' % (prefix, name) else: operation = name with DatastoreTrace(transaction, product='Elasticsearch', target=index, operation=operation): return wrapped(*args, **kwargs)
def wrapper_RoutesDispatcher_find_handler(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) try: # Call the original wrapped function to find the handler. handler = wrapped(*args, **kwargs) except: # Catch all # Can end up here when the URL was invalid in some way. transaction.record_exception() raise if handler: # Should be the actual handler, wrap it with the handler # wrapper. handler = handler_wrapper(handler) else: # No handler could be found so name the web transaction # after the 404 status code. transaction.set_transaction_name('404', group='StatusCode') return handler
def wrapper_GearmanConnectionManager_handle_function(wrapped, instance, args, kwargs): def _bind_params(current_connection, *args, **kwargs): return current_connection transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) tracer = transaction.active_node() if not isinstance(tracer, ExternalTrace): return wrapped(*args, **kwargs) # Now override the URL for the external to be the specific server we # ended up communicating with. This could get overridden multiple # times in the context of a single poll_connections_until_stopped() # call and so will be set to the last server data was processed for. # This thus may not necessarily be correct if commnicating with # multiple servers and data from more than one was being handled for # some reason. Can't really do much better than this though but will # be fine for the expected typical use case of a single server. if not tracer.url.startswith('gearman:'): return wrapped(*args, **kwargs) current_connection = _bind_params(*args, **kwargs) tracer.url = 'gearman://%s:%s' % (current_connection.gearman_host, current_connection.gearman_port) return wrapped(*args, **kwargs)
def httplib_endheaders_wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) connection = instance # Check if the OA headers have already been added. This is just in # case a higher level library which uses httplib underneath so # happened to have been instrumented to also add the headers. try: skip_headers = getattr(connection, '_bw_skip_headers', False) if skip_headers: return wrapped(*args, **kwargs) outgoing_headers = ExternalTrace.generate_request_headers(transaction) for header_name, header_value in outgoing_headers: connection.putheader(header_name, header_value) return wrapped(*args, **kwargs) finally: try: del connection._bw_skip_headers except AttributeError: pass
def __call__(self, *args, **kwargs): transaction = current_transaction() if transaction and self.__instance: return stream_wrapper(self.__wrapped(*args, **kwargs), self.__instance.filepath) else: return self.__wrapped(*args, **kwargs)
def wrapper_RoutesDispatcher_find_handler(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) try: # Call the original wrapped function to find the handler. handler = wrapped(*args, **kwargs) except: # Catch all # Can end up here when the URL was invalid in some way. transaction.record_exception() raise if handler: # Should be the actual handler, wrap it with the handler # wrapper. handler = handler_wrapper(handler) else: # No handler could be found so name the web transaction # after the 404 status code. transaction.set_transaction_name('404', group='StatusCode') return handler
def __call__(self, url, *args, **kwargs): # The URL be a string or a file like object. Pass call # through if not a string. if not isinstance(url, six.string_types): return self._bw_next_object(url, *args, **kwargs) # Only then wrap the call if it looks like a URL. To # work that out need to first do some conversions of # accepted 'feed' formats to proper URL format. parsed_url = url if parsed_url.startswith('feed:http'): parsed_url = parsed_url[5:] elif parsed_url.startswith('feed:'): parsed_url = 'http:' + url[5:] if parsed_url.split(':')[0].lower() in ['http', 'https', 'ftp']: transaction = current_transaction() if current_transaction: trace = ExternalTrace(transaction, 'feedparser', parsed_url, 'GET') context_manager = trace.__enter__() try: result = self._bw_next_object(url, *args, **kwargs) except: # Catch all context_manager.__exit__(*sys.exc_info()) raise context_manager.__exit__(None, None, None) return result else: return self._bw_next_object(url, *args, **kwargs) else: return self._bw_next_object(url, *args, **kwargs)
def wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) with FunctionTrace(transaction, name=name): return wrapped(*args, **kwargs)
def __iter__(self): name = callable_name(self._bw_wrapped) iterable = iter(self._bw_generator) while 1: transaction = current_transaction() with FunctionTrace( transaction, name, group='Python/Twisted/Generator'): yield next(iterable)
def wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) with FunctionTrace(transaction, name=name): return wrapped(*args, **kwargs)
def callproc(self, procname, parameters=DEFAULT): transaction = current_transaction() with DatabaseTrace(transaction, 'CALL %s' % procname, self._bw_dbapi2_module): if parameters is not DEFAULT: return self.__wrapped__.callproc(procname, parameters) else: return self.__wrapped__.callproc(procname)
def transaction_name_delegate(*args, **kwargs): transaction = current_transaction() if transaction: if isinstance(args[1], six.string_types): f = args[1] else: f = callable_name(args[1]) transaction.set_transaction_name(f) return (args, kwargs)
def wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) with FunctionTrace(transaction, name=instance.name, group='Template/Block'): return wrapped(*args, **kwargs)
def __call__(self, *args, **kwargs): transaction = current_transaction() if transaction: transaction.set_transaction_name(self._bw_name, priority=self._bw_priority) with FunctionTrace(transaction, name=self._bw_name): return self._bw_wrapped(*args, **kwargs) else: return self._bw_wrapped(*args, **kwargs)
def retrieve_current_transaction(): # Retrieves the current transaction regardless of whether it has # been stopped or ignored. We can't just return the current # transaction gated by whether the transaction has been stopped or # is being ignored as that would screw things up because of all the # funny checks we need to do against the transaction when resuming a # transaction and so on. return current_transaction(active_only=False)
def wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) with FunctionTrace(transaction, name=name): callback, param_dict = wrapped(*args, **kwargs) return (wrap_view_handler(callback, priority=priority), param_dict)
def execute(self, sql, parameters=DEFAULT): transaction = current_transaction() if parameters is not DEFAULT: with DatabaseTrace(transaction, sql, self._bw_dbapi2_module, self._bw_connect_params, None, parameters): return self.__wrapped__.execute(sql, parameters) else: with DatabaseTrace(transaction, sql, self._bw_dbapi2_module, self._bw_connect_params): return self.__wrapped__.execute(sql)
def wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) with FunctionTrace(transaction, name=instance.name, group='Template/Block'): return wrapped(*args, **kwargs)
def wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) with FunctionTrace(transaction, name=name): callback, param_dict = wrapped(*args, **kwargs) return (wrap_view_handler(callback, priority=priority), param_dict)
def execute(self, sql, parameters=DEFAULT): transaction = current_transaction() if parameters is not DEFAULT: with DatabaseTrace(transaction, sql, self._bw_dbapi2_module, self._bw_connect_params, None, parameters): return self.__wrapped__.execute(sql, parameters) else: with DatabaseTrace(transaction, sql, self._bw_dbapi2_module, self._bw_connect_params): return self.__wrapped__.execute(sql)
def __call__(self, template, *args, **kwargs): transaction = current_transaction() if transaction: if hasattr(template, 'filename'): name = template.filename or '<template>' with FunctionTrace(transaction, name=name, group='Template/Render'): return self.__wrapped(template, *args, **kwargs) else: return self.__wrapped(template, *args, **kwargs) else: return self.__wrapped(template, *args, **kwargs)
def __call__(self, *args, **kwargs): transaction = current_transaction() settings = global_settings() rollup = 'Database/all' with FunctionTrace(transaction, callable_name(self.__wrapped__), terminal=True, rollup=rollup): return self.__connection_wrapper__(self.__wrapped__( *args, **kwargs), self._bw_dbapi2_module, (args, kwargs))
def _bw_wrapper_Flask_before_request_wrapped_(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) name = callable_name(wrapped) transaction.set_transaction_name(name) with FunctionTrace(transaction, name): return wrapped(*args, **kwargs)
def wrapper_Resource_method(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) name = callable_name(wrapped) transaction.set_transaction_name(name) with FunctionTrace(transaction, name): return wrapped(*args, **kwargs)
def _bw_wrapper_Flask_before_request_wrapped_(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) name = callable_name(wrapped) transaction.set_transaction_name(name) with FunctionTrace(transaction, name): return wrapped(*args, **kwargs)
def __exit__(self, exc, value, tb): transaction = current_transaction() name = callable_name(self.__wrapped__.__exit__) with FunctionTrace(transaction, name): if exc is None: with DatabaseTrace(transaction, 'COMMIT', self._bw_dbapi2_module): return self.__wrapped__.__exit__(exc, value, tb) else: with DatabaseTrace(transaction, 'ROLLBACK', self._bw_dbapi2_module): return self.__wrapped__.__exit__(exc, value, tb)
def __call__(self, *args, **kwargs): transaction = current_transaction() if not transaction: return self._bw_next_object(*args, **kwargs) result = self._bw_next_object(*args, **kwargs) if not result: return result return iter(InlineGeneratorWrapper(self._bw_next_object, result))
def __exit__(self, exc, value, tb): transaction = current_transaction() name = callable_name(self.__wrapped__.__exit__) with FunctionTrace(transaction, name): if exc is None: with DatabaseTrace(transaction, 'COMMIT', self._bw_dbapi2_module): return self.__wrapped__.__exit__(exc, value, tb) else: with DatabaseTrace(transaction, 'ROLLBACK', self._bw_dbapi2_module): return self.__wrapped__.__exit__(exc, value, tb)
def __enter__(self): transaction = current_transaction() name = callable_name(self.__wrapped__.__enter__) with FunctionTrace(transaction, name): self.__wrapped__.__enter__() # Must return a reference to self as otherwise will be # returning the inner connection object. If 'as' is used # with the 'with' statement this will mean no longer # using the wrapped connection object and nothing will be # tracked. return self
def __call__(self, request, response, session): txn = current_transaction() if txn: HTTP = import_module('gluon.http').HTTP try: return self._bw_next_object(request, response, session) except HTTP: raise except: # Catch all txn.record_exception(*sys.exc_info()) raise else: return self._bw_next_object(request, response, session)
def __enter__(self): transaction = current_transaction() name = callable_name(self.__wrapped__.__enter__) with FunctionTrace(transaction, name): self.__wrapped__.__enter__() # Must return a reference to self as otherwise will be # returning the inner connection object. If 'as' is used # with the 'with' statement this will mean no longer # using the wrapped connection object and nothing will be # tracked. return self
def __call__(self, *args, **kwargs): transaction = current_transaction() if transaction: webob_exc = import_module('webob.exc') try: return self.__wrapped(*args, **kwargs) except webob_exc.HTTPException: raise except: # Catch all transaction.record_exception(*sys.exc_info()) raise else: return self.__wrapped(*args, **kwargs)
def __call__(self, *args, **kwargs): transaction = current_transaction() if transaction: webob_exc = import_module('webob.exc') try: return self.__wrapped(*args, **kwargs) except webob_exc.HTTPException: raise except: # Catch all transaction.record_exception(*sys.exc_info()) raise else: return self.__wrapped(*args, **kwargs)
def __enter__(self): transaction = current_transaction() name = callable_name(self.__wrapped__.__enter__) with FunctionTrace(transaction, name): cursor = self.__wrapped__.__enter__() # The __enter__() method of original connection object returns # a new cursor instance for use with 'as' assignment. We need # to wrap that in a cursor wrapper otherwise we will not track # any queries done via it. return self.__cursor_wrapper__(cursor, self._bw_dbapi2_module, self._bw_connect_params, None)
def __enter__(self): transaction = current_transaction() name = callable_name(self.__wrapped__.__enter__) with FunctionTrace(transaction, name): cursor = self.__wrapped__.__enter__() # The __enter__() method of original connection object returns # a new cursor instance for use with 'as' assignment. We need # to wrap that in a cursor wrapper otherwise we will not track # any queries done via it. return self.__cursor_wrapper__(cursor, self._bw_dbapi2_module, self._bw_connect_params, None)
def wrapper_Dispatcher_find_handler(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) try: # Call the original wrapped function to find the handler. obj, vpath = wrapped(*args, **kwargs) except: # Catch all # Can end up here when a custom _cp_dispatch() method is # used and that raises an exception. transaction.record_exception() raise if obj: if instance.__class__.__name__ == 'MethodDispatcher': # We initially name the web transaction as if the # corresponding method for the HTTP method will not # be found and the request will not be allowed. This # will be overridden with the actual handler name # when the subsequent wrapper around the handler is # executed. transaction.set_transaction_name('405', group='StatusCode') # We have to use a custom object proxy here in order # to intercept accesses made by the dispatcher on the # returned object, after this function returns, to # retrieve the method of the object corresponding to # the HTTP method. For each such access we wrap what # is returned in the handler wrapper. obj = ResourceProxy(obj) else: # Should be the actual handler, wrap it with the # handler wrapper. obj = handler_wrapper(obj) else: # No handler could be found so name the web transaction # after the 404 status code. transaction.set_transaction_name('404', group='StatusCode') return obj, vpath
def wrapper_Dispatcher_find_handler(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) try: # Call the original wrapped function to find the handler. obj, vpath = wrapped(*args, **kwargs) except: # Catch all # Can end up here when a custom _cp_dispatch() method is # used and that raises an exception. transaction.record_exception() raise if obj: if instance.__class__.__name__ == 'MethodDispatcher': # We initially name the web transaction as if the # corresponding method for the HTTP method will not # be found and the request will not be allowed. This # will be overridden with the actual handler name # when the subsequent wrapper around the handler is # executed. transaction.set_transaction_name('405', group='StatusCode') # We have to use a custom object proxy here in order # to intercept accesses made by the dispatcher on the # returned object, after this function returns, to # retrieve the method of the object corresponding to # the HTTP method. For each such access we wrap what # is returned in the handler wrapper. obj = ResourceProxy(obj) else: # Should be the actual handler, wrap it with the # handler wrapper. obj = handler_wrapper(obj) else: # No handler could be found so name the web transaction # after the 404 status code. transaction.set_transaction_name('404', group='StatusCode') return obj, vpath
def wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) def _args(request, *args, **kwargs): return request view = instance request = _args(*args, **kwargs) # We can't intercept the delegated view handler when it # is looked up by the dispatch() method so we need to # duplicate the lookup mechanism. if request.method.lower() in view.http_method_names: handler = getattr(view, request.method.lower(), view.http_method_not_allowed) else: handler = view.http_method_not_allowed name = callable_name(handler) # The priority to be used when naming the transaction is # bit tricky. If the transaction name is already that of # the class based view, but not the method, then we want # the name of the method to override. This can occur # where the class based view was registered directly in # urls.py as the view handler. In this case we use the # priority of 5, matching what would be used by the view # handler so that it can override the transaction name. # # If however the transaction name is unrelated, we # preferably don't want it overridden. This can happen # where the class based view was invoked explicitly # within an existing view handler. In this case we use # the priority of 4 so it will not override the view # handler name where used as the transaction name. priority = 4 if transaction.group == 'Function': if transaction.name == callable_name(view): priority = 5 transaction.set_transaction_name(name, priority=priority) with FunctionTrace(transaction, name=name): return wrapped(*args, **kwargs)
def __call__(self, environ, start_response): transaction = current_transaction() if transaction is None: return self.wsgi_application(environ, start_response) name = callable_name(self.wsgi_application) with FunctionTrace(transaction, name='Application', group='Python/WSGI'): with FunctionTrace(transaction, name=name): result = self.wsgi_application(environ, start_response) return _WSGIApplicationIterable(transaction, result)
def wrapper(wrapped, instance, args, kwargs): transaction = current_transaction() if transaction is None: return wrapped(*args, **kwargs) transaction.set_transaction_name(name, priority=priority) with FunctionTrace(transaction, name=name): try: return wrapped(*args, **kwargs) except: # Catch all transaction.record_exception(ignore_errors=should_ignore) raise