예제 #1
0
    def call(url, *args, **kwargs):
        # Check if request was sent from an exporter. If so, do not wrap.
        if execution_context.is_exporter():
            return requests_func(url, *args, **kwargs)
        excludelist_hostnames = execution_context.get_opencensus_attr(
            'excludelist_hostnames')
        parsed_url = urlparse(url)
        if parsed_url.port is None:
            dest_url = parsed_url.hostname
        else:
            dest_url = '{}:{}'.format(parsed_url.hostname, parsed_url.port)
        if utils.disable_tracing_hostname(dest_url, excludelist_hostnames):
            return requests_func(url, *args, **kwargs)

        path = parsed_url.path if parsed_url.path else '/'

        _tracer = execution_context.get_opencensus_tracer()
        _span = _tracer.start_span()
        _span.name = '{}'.format(path)
        _span.span_kind = span_module.SpanKind.CLIENT

        # Add the component type to attributes
        _tracer.add_attribute_to_current_span("component", "HTTP")

        # Add the requests host to attributes
        _tracer.add_attribute_to_current_span(HTTP_HOST, dest_url)

        # Add the requests method to attributes
        _tracer.add_attribute_to_current_span(HTTP_METHOD,
                                              requests_func.__name__.upper())

        # Add the requests path to attributes
        _tracer.add_attribute_to_current_span(HTTP_PATH, path)

        # Add the requests url to attributes
        _tracer.add_attribute_to_current_span(HTTP_URL, url)

        try:
            result = requests_func(url, *args, **kwargs)
        except requests.Timeout:
            _span.set_status(exceptions_status.TIMEOUT)
            raise
        except requests.URLRequired:
            _span.set_status(exceptions_status.INVALID_URL)
            raise
        except Exception as e:
            _span.set_status(exceptions_status.unknown(e))
            raise
        else:
            # Add the status code to attributes
            _tracer.add_attribute_to_current_span(HTTP_STATUS_CODE,
                                                  result.status_code)
            _span.set_status(utils.status_from_http_code(result.status_code))
            return result
        finally:
            _tracer.end_span()
예제 #2
0
def wrap_session_request(wrapped, instance, args, kwargs):
    """Wrap the session function to trace it."""
    # Check if request was sent from an exporter. If so, do not wrap.
    if execution_context.is_exporter():
        return wrapped(*args, **kwargs)

    method = kwargs.get('method') or args[0]
    url = kwargs.get('url') or args[1]

    excludelist_hostnames = execution_context.get_opencensus_attr(
        'excludelist_hostnames')
    parsed_url = urlparse(url)
    if parsed_url.port is None:
        dest_url = parsed_url.hostname
    else:
        dest_url = '{}:{}'.format(parsed_url.hostname, parsed_url.port)
    if utils.disable_tracing_hostname(dest_url, excludelist_hostnames):
        return wrapped(*args, **kwargs)

    path = parsed_url.path if parsed_url.path else '/'

    _tracer = execution_context.get_opencensus_tracer()
    _span = _tracer.start_span()

    _span.name = '{}'.format(path)
    _span.span_kind = span_module.SpanKind.CLIENT

    try:
        tracer_headers = _tracer.propagator.to_headers(_tracer.span_context)
        kwargs.setdefault('headers', {}).update(tracer_headers)
    except Exception:  # pragma: NO COVER
        pass

    # Add the component type to attributes
    _tracer.add_attribute_to_current_span("component", "HTTP")

    # Add the requests host to attributes
    _tracer.add_attribute_to_current_span(HTTP_HOST, dest_url)

    # Add the requests method to attributes
    _tracer.add_attribute_to_current_span(HTTP_METHOD, method.upper())

    # Add the requests path to attributes
    _tracer.add_attribute_to_current_span(HTTP_PATH, path)

    # Add the requests url to attributes
    _tracer.add_attribute_to_current_span(HTTP_URL, url)

    try:
        result = wrapped(*args, **kwargs)
    except requests.Timeout:
        _span.set_status(exceptions_status.TIMEOUT)
        raise
    except requests.URLRequired:
        _span.set_status(exceptions_status.INVALID_URL)
        raise
    except Exception as e:
        _span.set_status(exceptions_status.unknown(e))
        raise
    else:
        # Add the status code to attributes
        _tracer.add_attribute_to_current_span(HTTP_STATUS_CODE,
                                              result.status_code)
        _span.set_status(utils.status_from_http_code(result.status_code))
        return result
    finally:
        _tracer.end_span()
예제 #3
0
    def test_grpc_code_from_http_code(self):
        test_cases = [
            {
                'http_code': 0,
                'grpc_code': code_pb2.UNKNOWN,
            },
            {
                'http_code': 200,
                'grpc_code': code_pb2.OK,
            },
            {
                'http_code': 399,
                'grpc_code': code_pb2.OK,
            },
            {
                'http_code': 400,
                'grpc_code': code_pb2.INVALID_ARGUMENT,
            },
            {
                'http_code': 504,
                'grpc_code': code_pb2.DEADLINE_EXCEEDED,
            },
            {
                'http_code': 404,
                'grpc_code': code_pb2.NOT_FOUND,
            },
            {
                'http_code': 403,
                'grpc_code': code_pb2.PERMISSION_DENIED,
            },
            {
                'http_code': 401,
                'grpc_code': code_pb2.UNAUTHENTICATED,
            },
            {
                'http_code': 429,
                'grpc_code': code_pb2.RESOURCE_EXHAUSTED,
            },
            {
                'http_code': 501,
                'grpc_code': code_pb2.UNIMPLEMENTED,
            },
            {
                'http_code': 503,
                'grpc_code': code_pb2.UNAVAILABLE,
            },
            {
                'http_code': 600,
                'grpc_code': code_pb2.UNKNOWN,
            },
        ]

        for test_case in test_cases:
            status = utils.status_from_http_code(test_case['http_code'])
            self.assertEqual(
                status.canonical_code, test_case['grpc_code'],
                'HTTP: {} / GRPC: expected = {}, actual = {}'.format(
                    test_case['http_code'],
                    test_case['grpc_code'],
                    status.canonical_code,
                ))