Skip to content

Commit 0f49d79

Browse files
author
Andrey Slotin
authored
Do not mark Django HTTP 404 response spans as errors (#294)
1 parent 6a4d4f0 commit 0f49d79

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

instana/instrumentation/django/middleware.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ def process_response(self, request, response):
5959
request.iscope.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
6060
tracer.inject(request.iscope.span.context, ot.Format.HTTP_HEADERS, response)
6161
response['Server-Timing'] = "intid;desc=%s" % request.iscope.span.context.trace_id
62-
6362
except Exception:
6463
logger.debug("Instana middleware @ process_response", exc_info=True)
6564
finally:
@@ -69,6 +68,11 @@ def process_response(self, request, response):
6968
return response
7069

7170
def process_exception(self, request, exception):
71+
from django.http.response import Http404
72+
73+
if isinstance(exception, Http404):
74+
return None
75+
7276
if request.iscope is not None:
7377
request.iscope.span.log_exception(exception)
7478

tests/apps/app_django.py

100644100755
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import opentracing.ext.tags as ext
88

99
from django.conf.urls import url
10-
from django.http import HttpResponse
10+
from django.http import HttpResponse, Http404
1111

1212
filepath, extension = os.path.splitext(__file__)
1313
os.environ['DJANGO_SETTINGS_MODULE'] = os.path.basename(filepath)
@@ -91,6 +91,10 @@ def another(request):
9191
return HttpResponse('Stan wuz here!')
9292

9393

94+
def not_found(request):
95+
raise Http404('Nothing here')
96+
97+
9498
def complex(request):
9599
with opentracing.tracer.start_active_span('asteroid') as pscope:
96100
pscope.span.set_tag(ext.COMPONENT, "Python simple example app")
@@ -118,5 +122,6 @@ def complex(request):
118122
url(r'^$', index, name='index'),
119123
url(r'^cause_error$', cause_error, name='cause_error'),
120124
url(r'^another$', another),
125+
url(r'^not_found$', not_found, name='not_found'),
121126
url(r'^complex$', complex, name='complex')
122127
]

tests/frameworks/test_django.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_synthetic_request(self):
7777
headers = {
7878
'X-Instana-Synthetic': '1'
7979
}
80-
80+
8181
with tracer.start_active_span('test'):
8282
response = self.http.request('GET', self.live_server_url + '/', headers=headers)
8383

@@ -155,6 +155,28 @@ def test_request_with_error(self):
155155
self.assertEqual('This is a fake error: /cause-error', django_span.data["http"]["error"])
156156
self.assertIsNone(django_span.stack)
157157

158+
def test_request_with_not_found(self):
159+
with tracer.start_active_span('test'):
160+
response = self.http.request('GET', self.live_server_url + '/not_found')
161+
162+
assert response
163+
self.assertEqual(404, response.status)
164+
165+
spans = self.recorder.queued_spans()
166+
spans = drop_log_spans_from_list(spans)
167+
168+
span_count = len(spans)
169+
if span_count != 3:
170+
msg = "Expected 3 spans but got %d" % span_count
171+
fail_with_message_and_span_dump(msg, spans)
172+
173+
filter = lambda span: span.n == 'django'
174+
django_span = get_first_span_by_filter(spans, filter)
175+
assert(django_span)
176+
177+
self.assertIsNone(django_span.ec)
178+
self.assertEqual(404, django_span.data["http"]["status"])
179+
158180
def test_complex_request(self):
159181
with tracer.start_active_span('test'):
160182
response = self.http.request('GET', self.live_server_url + '/complex')

0 commit comments

Comments
 (0)