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._nr_instance request = args[-1] assert instance != None transaction = newrelic.api.transaction.current_transaction() if transaction is None: return self._nr_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" % ( newrelic.api.object_wrapper.callable_name( instance), request.method) transaction.set_transaction_name(name, priority=1) with newrelic.api.function_trace.FunctionTrace(transaction, name): return self._nr_next_object(*args)
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._nr_instance request = args[-1] assert instance != None transaction = newrelic.api.transaction.current_transaction() if transaction is None: return self._nr_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" % ( newrelic.api.object_wrapper.callable_name( instance), request.method) transaction.set_transaction_name(name, priority=1) return newrelic.api.function_trace.FunctionTraceWrapper(self._nr_next_object, name)(*args)
def __call__(self, *args, **kwargs): transaction = newrelic.api.transaction.current_transaction() if transaction: transaction.set_transaction_name(self._nr_name, priority=self._nr_priority) with newrelic.api.function_trace.FunctionTrace(transaction, name=self._nr_name): return self._nr_wrapped(*args, **kwargs) else: return self._nr_wrapped(*args, **kwargs)
def transaction_name_delegate(*args, **kwargs): transaction = newrelic.api.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 __call__(self, *args, **kwargs): transaction = newrelic.api.transaction.current_transaction() if transaction: transaction.set_transaction_name(self._nr_name, priority=self._nr_priority) with newrelic.api.function_trace.FunctionTrace(name=self._nr_name): return self._nr_wrapped(*args, **kwargs) else: return self._nr_wrapped(*args, **kwargs)
def target_wsgi_application(environ, start_response): status = '200 OK' txn_name = environ.get('txn') if six.PY2: txn_name = txn_name.decode('UTF-8') txn_name = txn_name.split('/', 3) guid = environ.get('guid') old_cat = environ.get('old_cat') == 'True' txn = current_transaction() txn.guid = guid for req in OUTBOUD_REQUESTS: # Change the transaction name before making an outbound call. outgoing_name = req['outboundTxnName'].split('/', 3) if outgoing_name[0] != 'WebTransaction': set_background_task(True) set_transaction_name(outgoing_name[2], group=outgoing_name[1]) expected_outbound_header = obfuscate( json_encode(req['expectedOutboundPayload']), ENCODING_KEY) generated_outbound_header = dict( ExternalTrace.generate_request_headers(txn)) # A 500 error is returned because 'assert' statements in the wsgi app # are ignored. if old_cat: if (expected_outbound_header != generated_outbound_header['X-NewRelic-Transaction']): status = '500 Outbound Headers Check Failed.' else: if 'X-NewRelic-Transaction' in generated_outbound_header: status = '500 Outbound Headers Check Failed.' r = urlopen(environ['server_url']) r.read(10) # Set the final transaction name. if txn_name[0] != 'WebTransaction': set_background_task(True) set_transaction_name(txn_name[2], group=txn_name[1]) text = '<html><head>%s</head><body><p>RESPONSE</p>%s</body></html>' output = (text % (get_browser_timing_header(), get_browser_timing_footer())).encode('UTF-8') response_headers = [('Content-type', 'text/html; charset=utf-8'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output]
def target_wsgi_application(environ, start_response): status = '200 OK' txn_name = environ.get('txn_name') set_transaction_name(txn_name, group='') user_attrs = json.loads(environ.get('user_attrs')) for key, value in user_attrs.items(): add_custom_parameter(key, value) text = '<html><head>%s</head><body><p>RESPONSE</p></body></html>' output = (text % get_browser_timing_footer()).encode('UTF-8') response_headers = [('Content-Type', 'text/html; charset=utf-8'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output]
def __call__(self, environ, start_response): transaction = newrelic.api.transaction.current_transaction() # Check to see if we are being called within the # context of any sort of transaction. If we are, # then we don't bother doing anything and just # call the wrapped function. if transaction: # Record details of framework against the transaction # for later reporting as supportability metrics. if self._nr_framework: transaction._frameworks.add(self._nr_framework) # Override the web transaction name to be the name of the # wrapped callable if not explicitly named, and we want the # default name to be that of the WSGI component for the # framework. This will override the use of a raw URL which # can result in metric grouping issues where a framework is # not instrumented or is leaking URLs. settings = transaction._settings if self._nr_name is None and settings: if self._nr_framework is not None: naming_scheme = settings.transaction_name.naming_scheme if naming_scheme in (None, 'framework'): name = newrelic.api.object_wrapper.callable_name( self._nr_next_object) transaction.set_transaction_name(name, priority=1) elif self._nr_name: transaction.set_transaction_name(self._nr_name, self._nr_group, priority=1) return self._nr_next_object(environ, start_response) # Otherwise treat it as top level transaction. # We have to though look first to see whether the # application name has been overridden through # the WSGI environ dictionary. app_name = environ.get('newrelic.app_name') if app_name: if app_name.find(';') != -1: app_names = [string.strip(n) for n in app_name.split(';')] app_name = app_names[0] application = newrelic.api.application.application_instance( app_name) for altname in app_names[1:]: application.link_to_application(altname) else: application = newrelic.api.application.application_instance( app_name) else: application = self._nr_application # If application has an activate() method we assume it is an # actual application. Do this rather than check type so that # can easily mock it for testing. # FIXME Should this allow for multiple apps if a string. if not hasattr(application, 'activate'): application = newrelic.api.application.application_instance( application) # Now start recording the actual web transaction. transaction = WebTransaction(application, environ) transaction.__enter__() # Record details of framework against the transaction # for later reporting as supportability metrics. if self._nr_framework: transaction._frameworks.add(self._nr_framework) # Override the initial web transaction name to be the supplied # name, or the name of the wrapped callable if wanting to use # the callable as the default. This will override the use of a # raw URL which can result in metric grouping issues where a # framework is not instrumented or is leaking URLs. # # Note that at present if default for naming scheme is still # None and we aren't specifically wrapping a designated # framework, then we still allow old URL based naming to # override. When we switch to always forcing a name we need to # check for naming scheme being None here. settings = transaction._settings if self._nr_name is None and settings: naming_scheme = settings.transaction_name.naming_scheme if self._nr_framework is not None: if naming_scheme in (None, 'framework'): name = newrelic.api.object_wrapper.callable_name( self._nr_next_object) transaction.set_transaction_name(name, priority=1) elif naming_scheme in ('component', 'framework'): name = newrelic.api.object_wrapper.callable_name( self._nr_next_object) transaction.set_transaction_name(name, priority=1) elif self._nr_name: transaction.set_transaction_name(self._nr_name, self._nr_group, priority=1) def _start_response(status, response_headers, *args): additional_headers = transaction.process_response( status, response_headers, *args) _write = start_response(status, response_headers+additional_headers, *args) def write(data): if not transaction._sent_start: transaction._sent_start = time.time() result = _write(data) transaction._calls_write += 1 try: transaction._bytes_sent += len(data) except Exception: pass transaction._sent_end = time.time() return result return write try: # Should always exist, but check as test harnesses may not # have it. if 'wsgi.input' in environ: environ['wsgi.input'] = WSGIInputWrapper(transaction, environ['wsgi.input']) application = newrelic.api.function_trace.FunctionTraceWrapper( self._nr_next_object) with newrelic.api.function_trace.FunctionTrace( transaction, name='Application', group='Python/WSGI'): result = application(environ, _start_response) except: # Catch all transaction.__exit__(*sys.exc_info()) raise return _WSGIApplicationIterable(transaction, result)
def test_transaction_name_empty_group_bt(): set_transaction_name('Transaction', group='')
def name_transaction(name, group=None, priority=None): #warnings.warn('API change. Use set_transaction_name() instead of ' # 'name_transaction().', DeprecationWarning, stacklevel=2) transaction = current_transaction() if transaction: transaction.set_transaction_name(name, group, priority)
def set_transaction_name(name, group=None, priority=None): transaction = current_transaction() if transaction: transaction.set_transaction_name(name, group, priority)
def __call__(self, environ, start_response): transaction = newrelic.api.transaction.current_transaction() # Check to see if we are being called within the # context of any sort of transaction. If we are, # then we don't bother doing anything and just # call the wrapped function. if transaction: # Record details of framework against the transaction # for later reporting as supportability metrics. if self._nr_framework: transaction._frameworks.add(self._nr_framework) # Override the web transaction name to be the name of the # wrapped callable if not explicitly named, and we want the # default name to be that of the WSGI component for the # framework. This will override the use of a raw URL which # can result in metric grouping issues where a framework is # not instrumented or is leaking URLs. settings = transaction._settings if self._nr_name is None and settings: if self._nr_framework is not None: naming_scheme = settings.transaction_name.naming_scheme if naming_scheme in (None, 'framework'): name = newrelic.api.object_wrapper.callable_name( self._nr_next_object) transaction.set_transaction_name(name, priority=1) elif self._nr_name: transaction.set_transaction_name(self._nr_name, self._nr_group, priority=1) return self._nr_next_object(environ, start_response) # Otherwise treat it as top level transaction. # We have to though look first to see whether the # application name has been overridden through # the WSGI environ dictionary. app_name = environ.get('newrelic.app_name') if app_name: if app_name.find(';') != -1: app_names = [string.strip(n) for n in app_name.split(';')] app_name = app_names[0] application = newrelic.api.application.application_instance( app_name) for altname in app_names[1:]: application.link_to_application(altname) else: application = newrelic.api.application.application_instance( app_name) else: application = self._nr_application # If application has an activate() method we assume it is an # actual application. Do this rather than check type so that # can easily mock it for testing. # FIXME Should this allow for multiple apps if a string. if not hasattr(application, 'activate'): application = newrelic.api.application.application_instance( application) # Now start recording the actual web transaction. transaction = WebTransaction(application, environ) transaction.__enter__() # Record details of framework against the transaction # for later reporting as supportability metrics. if self._nr_framework: transaction._frameworks.add(self._nr_framework) # Override the initial web transaction name to be the supplied # name, or the name of the wrapped callable if wanting to use # the callable as the default. This will override the use of a # raw URL which can result in metric grouping issues where a # framework is not instrumented or is leaking URLs. # # Note that at present if default for naming scheme is still # None and we aren't specifically wrapping a designated # framework, then we still allow old URL based naming to # override. When we switch to always forcing a name we need to # check for naming scheme being None here. settings = transaction._settings if self._nr_name is None and settings: naming_scheme = settings.transaction_name.naming_scheme if self._nr_framework is not None: if naming_scheme in (None, 'framework'): name = newrelic.api.object_wrapper.callable_name( self._nr_next_object) transaction.set_transaction_name(name, priority=1) elif naming_scheme in ('component', 'framework'): name = newrelic.api.object_wrapper.callable_name( self._nr_next_object) transaction.set_transaction_name(name, priority=1) elif self._nr_name: transaction.set_transaction_name(self._nr_name, self._nr_group, priority=1) def _start_response(status, response_headers, *args): additional_headers = transaction.process_response( status, response_headers, *args) _write = start_response(status, response_headers + additional_headers, *args) def write(data): if not transaction._sent_start: transaction._sent_start = time.time() result = _write(data) transaction._calls_write += 1 try: transaction._bytes_sent += len(data) except Exception: pass transaction._sent_end = time.time() return result return write try: # Should always exist, but check as test harnesses may not # have it. if 'wsgi.input' in environ: environ['wsgi.input'] = WSGIInputWrapper( transaction, environ['wsgi.input']) application = newrelic.api.function_trace.FunctionTraceWrapper( self._nr_next_object) with newrelic.api.function_trace.FunctionTrace( transaction, name='Application', group='Python/WSGI'): result = application(environ, _start_response) except: # Catch all transaction.__exit__(*sys.exc_info()) raise return _WSGIApplicationIterable(transaction, result)
def test_transaction_name_leading_slash_on_group_bt(): set_transaction_name('Transaction', group='/Group')
def test_transaction_name_valid_override_bt(): set_transaction_name('Transaction', group='Custom')