def test_wrap_requests(self):
        mock_return = mock.Mock()
        mock_return.status_code = 200
        return_value = mock_return
        mock_func = mock.Mock()
        mock_func.__name__ = 'get'
        mock_func.return_value = return_value
        mock_tracer = MockTracer()

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

        wrapped = trace.wrap_requests(mock_func)

        url = 'http://localhost:8080'

        with patch:
            wrapped(url)

        expected_labels = {'requests/url': url, 'requests/status_code': 200}
        expected_name = '[requests]get'

        self.assertEqual(expected_labels, mock_tracer.current_span.labels)
        self.assertEqual(expected_name, mock_tracer.current_span.name)
예제 #2
0
    def test_wrap_requests(self):
        mock_return = mock.Mock()
        mock_return.status_code = 200
        return_value = mock_return
        mock_func = mock.Mock()
        mock_func.__name__ = 'get'
        mock_func.return_value = return_value
        mock_tracer = MockTracer()

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

        wrapped = trace.wrap_requests(mock_func)

        url = 'http://localhost:8080'

        with patch:
            wrapped(url)

        expected_attributes = {'http.url': url, 'http.status_code': '200'}
        expected_name = '[requests]get'

        self.assertEqual(span_module.SpanKind.CLIENT,
                         mock_tracer.current_span.span_kind)
        self.assertEqual(expected_attributes,
                         mock_tracer.current_span.attributes)
        self.assertEqual(expected_name, mock_tracer.current_span.name)
    def test_wrap_requests_blacklist_nok(self):
        mock_return = mock.Mock()
        mock_return.status_code = 200
        return_value = mock_return
        mock_func = mock.Mock()
        mock_func.__name__ = 'get'
        mock_func.return_value = return_value
        mock_tracer = MockTracer()

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

        wrapped = trace.wrap_requests(mock_func)

        url = 'http://localhost:8080'

        with patch_tracer, patch_attr:
            wrapped(url)

        self.assertEqual(None, mock_tracer.current_span)