Esempio n. 1
0
    def test_disable_tracing_hostname_explicit(self):
        blacklist_paths = ['127.0.0.1', '192.168.0.1:80']

        url = '127.0.0.1:8080'
        disable_tracing = utils.disable_tracing_hostname(url, blacklist_paths)
        self.assertFalse(disable_tracing)

        url = '127.0.0.1:80'
        disable_tracing = utils.disable_tracing_hostname(url, blacklist_paths)
        self.assertFalse(disable_tracing)
Esempio n. 2
0
    def call(self, method, url, body, headers, *args, **kwargs):
        _tracer = execution_context.get_opencensus_tracer()
        blacklist_hostnames = execution_context.get_opencensus_attr(
            'blacklist_hostnames')
        dest_url = '{}:{}'.format(self.host, self.port)
        if utils.disable_tracing_hostname(dest_url, blacklist_hostnames):
            return request_func(self, method, url, body,
                                headers, *args, **kwargs)
        _span = _tracer.start_span()
        _span.span_kind = span_module.SpanKind.CLIENT
        _span.name = '[httplib]{}'.format(request_func.__name__)

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

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

        # Store the current span id to thread local.
        execution_context.set_opencensus_attr(
            'httplib/current_span_id', _span.span_id)
        try:
            headers = headers.copy()
            headers.update(_tracer.propagator.to_headers(
                _span.context_tracer.span_context))
        except Exception:  # pragma: NO COVER
            pass
        return request_func(self, method, url, body, headers, *args, **kwargs)
Esempio n. 3
0
    def call(url, *args, **kwargs):
        blacklist_hostnames = execution_context.get_opencensus_attr(
            'blacklist_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, blacklist_hostnames):
            return requests_func(url, *args, **kwargs)

        _tracer = execution_context.get_opencensus_tracer()
        _span = _tracer.start_span()
        _span.name = '[requests]{}'.format(requests_func.__name__)
        _span.span_kind = span_module.SpanKind.CLIENT

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

        result = requests_func(url, *args, **kwargs)

        # Add the status code to attributes
        _tracer.add_attribute_to_current_span(
            HTTP_STATUS_CODE, str(result.status_code))

        _tracer.end_span()
        return result
Esempio n. 4
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()
Esempio n. 5
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]

    blacklist_hostnames = execution_context.get_opencensus_attr(
        'blacklist_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, blacklist_hostnames):
        return wrapped(*args, **kwargs)

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

    _span.name = '[requests]{}'.format(method)
    _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 requests url to attributes
    _tracer.add_attribute_to_current_span(HTTP_URL, url)

    result = wrapped(*args, **kwargs)

    # Add the status code to attributes
    _tracer.add_attribute_to_current_span(HTTP_STATUS_CODE,
                                          str(result.status_code))

    _tracer.end_span()
    return result
Esempio n. 6
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()
Esempio n. 7
0
    def test_disable_tracing_hostname_default(self):
        url = '127.0.0.1:8080'

        disable_tracing = utils.disable_tracing_hostname(url)
        self.assertFalse(disable_tracing)