diff --git a/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py b/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py index a8f12fdb0..d1fd85d3b 100644 --- a/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py +++ b/contrib/opencensus-ext-django/opencensus/ext/django/middleware.py @@ -208,7 +208,7 @@ def process_request(self, request): span.span_kind = span_module.SpanKind.SERVER tracer.add_attribute_to_current_span( attribute_key=HTTP_HOST, - attribute_value=request.get_host()) + attribute_value=request._get_raw_host()) tracer.add_attribute_to_current_span( attribute_key=HTTP_METHOD, attribute_value=request.method) @@ -220,7 +220,7 @@ def process_request(self, request): attribute_value=str(request.path)) tracer.add_attribute_to_current_span( attribute_key=HTTP_URL, - attribute_value=str(request.build_absolute_uri())) + attribute_value=str(request.get_raw_uri())) # Add the span to thread local # in some cases (exceptions, timeouts) currentspan in diff --git a/contrib/opencensus-ext-django/tests/test_django_middleware.py b/contrib/opencensus-ext-django/tests/test_django_middleware.py index 522ea458c..30bff507f 100644 --- a/contrib/opencensus-ext-django/tests/test_django_middleware.py +++ b/contrib/opencensus-ext-django/tests/test_django_middleware.py @@ -17,7 +17,7 @@ import unittest import mock -from django.test import RequestFactory +from django.test import RequestFactory, override_settings from django.test.utils import teardown_test_environment from opencensus.trace import execution_context, print_exporter, samplers @@ -129,6 +129,59 @@ def test_process_request(self): self.assertEqual(span.name, 'mock.mock.Mock') + @override_settings(ALLOWED_HOSTS=['allowedhost']) + def test_process_request_with_disallowed_host(self): + from opencensus.ext.django import middleware + + trace_id = '2dd43a1d6b2549c6bc2a1a54c2fc0b05' + span_id = '6e0c63257de34c92' + django_trace_id = '00-{}-{}-00'.format(trace_id, span_id) + + django_request = RequestFactory().get('/wiki/Rabbit', **{ + 'HTTP_HOST': 'notallowedhost', + 'HTTP_TRACEPARENT': django_trace_id}) + + # Force the test request to be sampled + settings = type('Test', (object,), {}) + settings.OPENCENSUS = { + 'TRACE': { + 'SAMPLER': 'opencensus.trace.samplers.AlwaysOnSampler()', # noqa + } + } + patch_settings = mock.patch( + 'django.conf.settings', + settings) + + with patch_settings: + middleware_obj = middleware.OpencensusMiddleware() + + # test process_request + middleware_obj.process_request(django_request) + + tracer = middleware._get_current_tracer() + + span = tracer.current_span() + + expected_attributes = { + 'http.host': u'notallowedhost', + 'http.method': 'GET', + 'http.path': u'/wiki/Rabbit', + 'http.route': u'/wiki/Rabbit', + 'http.url': u'http://notallowedhost/wiki/Rabbit', + } + self.assertEqual(span.span_kind, span_module.SpanKind.SERVER) + self.assertEqual(span.attributes, expected_attributes) + self.assertEqual(span.parent_span.span_id, span_id) + + span_context = tracer.span_context + self.assertEqual(span_context.trace_id, trace_id) + + # test process_view + view_func = mock.Mock() + middleware_obj.process_view(django_request, view_func) + + self.assertEqual(span.name, 'mock.mock.Mock') + def test_excludelist_path(self): from opencensus.ext.django import middleware