예제 #1
0
def _nr_wrapper_httpclient_AsyncHTTPClient_fetch_(wrapped, instance, args,
                                                  kwargs):

    transaction = current_transaction()

    if transaction is None:
        return wrapped(*args, **kwargs)

    try:
        req, _cb, _raise_error = _prepare_request(*args, **kwargs)
    except:
        return wrapped(*args, **kwargs)

    # Prepare outgoing CAT headers
    outgoing_headers = ExternalTrace.generate_request_headers(transaction)
    for header_name, header_value in outgoing_headers:
        # User headers should override our CAT headers
        if header_name in req.headers:
            continue
        req.headers[header_name] = header_value

    trace = ExternalTrace(transaction, 'tornado.httpclient', req.url,
                          req.method.upper())

    def external_trace_done(future):
        exc_info = future.exc_info()
        if exc_info:
            trace.__exit__(*exc_info)
        else:
            response = future.result()
            # Process CAT response headers
            trace.process_response_headers(response.headers.get_all())
            trace.__exit__(None, None, None)

    trace.__enter__()
    if trace.transaction and trace.transaction.current_node is trace:
        # externals should not have children
        trace.transaction._pop_current(trace)

    try:
        future = wrapped(req, _cb, _raise_error)
        future.add_done_callback(external_trace_done)
    except Exception:
        trace.__exit__(*sys.exc_info())
        raise
    return future
예제 #2
0
def _nr_wrapper_httpclient_AsyncHTTPClient_fetch_(wrapped, instance, args,
                                                  kwargs):

    transaction = retrieve_current_transaction()

    if transaction is None:
        return wrapped(*args, **kwargs)

    req, _cb, _raise_error = _prepare_request(*args, **kwargs)

    # Prepare outgoing CAT headers
    outgoing_headers = ExternalTrace.generate_request_headers(transaction)
    for header_name, header_value in outgoing_headers:
        req.headers[header_name] = header_value

    trace = ExternalTrace(transaction, 'tornado.httpclient', req.url)

    def external_trace_done(future):
        exc_info = future.exc_info()
        if exc_info:
            trace.__exit__(*exc_info)
        else:
            response = future.result()
            # Process CAT response headers
            trace.process_response_headers(response.headers.get_all())
            trace.__exit__(None, None, None)
        transaction._ref_count -= 1

    transaction._ref_count += 1
    trace.__enter__()

    # Because traces are terminal but can be generated concurrently in
    # tornado, pop the trace immediately after entering.
    if trace.transaction and trace.transaction.current_node is trace:
        trace.transaction._pop_current(trace)

    try:
        future = wrapped(req, _cb, _raise_error)
        future.add_done_callback(external_trace_done)
    except Exception:
        transaction._ref_count -= 1
        trace.__exit__(*sys.exc_info())
        raise
    return future
예제 #3
0
def _nr_wrapper_httpclient_AsyncHTTPClient_fetch_(wrapped, instance, args,
                                                  kwargs):

    transaction = current_transaction()

    if transaction is None:
        return wrapped(*args, **kwargs)

    try:
        req, _cb, _raise_error = _prepare_request(*args, **kwargs)
    except:
        return wrapped(*args, **kwargs)

    # Prepare outgoing CAT headers
    outgoing_headers = ExternalTrace.generate_request_headers(transaction)
    for header_name, header_value in outgoing_headers:
        # User headers should override our CAT headers
        if header_name in req.headers:
            continue
        req.headers[header_name] = header_value

    # wrap the fetch_impl on the unbound method
    instance_type = type(instance)
    if not hasattr(instance_type, '_nr_wrapped'):
        instance_type.fetch_impl = wrap_fetch_impl(instance_type.fetch_impl)
        instance_type._nr_wrapped = True

    trace = ExternalTrace(transaction, 'tornado.httpclient', req.url,
                          req.method.upper())
    instance._nr_args = (_raise_error, trace)
    trace.__enter__()
    if trace.transaction and trace.transaction.current_node is trace:
        # externals should not have children
        trace.transaction._pop_current(trace)

    try:
        future = wrapped(req, _cb, _raise_error)
    except Exception:
        trace.__exit__(*sys.exc_info())
        raise
    return future
예제 #4
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