<s> from newrelic . agent import ( current_transaction , wrap_function_wrapper , <EOL> FunctionTrace , WSGIApplicationWrapper ) <EOL> from newrelic . api . function_trace import FunctionTraceWrapper <EOL> from functools import wraps , partial <EOL> import logging <EOL> console = logging . getLogger ( __name__ ) <EOL> nr_trace_wsgi = partial ( FunctionTraceWrapper , group = "<STR_LIT>" ) <EOL> class NewrelicPybald ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , application , name = None ) : <EOL> if name : <EOL> self . __class__ . __name__ = name <EOL> self . application = application <EOL> def __call__ ( self , * args , ** kargs ) : <EOL> return self . application ( * args , ** kargs ) <EOL> def handler_wrapper ( name ) : <EOL> def _inner_wrap ( method ) : <EOL> @ wraps ( method ) <EOL> def _wrapper ( * args , ** kargs ) : <EOL> transaction = current_transaction ( ) <EOL> if transaction is None : <EOL> return method ( * args , ** kargs ) <EOL> transaction . set_transaction_name ( name ) <EOL> with FunctionTrace ( transaction , name , group = "<STR_LIT>" ) : <EOL> return method ( * args , ** kargs ) <EOL> return _wrapper <EOL> return _inner_wrap <EOL> def wrapper_Pybald_Router_get_handler ( wrapped , instance , args , kwargs ) : <EOL> '''<STR_LIT>''' <EOL> action = wrapped ( * args , ** kwargs ) <EOL> action_name = action . __name__ <EOL> controller_classname = "<STR_LIT>" <EOL> if hasattr ( action , '<STR_LIT>' ) : <EOL> controller = action . __self__ <EOL> if hasattr ( controller , '<STR_LIT>' ) : <EOL> controller_classname = controller . __class__ . __name__ <EOL> name = "<STR_LIT>" . format ( controller_classname , action_name ) <EOL> action = handler_wrapper ( name ) ( action ) <EOL> return action <EOL> def add_wsgi_trace ( app ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> next_level = app . application <EOL> if next_level is None : <EOL> return True <EOL> else : <EOL> app . application = nr_trace_wsgi ( next_level ) <EOL> except AttributeError : <EOL> return True <EOL> return add_wsgi_trace ( next_level ) <EOL> def instrument_pybald ( app , name = None ) : <EOL> '''<STR_LIT>''' <EOL> import pybald <EOL> app = NewrelicPybald ( app , name ) <EOL> add_wsgi_trace ( app ) <EOL> app = WSGIApplicationWrapper ( app , name = name , group = "<STR_LIT>" , <EOL> framework = ( "<STR_LIT>" , pybald . __version__ ) ) <EOL> return app <EOL> def instrument_pybald_app ( app , name = None ) : <EOL> '''<STR_LIT>''' <EOL> import pybald . core . router <EOL> wrap_function_wrapper ( pybald . core . router , '<STR_LIT>' , <EOL> wrapper_Pybald_Router_get_handler ) <EOL> return instrument_pybald ( app , name = name ) </s>
