def instrument_flask_app(module): wrap_wsgi_application(module, 'Flask.wsgi_app', framework=framework_details()) wrap_function_wrapper(module, 'Flask.add_url_rule', _bw_wrapper_Flask_add_url_rule_input_) if hasattr(module.Flask, 'endpoint'): wrap_function_wrapper(module, 'Flask.endpoint', _bw_wrapper_Flask_endpoint_) wrap_function_wrapper(module, 'Flask.handle_http_exception', _bw_wrapper_Flask_handle_http_exception_) # Use the same wrapper for initial user exception processing and # fallback for unhandled exceptions. if hasattr(module.Flask, 'handle_user_exception'): wrap_function_wrapper(module, 'Flask.handle_user_exception', _bw_wrapper_Flask_handle_exception_) wrap_function_wrapper(module, 'Flask.handle_exception', _bw_wrapper_Flask_handle_exception_) # The _register_error_handler() method was only introduced in # Flask version 0.7.0. if hasattr(module.Flask, '_register_error_handler'): wrap_function_wrapper(module, 'Flask._register_error_handler', _bw_wrapper_Flask__register_error_handler_) # Different before/after methods were added in different versions. # Check for the presence of everything before patching. if hasattr(module.Flask, 'try_trigger_before_first_request_functions'): wrap_function_wrapper(module, 'Flask.try_trigger_before_first_request_functions', _bw_wrapper_Flask_try_trigger_before_first_request_functions_) wrap_function_wrapper(module, 'Flask.before_first_request', _bw_wrapper_Flask_before_first_request_) if hasattr(module.Flask, 'preprocess_request'): wrap_function_trace(module, 'Flask.preprocess_request') wrap_function_wrapper(module, 'Flask.before_request', _bw_wrapper_Flask_before_request_) if hasattr(module.Flask, 'process_response'): wrap_function_trace(module, 'Flask.process_response') wrap_function_wrapper(module, 'Flask.after_request', _bw_wrapper_Flask_after_request_) if hasattr(module.Flask, 'do_teardown_request'): wrap_function_trace(module, 'Flask.do_teardown_request') wrap_function_wrapper(module, 'Flask.teardown_request', _bw_wrapper_Flask_teardown_request_) if hasattr(module.Flask, 'do_teardown_appcontext'): wrap_function_trace(module, 'Flask.do_teardown_appcontext') wrap_function_wrapper(module, 'Flask.teardown_appcontext', _bw_wrapper_Flask_teardown_appcontext_)
def instrument_openerp_service_wsgi_server(module): # Wrap wsgi application. wrap_wsgi_application(module, 'application') # Before the wrapped wsgi application called , filter the longpolling # request which correspond to url /longpolling/poll by set the environ # value blueware.ignore_transaction to True to ignore this transaction. wrap_function_wrapper(module, 'application', _bw_wrapper_applictation)
def instrument_bottle(module): global module_bottle module_bottle = module framework_details = ('Bottle', getattr(module, '__version__')) if hasattr(module.Bottle, 'wsgi'): # version >= 0.9 wrap_wsgi_application(module, 'Bottle.wsgi', framework=framework_details) elif hasattr(module.Bottle, '__call__'): # version < 0.9 wrap_wsgi_application(module, 'Bottle.__call__', framework=framework_details) if (hasattr(module, 'Route') and hasattr(module.Route, '_make_callback')): # version >= 0.10 wrap_out_function(module, 'Route._make_callback', output_wrapper_Route_make_callback) elif hasattr(module.Bottle, '_match'): # version >= 0.9 wrap_out_function(module, 'Bottle._match', output_wrapper_Bottle_match) elif hasattr(module.Bottle, 'match_url'): # version < 0.9 wrap_out_function(module, 'Bottle.match_url', output_wrapper_Bottle_match) wrap_object_attribute(module, 'Bottle.error_handler', proxy_Bottle_error_handler) if hasattr(module, 'auth_basic'): wrap_function_wrapper(module, 'auth_basic', wrapper_auth_basic) if hasattr(module, 'SimpleTemplate'): wrap_function_trace(module, 'SimpleTemplate.render') if hasattr(module, 'MakoTemplate'): wrap_function_trace(module, 'MakoTemplate.render') if hasattr(module, 'CheetahTemplate'): wrap_function_trace(module, 'CheetahTemplate.render') if hasattr(module, 'Jinja2Template'): wrap_function_trace(module, 'Jinja2Template.render') if hasattr(module, 'SimpleTALTemplate'): wrap_function_trace(module, 'SimpleTALTemplate.render')
def instrument_tornado_wsgi(module): wrap_function_wrapper(module, 'WSGIContainer.__init__', _bw_wrapper_WSGIContainer___init___) wrap_function_wrapper(module, 'WSGIContainer.__call__', _bw_wrapper_WSGIContainer___call___) import tornado if hasattr(tornado, 'version_info'): version = '.'.join(map(str, tornado.version_info)) else: version = None framework = ('Tornado/WSGI', version) wrap_wsgi_application(module, 'WSGIApplication.__call__', framework=framework) # For tornado 4.0+ if hasattr(module, 'WSGIAdapter'): wrap_wsgi_application(module, 'WSGIAdapter.__call__', framework=('Tornado/WSGI', version))
def instrument_gluon_main(module): wrap_wsgi_application(module, 'wsgibase') # Wrap main function which dispatches the various # phases of a request in order to capture any # errors. Need to use a custom object wrapper as we # need to ignore exceptions of type HTTP as that # type of exception is used to programmatically # return a valid response. For the case of a 404, # where we want to name the web transactions as # such, we pick that up later. class error_serve_controller(object): def __init__(self, wrapped): update_wrapper(self, wrapped) self._bw_next_object = wrapped if not hasattr(self, '_bw_last_object'): self._bw_last_object = wrapped 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 __getattr__(self, name): return getattr(self._bw_next_object, name) wrap_object(module, 'serve_controller', error_serve_controller)
def instrument_cherrypy__cptree(module): wrap_wsgi_application(module, 'Application.__call__', framework=framework_details())
def instrument_cherrypy__cpwsgi(module): wrap_wsgi_application(module, 'CPWSGIApp.__call__', framework=framework_details())
def instrument_flask_app(module): wrap_wsgi_application(module, 'Flask.wsgi_app', framework=framework_details()) wrap_function_wrapper(module, 'Flask.add_url_rule', _bw_wrapper_Flask_add_url_rule_input_) if hasattr(module.Flask, 'endpoint'): wrap_function_wrapper(module, 'Flask.endpoint', _bw_wrapper_Flask_endpoint_) wrap_function_wrapper(module, 'Flask.handle_http_exception', _bw_wrapper_Flask_handle_http_exception_) # Use the same wrapper for initial user exception processing and # fallback for unhandled exceptions. if hasattr(module.Flask, 'handle_user_exception'): wrap_function_wrapper(module, 'Flask.handle_user_exception', _bw_wrapper_Flask_handle_exception_) wrap_function_wrapper(module, 'Flask.handle_exception', _bw_wrapper_Flask_handle_exception_) # The _register_error_handler() method was only introduced in # Flask version 0.7.0. if hasattr(module.Flask, '_register_error_handler'): wrap_function_wrapper(module, 'Flask._register_error_handler', _bw_wrapper_Flask__register_error_handler_) # Different before/after methods were added in different versions. # Check for the presence of everything before patching. if hasattr(module.Flask, 'try_trigger_before_first_request_functions'): wrap_function_wrapper( module, 'Flask.try_trigger_before_first_request_functions', _bw_wrapper_Flask_try_trigger_before_first_request_functions_) wrap_function_wrapper(module, 'Flask.before_first_request', _bw_wrapper_Flask_before_first_request_) if hasattr(module.Flask, 'preprocess_request'): wrap_function_trace(module, 'Flask.preprocess_request') wrap_function_wrapper(module, 'Flask.before_request', _bw_wrapper_Flask_before_request_) if hasattr(module.Flask, 'process_response'): wrap_function_trace(module, 'Flask.process_response') wrap_function_wrapper(module, 'Flask.after_request', _bw_wrapper_Flask_after_request_) if hasattr(module.Flask, 'do_teardown_request'): wrap_function_trace(module, 'Flask.do_teardown_request') wrap_function_wrapper(module, 'Flask.teardown_request', _bw_wrapper_Flask_teardown_request_) if hasattr(module.Flask, 'do_teardown_appcontext'): wrap_function_trace(module, 'Flask.do_teardown_appcontext') wrap_function_wrapper(module, 'Flask.teardown_appcontext', _bw_wrapper_Flask_teardown_appcontext_)