예제 #1
0
    def test_wrap_httplib_request_blacklist_ok(self):
        mock_span = mock.Mock()
        span_id = '1234'
        mock_span.span_id = span_id
        mock_tracer = MockTracer(mock_span)
        mock_request_func = mock.Mock()
        mock_request_func.__name__ = 'request'

        patch_tracer = mock.patch(
            'opencensus.ext.requests.trace.execution_context.'
            'get_opencensus_tracer',
            return_value=mock_tracer)
        patch_attr = mock.patch(
            'opencensus.ext.requests.trace.execution_context.'
            'get_opencensus_attr',
            return_value=None)

        wrapped = trace.wrap_httplib_request(mock_request_func)

        mock_self = mock.Mock()
        method = 'GET'
        url = 'http://localhost:8080'
        body = None
        headers = {}

        with patch_tracer, patch_attr:
            wrapped(mock_self, method, url, body, headers)

        mock_request_func.assert_called_with(mock_self, method, url, body, {
            'traceparent': '00-123-456-01',
        })
예제 #2
0
    def test_wrap_httplib_request_excludelist_nok(self):
        mock_span = mock.Mock()
        span_id = '1234'
        mock_span.span_id = span_id
        mock_tracer = MockTracer(mock_span)
        mock_request_func = mock.Mock()
        mock_request_func.__name__ = 'request'

        patch_tracer = mock.patch(
            'opencensus.ext.requests.trace.execution_context.'
            'get_opencensus_tracer',
            return_value=mock_tracer)
        patch_attr = mock.patch(
            'opencensus.ext.requests.trace.execution_context.'
            'get_opencensus_attr',
            return_value=['localhost:8080'])
        patch_thread = mock.patch(
            'opencensus.ext.requests.trace.execution_context.'
            'is_exporter',
            return_value=False)

        wrapped = trace.wrap_httplib_request(mock_request_func)

        mock_self = mock.Mock()
        mock_self.host = 'localhost'
        mock_self.port = '8080'
        method = 'GET'
        url = 'http://{}:{}'.format(mock_self.host, mock_self.port)
        body = None
        headers = {}

        with patch_tracer, patch_attr, patch_thread:
            wrapped(mock_self, method, url, body, headers)

        mock_request_func.assert_called_with(mock_self, method, url, body, {})
예제 #3
0
    def test_wrap_httplib_request(self):
        mock_span = mock.Mock()
        span_id = '1234'
        mock_span.span_id = span_id
        mock_tracer = MockTracer(mock_span)
        mock_request_func = mock.Mock()
        mock_request_func.__name__ = 'request'

        patch = mock.patch(
            'opencensus.ext.requests.trace.execution_context.'
            'get_opencensus_tracer',
            return_value=mock_tracer)

        wrapped = trace.wrap_httplib_request(mock_request_func)

        mock_self = mock.Mock()
        method = 'GET'
        url = 'http://localhost:8080'
        body = None
        headers = {}

        with patch:
            wrapped(mock_self, method, url, body, headers)

        expected_attributes = {'http.url': url, 'http.method': method}
        expected_name = '[httplib]request'

        mock_request_func.assert_called_with(mock_self, method, url, body, {
            'traceparent': '00-123-456-01',
        })
        self.assertEqual(expected_attributes, mock_tracer.span.attributes)
        self.assertEqual(expected_name, mock_tracer.span.name)
        self.assertEqual(span_module.SpanKind.CLIENT,
                         mock_tracer.span.span_kind)
예제 #4
0
    def test_wrap_httplib_request_exporter_thread(self):
        mock_request_func = mock.Mock()
        mock_request_func.__name__ = 'request'
        patch_thread = mock.patch(
            'opencensus.ext.requests.trace.execution_context.'
            'is_exporter',
            return_value=True)

        mock_self = mock.Mock()
        mock_self.host = 'localhost'
        mock_self.port = '8080'
        method = 'GET'
        url = 'http://{}:{}'.format(mock_self.host, mock_self.port)
        body = None
        headers = {}

        wrapped = trace.wrap_httplib_request(mock_request_func)

        with patch_thread:
            wrapped(mock_self, method, url, body, headers)

        mock_request_func.assert_called_with(mock_self, method, url, body, {})