def _nr_aiohttp_add_cat_headers_(wrapped, instance, args, kwargs):
    transaction = current_transaction()
    if transaction is None:
        return wrapped(*args, **kwargs)

    try:
        cat_headers = ExternalTrace.generate_request_headers(transaction)
    except:
        return wrapped(*args, **kwargs)

    tmp = instance.headers
    instance.headers = HeaderProxy(tmp, cat_headers)

    if is_coroutine_function(wrapped):

        @asyncio.coroutine
        def new_coro():
            try:
                result = yield from wrapped(*args, **kwargs)
                return result
            finally:
                instance.headers = tmp

        return new_coro()
    else:
        try:
            return wrapped(*args, **kwargs)
        finally:
            instance.headers = tmp
Example #2
0
def async_wrapper(wrapped):
    if is_coroutine_function(wrapped):
        return coroutine_wrapper
    elif is_generator_function(wrapped):
        if is_asyncio_coroutine(wrapped):
            return awaitable_generator_wrapper
        else:
            return generator_wrapper
Example #3
0
def async_proxy(wrapped):
    if is_coroutine_function(wrapped):
        return CoroutineProxy
    elif is_generator_function(wrapped):
        if is_asyncio_coroutine(wrapped):
            return AwaitableGeneratorProxy
        else:
            return GeneratorProxy
Example #4
0
def wrap_exception_handler(wrapped, instance, args, kwargs):
    if is_coroutine_function(wrapped):
        return wrap_exception_handler_async(FunctionTraceWrapper(wrapped)(*args, **kwargs), bind_exc(*args, **kwargs))
    else:
        with ContextOf(request=bind_request(*args, **kwargs)):
            response = FunctionTraceWrapper(wrapped)(*args, **kwargs)
            record_response_error(response, bind_exc(*args, **kwargs))
            return response
Example #5
0
def _nr_wrapper_convert_exception_to_response_(wrapped, instance, args,
                                               kwargs):
    def _bind_params(original_middleware, *args, **kwargs):
        return original_middleware

    original_middleware = _bind_params(*args, **kwargs)
    converted_middleware = wrapped(*args, **kwargs)
    name = callable_name(original_middleware)

    if is_coroutine_function(converted_middleware) or is_asyncio_coroutine(
            converted_middleware):
        return _nr_wrap_converted_middleware_async_(converted_middleware, name)
    return _nr_wrap_converted_middleware_(converted_middleware, name)
Example #6
0
def _nr_wrapper_Application_wsgi_(application):
    # Normally Application.wsgi() returns a WSGI application, but in
    # some async frameworks a special class or coroutine is returned. We must
    # check for those cases and avoid insturmenting the coroutine or
    # specialized class.

    try:
        if 'tornado.web' in sys.modules:
            import tornado.web
            if isinstance(application, tornado.web.Application):
                return application
    except ImportError:
        pass

    if not is_coroutine_function(application):
        return WSGIApplicationWrapper(application)

    return application
def is_coroutine(fn):
    return is_coroutine_function(fn) or is_asyncio_coroutine(fn)