Skip to content

Commit 842ea1f

Browse files
authored
Merge pull request #71 from instana/django_safeties
Add safeties to Django calls
2 parents 684c457 + 4df2d86 commit 842ea1f

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

instana/django.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,39 @@ def __init__(self, get_response=None):
2222
self
2323

2424
def process_request(self, request):
25-
env = request.environ
26-
if 'HTTP_X_INSTANA_T' in env and 'HTTP_X_INSTANA_S' in env:
27-
ctx = internal_tracer.extract(ot.Format.HTTP_HEADERS, env)
28-
span = internal_tracer.start_span("django", child_of=ctx)
29-
else:
30-
span = internal_tracer.start_span("django")
25+
try:
26+
env = request.environ
27+
if 'HTTP_X_INSTANA_T' in env and 'HTTP_X_INSTANA_S' in env:
28+
ctx = internal_tracer.extract(ot.Format.HTTP_HEADERS, env)
29+
span = internal_tracer.start_span("django", child_of=ctx)
30+
else:
31+
span = internal_tracer.start_span("django")
3132

32-
span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
33-
span.set_tag("http.params", env['QUERY_STRING'])
34-
span.set_tag(ext.HTTP_METHOD, request.method)
35-
span.set_tag("http.host", env['HTTP_HOST'])
36-
self.span = span
33+
span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
34+
span.set_tag("http.params", env['QUERY_STRING'])
35+
span.set_tag(ext.HTTP_METHOD, request.method)
36+
span.set_tag("http.host", env['HTTP_HOST'])
37+
self.span = span
38+
except as e:
39+
logger.debug("Instana middleware @ process_response: ", e)
3740

3841
def process_response(self, request, response):
39-
if self.span:
40-
if 500 <= response.status_code <= 511:
41-
self.span.set_tag("error", True)
42-
ec = self.span.tags.get('ec', 0)
43-
if ec is 0:
44-
self.span.set_tag("ec", ec+1)
45-
46-
self.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
47-
internal_tracer.inject(self.span.context, ot.Format.HTTP_HEADERS, response)
48-
self.span.finish()
49-
self.span = None
50-
return response
42+
try:
43+
if self.span:
44+
if 500 <= response.status_code <= 511:
45+
self.span.set_tag("error", True)
46+
ec = self.span.tags.get('ec', 0)
47+
if ec is 0:
48+
self.span.set_tag("ec", ec+1)
49+
50+
self.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
51+
internal_tracer.inject(self.span.context, ot.Format.HTTP_HEADERS, response)
52+
self.span.finish()
53+
self.span = None
54+
except as e:
55+
logger.debug("Instana middleware @ process_response: ", e)
56+
finally:
57+
return response
5158

5259
def process_exception(self, request, exception):
5360
if self.span:

0 commit comments

Comments
 (0)