Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 54 additions & 52 deletions newrelic/hooks/framework_bottle.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@

import functools

from newrelic.api.function_trace import (FunctionTrace, FunctionTraceWrapper,
wrap_function_trace)
from newrelic.api.function_trace import (
FunctionTrace,
FunctionTraceWrapper,
wrap_function_trace,
)
from newrelic.api.transaction import current_transaction
from newrelic.api.wsgi_application import wrap_wsgi_application
from newrelic.common.object_names import callable_name
from newrelic.common.object_wrapper import (wrap_out_function,
function_wrapper, ObjectProxy, wrap_object_attribute,
wrap_function_wrapper)
from newrelic.common.object_wrapper import (
ObjectProxy,
function_wrapper,
wrap_function_wrapper,
wrap_object_attribute,
wrap_out_function,
)

module_bottle = None

Expand All @@ -34,17 +41,17 @@ def status_code(exc, value, tb):
# The HTTPError class derives from HTTPResponse and so we do not
# need to check for it seperately as isinstance() will pick it up.

if isinstance(value, module_bottle.HTTPResponse):
if hasattr(value, 'status_code'):
if isinstance(value, module_bottle.HTTPResponse): # pragma: no cover
if hasattr(value, "status_code"):
return value.status_code
elif hasattr(value, 'status'):
elif hasattr(value, "status"):
return value.status
elif hasattr(value, 'http_status_code'):
elif hasattr(value, "http_status_code"):
return value.http_status_code


def should_ignore(exc, value, tb):
if hasattr(module_bottle, 'RouteReset'):
if hasattr(module_bottle, "RouteReset"):
if isinstance(value, module_bottle.RouteReset):
return True

Expand Down Expand Up @@ -113,8 +120,7 @@ def get(self, status, default=None):
transaction.set_transaction_name(name, priority=1)
handler = FunctionTraceWrapper(handler, name=name)
else:
transaction.set_transaction_name(str(status),
group='StatusCode', priority=1)
transaction.set_transaction_name(str(status), group="StatusCode", priority=1)

return handler or default

Expand All @@ -140,43 +146,39 @@ 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')
framework_details = ("Bottle", getattr(module, "__version__"))
# version >= 0.9
if hasattr(module.Bottle, "wsgi"): # pragma: no cover
wrap_wsgi_application(module, "Bottle.wsgi", framework=framework_details)
# version < 0.9
elif hasattr(module.Bottle, "__call__"): # pragma: no cover
wrap_wsgi_application(module, "Bottle.__call__", framework=framework_details)
# version >= 0.10
if hasattr(module, "Route") and hasattr(module.Route, "_make_callback"): # pragma: no cover
wrap_out_function(module, "Route._make_callback", output_wrapper_Route_make_callback)
# version >= 0.9
elif hasattr(module.Bottle, "_match"): # pragma: no cover
wrap_out_function(module, "Bottle._match", output_wrapper_Bottle_match)
# version < 0.9
elif hasattr(module.Bottle, "match_url"): # pragma: no cover
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"): # pragma: no cover
wrap_function_trace(module, "SimpleTALTemplate.render")
48 changes: 25 additions & 23 deletions newrelic/hooks/framework_pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,29 @@
from newrelic.api.transaction import current_transaction
from newrelic.api.wsgi_application import wrap_wsgi_application
from newrelic.common.object_names import callable_name
from newrelic.common.object_wrapper import (FunctionWrapper, wrap_out_function,
wrap_function_wrapper)
from newrelic.common.object_wrapper import (
FunctionWrapper,
wrap_function_wrapper,
wrap_out_function,
)


def instrument_pyramid_router(module):
pyramid_version = None

try:
import pkg_resources
pyramid_version = pkg_resources.get_distribution('pyramid').version

pyramid_version = pkg_resources.get_distribution("pyramid").version
except Exception:
pass

wrap_wsgi_application(
module, 'Router.__call__', framework=('Pyramid', pyramid_version))
wrap_wsgi_application(module, "Router.__call__", framework=("Pyramid", pyramid_version))


def status_code(exc, value, tb):
from pyramid.httpexceptions import HTTPException

# Ignore certain exceptions based on HTTP status codes.

if isinstance(value, HTTPException):
Expand All @@ -75,6 +79,7 @@ def status_code(exc, value, tb):

def should_ignore(exc, value, tb):
from pyramid.exceptions import PredicateMismatch

# Always ignore PredicateMismatch as it is raised by views to force
# subsequent views to be consulted when multi views are being used.
# It isn't therefore strictly an error as such as a subsequent view
Expand All @@ -100,9 +105,7 @@ def view_handler_wrapper(wrapped, instance, args, kwargs):

# set exception views to priority=1 so they won't take precedence over
# the original view callable
transaction.set_transaction_name(
name,
priority=1 if args and isinstance(args[0], Exception) else 2)
transaction.set_transaction_name(name, priority=1 if args and isinstance(args[0], Exception) else 2)

with FunctionTrace(name, source=view_callable) as trace:
try:
Expand All @@ -114,7 +117,7 @@ def view_handler_wrapper(wrapped, instance, args, kwargs):


def wrap_view_handler(mapped_view):
if hasattr(mapped_view, '_nr_wrapped'):
if hasattr(mapped_view, "_nr_wrapped"): # pragma: no cover
return mapped_view
else:
wrapped = FunctionWrapper(mapped_view, view_handler_wrapper)
Expand Down Expand Up @@ -157,7 +160,7 @@ def _wrapper(context, request):
return wrapper(context, request)
finally:
attr = instance.attr
inst = getattr(request, '__view__', None)
inst = getattr(request, "__view__", None)
if inst is not None:
if attr:
handler = getattr(inst, attr)
Expand All @@ -166,7 +169,7 @@ def _wrapper(context, request):
tracer.name = name
tracer.add_code_level_metrics(handler)
else:
method = getattr(inst, '__call__')
method = getattr(inst, "__call__")
if method:
name = callable_name(method)
transaction.set_transaction_name(name, priority=2)
Expand All @@ -180,22 +183,21 @@ def instrument_pyramid_config_views(module):
# Location of the ViewDeriver class changed from pyramid.config to
# pyramid.config.views so check if present before trying to update.

if hasattr(module, 'ViewDeriver'):
wrap_out_function(module, 'ViewDeriver.__call__', wrap_view_handler)
elif hasattr(module, 'Configurator'):
wrap_out_function(module, 'Configurator._derive_view',
wrap_view_handler)
if hasattr(module, "ViewDeriver"): # pragma: no cover
wrap_out_function(module, "ViewDeriver.__call__", wrap_view_handler)
elif hasattr(module, "Configurator"):
wrap_out_function(module, "Configurator._derive_view", wrap_view_handler)

if hasattr(module, 'DefaultViewMapper'):
if hasattr(module, "DefaultViewMapper"):
module.DefaultViewMapper.map_class_requestonly = FunctionWrapper(
module.DefaultViewMapper.map_class_requestonly,
default_view_mapper_wrapper)
module.DefaultViewMapper.map_class_requestonly, default_view_mapper_wrapper
)
module.DefaultViewMapper.map_class_native = FunctionWrapper(
module.DefaultViewMapper.map_class_native,
default_view_mapper_wrapper)
module.DefaultViewMapper.map_class_native, default_view_mapper_wrapper
)


def instrument_pyramid_config_tweens(module):
wrap_function_wrapper(module, 'Tweens.add_explicit', wrap_add_tween)
wrap_function_wrapper(module, "Tweens.add_explicit", wrap_add_tween)

wrap_function_wrapper(module, 'Tweens.add_implicit', wrap_add_tween)
wrap_function_wrapper(module, "Tweens.add_implicit", wrap_add_tween)
2 changes: 1 addition & 1 deletion newrelic/hooks/logger_loguru.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def patch_loguru_logger(logger):
if not hasattr(logger._core, "_nr_instrumented"):
logger.add(_nr_log_forwarder, format="{message}")
logger._core._nr_instrumented = True
elif not hasattr(logger, "_nr_instrumented"):
elif not hasattr(logger, "_nr_instrumented"): # pragma: no cover
for _, handler in six.iteritems(logger._handlers):
if handler._writer is _nr_log_forwarder:
logger._nr_instrumented = True
Expand Down
8 changes: 3 additions & 5 deletions newrelic/hooks/messagebroker_pika.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@


def _add_consume_rabbitmq_trace(transaction, method, properties, nr_start_time, queue_name=None):

routing_key = None
if hasattr(method, "routing_key"):
routing_key = method.routing_key
Expand Down Expand Up @@ -197,7 +196,7 @@ def _wrap_basic_get_Channel(wrapper, queue, callback, *args, **kwargs):
return queue, args, kwargs


def _wrap_basic_get_Channel_old(wrapper, callback=None, queue="", *args, **kwargs):
def _wrap_basic_get_Channel_old(wrapper, callback=None, queue="", *args, **kwargs): # pragma: no cover
if callback is not None:
callback = wrapper(callback)
args = (callback, queue) + args
Expand Down Expand Up @@ -368,7 +367,6 @@ def callback_wrapper(wrapped, instance, args, kwargs):
correlation_id=correlation_id,
source=wrapped,
) as mt:

# Improve transaction naming
_new_txn_name = "RabbitMQ/Exchange/%s/%s" % (exchange, name)
mt.set_transaction_name(_new_txn_name, group="Message")
Expand Down Expand Up @@ -404,7 +402,7 @@ def instrument_pika_adapters(module):

version = tuple(int(num) for num in pika.__version__.split(".", 1)[0])

if version[0] < 1:
if version[0] < 1: # pragma: no cover
wrap_consume = _wrap_basic_consume_BlockingChannel_old
else:
wrap_consume = _wrap_basic_consume_Channel
Expand All @@ -426,7 +424,7 @@ def instrument_pika_channel(module):

version = tuple(int(num) for num in pika.__version__.split(".", 1)[0])

if version[0] < 1:
if version[0] < 1: # pragma: no cover
wrap_consume = _wrap_basic_consume_Channel_old
wrap_get = _wrap_basic_get_Channel_old
else:
Expand Down
Loading