<s> from __future__ import absolute_import , unicode_literals <EOL> import functools <EOL> import newrelic . agent <EOL> import newrelic . config <EOL> from lymph . core import trace <EOL> from lymph . core . plugins import Plugin <EOL> from lymph . core . container import ServiceContainer <EOL> from lymph . core . channels import RequestChannel <EOL> from lymph . core . interfaces import DeferredReply <EOL> from lymph . web . interfaces import WebServiceInterface <EOL> def with_trace_id ( func ) : <EOL> @ functools . wraps ( func ) <EOL> def wrapped ( * args , ** kwargs ) : <EOL> newrelic . agent . add_custom_parameter ( '<STR_LIT>' , trace . get_id ( ) ) <EOL> return func ( * args , ** kwargs ) <EOL> return wrapped <EOL> def trace_rpc_method ( method , get_subject ) : <EOL> @ functools . wraps ( method ) <EOL> def wrapped ( self , * args , ** kwargs ) : <EOL> transaction = newrelic . agent . current_transaction ( ) <EOL> with newrelic . agent . FunctionTrace ( transaction , name = get_subject ( self ) , group = '<STR_LIT>' ) : <EOL> return method ( self , * args , ** kwargs ) <EOL> return wrapped <EOL> class NewrelicPlugin ( Plugin ) : <EOL> def __init__ ( self , container , config_file = None , environment = None , app_name = None , ** kwargs ) : <EOL> super ( NewrelicPlugin , self ) . __init__ ( ) <EOL> self . container = container <EOL> self . container . error_hook . install ( self . on_error ) <EOL> self . container . http_request_hook . install ( self . on_http_request ) <EOL> newrelic . agent . initialize ( config_file , environment ) <EOL> settings = newrelic . agent . global_settings ( ) <EOL> if app_name : <EOL> settings . app_name = app_name <EOL> newrelic . config . _process_app_name_setting ( ) <EOL> RequestChannel . get = trace_rpc_method ( RequestChannel . get , lambda channel : channel . request . subject ) <EOL> DeferredReply . get = trace_rpc_method ( DeferredReply . get , lambda deferred : deferred . subject ) <EOL> def on_interface_installation ( self , interface ) : <EOL> self . _wrap_methods ( interface . methods ) <EOL> self . _wrap_methods ( interface . event_handlers ) <EOL> if isinstance ( interface , WebServiceInterface ) : <EOL> interface . application = newrelic . agent . wsgi_application ( ) ( interface . application ) <EOL> def _wrap_methods ( self , methods ) : <EOL> for name , method in methods . items ( ) : <EOL> method . decorate ( with_trace_id ) <EOL> method . decorate ( newrelic . agent . background_task ( ) ) <EOL> def on_error ( self , exc_info , ** kwargs ) : <EOL> newrelic . agent . add_custom_parameter ( '<STR_LIT>' , trace . get_id ( ) ) <EOL> newrelic . agent . record_exception ( exc_info ) <EOL> def on_http_request ( self , request , rule , kwargs ) : <EOL> newrelic . agent . set_transaction_name ( "<STR_LIT>" % ( request . method , rule ) ) <EOL> newrelic . agent . add_custom_parameter ( '<STR_LIT>' , trace . get_id ( ) ) </s>
