Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_wrap_function_module_name(self):
_args = (1, 2)
_kwargs = { 'one': 1, 'two': 2 }
called = []
def wrapper(wrapped, instance, args, kwargs):
called.append((args, kwargs))
self.assertEqual(instance, None)
self.assertEqual(args, _args)
self.assertEqual(kwargs, _kwargs)
return wrapped(*args, **kwargs)
wrapt.wrap_function_wrapper(__name__, 'global_function_1', wrapper)
result = global_function_1(*_args, **_kwargs)
self.assertEqual(result, (_args, _kwargs))
self.assertEqual(called[0], (_args, _kwargs))
def test_wrap_class_method_module_name(self):
_args = (1, 2)
_kwargs = { 'one': 1, 'two': 2 }
called = []
def wrapper(wrapped, instance, args, kwargs):
called.append((args, kwargs))
self.assertEqual(instance, Class_2)
self.assertEqual(args, _args)
self.assertEqual(kwargs, _kwargs)
return wrapped(*args, **kwargs)
wrapt.wrap_function_wrapper(__name__, 'Class_2.method',
wrapper)
result = Class_2.method(*_args, **_kwargs)
self.assertEqual(result, (_args, _kwargs))
self.assertEqual(called[0], (_args, _kwargs))
def test_wrap_static_method_module_name(self):
_args = (1, 2)
_kwargs = { 'one': 1, 'two': 2 }
called = []
def wrapper(wrapped, instance, args, kwargs):
called.append((args, kwargs))
self.assertEqual(instance, None)
self.assertEqual(args, _args)
self.assertEqual(kwargs, _kwargs)
return wrapped(*args, **kwargs)
wrapt.wrap_function_wrapper(__name__, 'Class_3.method',
wrapper)
result = Class_3.method(*_args, **_kwargs)
self.assertEqual(result, (_args, _kwargs))
self.assertEqual(called[0], (_args, _kwargs))
def patch():
wrapt.wrap_function_wrapper("kombu", "messaging.Producer.publish", _inject_header)
wrapt.wrap_function_wrapper(
"kombu", "messaging.Consumer.__init__", _initialize_structlog_configuration
)
wrapt.wrap_function_wrapper(
"kombu", "messaging.Consumer.receive", _bind_request_id_on_message_receive
)
def patch():
""" Monkeypatch the requests library to trace http calls. """
wrapt.wrap_function_wrapper('requests', 'Session.request', patched_request)
Patch module.
:return: None
"""
# Automatically capture exceptions from logging
wrapt.wrap_function_wrapper('logging', 'exception', _wrapper)
wrapt.wrap_function_wrapper('logging', 'Logger.exception', _wrapper)
# Instrument logging with Epsagon trace ID
if trace_factory.is_logging_tracing_enabled():
wrapt.wrap_function_wrapper(
'logging',
'Logger.log',
partial(_epsagon_trace_id_wrapper, 1)
)
for log_function in LOGGING_FUNCTIONS:
wrapt.wrap_function_wrapper(
'logging',
'Logger.{}'.format(log_function),
partial(_epsagon_trace_id_wrapper, 0)
)
def patch():
if not config.rdb_integration_disabled():
wrapt.wrap_function_wrapper(
'psycopg2.extensions',
'register_type',
_wrapper_register_type
)
wrapt.wrap_function_wrapper(
'psycopg2',
'connect',
_wrapper)
:return:
"""
wrapt.wrap_function_wrapper(
'psycopg2.extensions',
'register_type',
_register_type_wrapper
)
wrapt.wrap_function_wrapper(
'psycopg2._psycopg',
'register_type',
_register_type_wrapper
)
wrapt.wrap_function_wrapper(
'psycopg2._json',
'register_type',
_register_type_wrapper
)
wrapt.wrap_function_wrapper(
'psycopg2.extensions',
'adapt',
_adapt_wrapper
)
settings.MIDDLEWARE_CLASSES = [DJ_INSTANA_MIDDLEWARE] + settings.MIDDLEWARE_CLASSES
else:
logger.warning("Instana: Couldn't add InstanaMiddleware to Django")
else:
logger.warning("Instana: Couldn't find middleware settings")
return wrapped(*args, **kwargs)
except Exception:
logger.warning("Instana: Couldn't add InstanaMiddleware to Django: ", exc_info=True)
try:
if 'django' in sys.modules:
logger.debug("Instrumenting django")
wrapt.wrap_function_wrapper('django.core.handlers.base', 'BaseHandler.load_middleware', load_middleware_wrapper)
if 'INSTANA_MAGIC' in os.environ:
# If we are instrumenting via AutoTrace (in an already running process), then the
# WSGI middleware has to be live reloaded.
from django.core.servers.basehttp import get_internal_wsgi_application
from django.core.exceptions import ImproperlyConfigured
try:
wsgiapp = get_internal_wsgi_application()
wsgiapp.load_middleware()
except ImproperlyConfigured:
pass
except Exception:
logger.debug("django.middleware:", exc_info=True)
pass
def patch():
"""Patch PynamoDB so it generates subsegements when calling DynamoDB."""
if hasattr(botocore.vendored.requests.sessions, '_xray_enabled'):
return
setattr(botocore.vendored.requests.sessions, '_xray_enabled', True)
if PYNAMODB4:
module = 'botocore.httpsession'
name = 'URLLib3Session.send'
else:
module = 'botocore.vendored.requests.sessions'
name = 'Session.send'
wrapt.wrap_function_wrapper(
module, name, _xray_traced_pynamodb,
)