How to use the wrapt.wrap_function_wrapper function in wrapt

To help you get started, we’ve selected a few wrapt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ionelmc / python-lazy-object-proxy / tests / test_monkey_patching.py View on Github external
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))
github ionelmc / python-lazy-object-proxy / tests / test_monkey_patching.py View on Github external
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))
github ionelmc / python-lazy-object-proxy / tests / test_monkey_patching.py View on Github external
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))
github treebohotels / casualty / casualty / ext / kombu / patch.py View on Github external
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
    )
github mvantellingen / django-aws-xray / src / django_aws_xray / patches / requests.py View on Github external
def patch():
    """ Monkeypatch the requests library to trace http calls. """
    wrapt.wrap_function_wrapper('requests', 'Session.request', patched_request)
github epsagon / epsagon-python / epsagon / modules / logging.py View on Github external
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)
            )
github thundra-io / thundra-lambda-agent-python / thundra / integrations / modules / psycopg2.py View on Github external
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)
github epsagon / epsagon-python / epsagon / modules / psycopg2.py View on Github external
: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
    )
github instana / python-sensor / instana / instrumentation / django / middleware.py View on Github external
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
github aws / aws-xray-sdk-python / aws_xray_sdk / ext / pynamodb / patch.py View on Github external
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,
    )