def _test_cat_headers():
        transaction = current_transaction()

        payload = ExternalTrace.generate_request_headers(transaction)
        assert not payload

        trace = ExternalTrace('testlib', 'http://example.com')
        response_headers = [('X-NewRelic-App-Data', 'Cookies')]
        with trace:
            trace.process_response_headers(response_headers)

        assert transaction.settings.cross_application_tracer.enabled is False
示例#2
0
class NRRequestCoroutineWrapper(ObjectProxy):
    def __init__(self, url, method, wrapped, func=None):
        super(NRRequestCoroutineWrapper, self).__init__(wrapped)
        transaction = current_transaction()
        self._nr_trace = ExternalTrace(transaction,
                'aiohttp', url, method)

    def __iter__(self):
        return self

    def __await__(self):
        return self

    def __next__(self):
        return self.send(None)

    def send(self, value):
        if not self._nr_trace.transaction:
            return self.__wrapped__.send(value)

        if not self._nr_trace.activated:
            self._nr_trace.__enter__()

            if (self._nr_trace.transaction and
                    self._nr_trace.transaction.current_node is self._nr_trace):
                # externals should not have children
                # This is so we do not miss concurrent external traces.
                self._nr_trace.transaction._pop_current(self._nr_trace)

        try:
            return self.__wrapped__.send(value)
        except StopIteration as e:
            try:
                result = e.value
                self._nr_trace.process_response_headers(result.headers.items())
            except:
                pass

            self._nr_trace.__exit__(None, None, None)
            raise
        except Exception as e:
            try:
                self._nr_trace.process_response_headers(e.headers.items())
            except:
                pass

            self._nr_trace.__exit__(*sys.exc_info())
            raise

    def throw(self, *args, **kwargs):
        try:
            return self.__wrapped__.throw(*args, **kwargs)
        except:
            self._nr_trace.__exit__(*sys.exc_info())
            raise

    def close(self):
        try:
            r = self.__wrapped__.close()
            self._nr_trace.__exit__(None, None, None)
            return r
        except:
            self._nr_trace.__exit__(*sys.exc_info())
            raise