Created
August 24, 2012 06:47
-
-
Save amjith/3446825 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from newrelic.api.function_trace import FunctionTrace | |
from newrelic.api.object_wrapper import ObjectWrapper | |
from newrelic.api.transaction import current_transaction | |
from newrelic.api.pre_function import wrap_pre_function | |
def wrap_handle_exception(*args): | |
transaction = current_transaction() | |
if transaction: | |
transaction.record_exception(*sys.exc_info()) | |
def outer_fn_wrapper(outer_fn, instance, args, kwargs): | |
view_name = args[0] | |
meta = instance._meta | |
if meta.api_name is not None: | |
group = 'Python/TastyPie/Api' | |
name = '%s/%s/%s' % (meta.api_name, meta.resource_name, view_name) | |
else: | |
group = 'Python/TastyPie/Resource' | |
name = '%s/%s' % (meta.resource_name, view_name) | |
def inner_fn_wrapper(inner_fn, instance, args, kwargs): | |
transaction = current_transaction() | |
if transaction is None or name is None: | |
return inner_fn(*args, **kwargs) | |
transaction.name_transaction(name, group, priority=4) | |
with FunctionTrace(transaction, name=name): | |
try: | |
return inner_fn(*args, **kwargs) | |
except: | |
transaction.record_exception(*sys.exc_info()) | |
result = outer_fn(*args, **kwargs) | |
return ObjectWrapper(result, None, inner_fn_wrapper) | |
def instrument_tastypie_resources(module): | |
_wrap_view = module.Resource.wrap_view | |
module.Resource.wrap_view = ObjectWrapper(_wrap_view, None, outer_fn_wrapper) | |
wrap_pre_function(module, 'Resource._handle_500', wrap_handle_exception) | |
#def api_outer_fn_wrapper(outer_fn, instance, args, kwargs): | |
# view_name = args[0] | |
# | |
# group = 'Python/TastyPie/Api' | |
# name =meta.api_name | |
# | |
# def api_inner_fn_wrapper(inner_fn, instance, args, kwargs): | |
# transaction = current_transaction() | |
# | |
# if transaction is None or name is None: | |
# return inner_fn(*args, **kwargs) | |
# | |
# transaction.name_transaction(name, group, priority=4) | |
# | |
# with FunctionTrace(transaction, name=name): | |
# try: | |
# return inner_fn(*args, **kwargs) | |
# except: | |
# transaction.record_exception(*sys.exc_info()) | |
# | |
# result = outer_fn(*args, **kwargs) | |
# | |
# return ObjectWrapper(result, None, api_inner_fn_wrapper) | |
def instrument_tastypie_api(module): | |
_wrap_view = module.Api.wrap_view | |
module.Api.wrap_view = ObjectWrapper(_wrap_view, None, outer_fn_wrapper) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment