def test_wrap_session_request_blacklist_ok(self): def wrapped(*args, **kwargs): result = mock.Mock() result.status_code = 200 return result mock_tracer = MockTracer(propagator=mock.Mock( to_headers=lambda x: {'x-trace': 'some-value'})) 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=None) url = 'http://localhost' request_method = 'POST' with patch_tracer, patch_attr: trace.wrap_session_request(wrapped, 'Session.request', (request_method, url), {}) expected_name = '[requests]POST' self.assertEqual(expected_name, mock_tracer.current_span.name)
def test_wrap_session_request(self): wrapped = mock.Mock(return_value=mock.Mock(status_code=200)) mock_tracer = MockTracer(propagator=mock.Mock( to_headers=lambda x: {'x-trace': 'some-value'})) patch = mock.patch( 'opencensus.trace.ext.requests.trace.execution_context.' 'get_opencensus_tracer', return_value=mock_tracer) url = 'http://localhost:8080' request_method = 'POST' kwargs = {} with patch: trace.wrap_session_request(wrapped, 'Session.request', (request_method, url), kwargs) expected_attributes = {'http.url': url, 'http.status_code': '200'} expected_name = '[requests]POST' self.assertEqual(span_module.SpanKind.CLIENT, mock_tracer.current_span.span_kind) self.assertEqual(expected_attributes, mock_tracer.current_span.attributes) self.assertEqual(kwargs['headers']['x-trace'], 'some-value') self.assertEqual(expected_name, mock_tracer.current_span.name)
def test_wrap_session_request(self): def wrapped(*args, **kwargs): result = mock.Mock() result.status_code = 200 return result mock_tracer = MockTracer() patch = mock.patch( 'opencensus.trace.ext.requests.trace.execution_context.' 'get_opencensus_tracer', return_value=mock_tracer) url = 'http://localhost:8080' request_method = 'POST' with patch: result = trace.wrap_session_request(wrapped, 'Session.request', (request_method, url), {}) expected_attributes = {'http.url': url, 'http.status_code': '200'} expected_name = '[requests]POST' 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_session_request(self): mock_return = mock.Mock() mock_return.status_code = 200 return_value = mock_return mock_func = mock.Mock() 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_session_request(mock_func) url = 'http://localhost:8080' request_method = 'POST' with patch: wrapped(request_method, url) expected_labels = {'requests/url': url, 'requests/status_code': 200} expected_name = '[requests]POST' self.assertEqual(expected_labels, mock_tracer.current_span.labels) self.assertEqual(expected_name, mock_tracer.current_span.name)
def test_tracer_headers_are_overwritten(self): wrapped = mock.Mock(return_value=mock.Mock(status_code=200)) mock_tracer = MockTracer(propagator=mock.Mock( to_headers=lambda x: {'x-trace': 'some-value'})) patch = mock.patch( 'opencensus.trace.ext.requests.trace.execution_context.' 'get_opencensus_tracer', return_value=mock_tracer) url = 'http://localhost:8080' request_method = 'POST' kwargs = {'headers': {'x-trace': 'original-value'}} with patch: trace.wrap_session_request(wrapped, 'Session.request', (request_method, url), kwargs) self.assertEqual(kwargs['headers']['x-trace'], 'some-value')
def test_wrap_session_request_blacklist_nok(self): def wrapped(*args, **kwargs): result = mock.Mock() result.status_code = 200 return result 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']) url = 'http://localhost:8080' request_method = 'POST' with patch_tracer, patch_attr: trace.wrap_session_request(wrapped, 'Session.request', (request_method, url), {}) self.assertEqual(None, mock_tracer.current_span)