Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix set trace attributes for django when host is not allowed #1026

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
55 changes: 54 additions & 1 deletion contrib/opencensus-ext-django/tests/test_django_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down