Skip to content

Commit ecbcc8a

Browse files
committed
Unify Django middleware into one; Update hooks
1 parent 06450e4 commit ecbcc8a

File tree

3 files changed

+55
-74
lines changed

3 files changed

+55
-74
lines changed

instana/django.py

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
from __future__ import print_function
22
import opentracing as ot
33
from instana import internal_tracer
4+
from instana.log import logger
45
import opentracing.ext.tags as ext
56
import os
67

78

89
DJ_INSTANA_MIDDLEWARE = 'instana.django.InstanaMiddleware'
910

11+
try:
12+
from django.utils.deprecation import MiddlewareMixin
13+
except ImportError:
14+
MiddlewareMixin = object
1015

11-
class InstanaMiddleware(object):
16+
17+
class InstanaMiddleware(MiddlewareMixin):
1218
""" Django Middleware to provide request tracing for Instana """
13-
def __init__(self, get_response):
19+
def __init__(self, get_response=None):
1420
self.get_response = get_response
1521
self
1622

17-
def __call__(self, request):
23+
def process_request(self, request):
1824
env = request.environ
1925
if 'HTTP_X_INSTANA_T' in env and 'HTTP_X_INSTANA_S' in env:
2026
ctx = internal_tracer.extract(ot.Format.HTTP_HEADERS, env)
@@ -26,22 +32,39 @@ def __call__(self, request):
2632
span.set_tag("http.params", env['QUERY_STRING'])
2733
span.set_tag(ext.HTTP_METHOD, request.method)
2834
span.set_tag("http.host", env['HTTP_HOST'])
35+
self.span = span
2936

30-
response = self.get_response(request)
31-
32-
if 500 <= response.status_code <= 511:
33-
span.set_tag("error", True)
34-
ec = span.tags.get('ec', 0)
35-
span.set_tag("ec", ec+1)
37+
def process_response(self, request, response):
38+
if self.span:
39+
if 500 <= response.status_code <= 511:
40+
self.span.set_tag("error", True)
41+
ec = self.span.tags.get('ec', 0)
42+
if ec is 0:
43+
self.span.set_tag("ec", ec+1)
3644

37-
span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
38-
internal_tracer.inject(span.context, ot.Format.HTTP_HEADERS, response)
39-
span.finish()
45+
self.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
46+
internal_tracer.inject(self.span.context, ot.Format.HTTP_HEADERS, response)
47+
self.span.finish()
48+
self.span = None
4049
return response
4150

51+
def process_exception(self, request, exception):
52+
logger.warn("process exception")
53+
if self.span:
54+
self.span.log_kv({'message': exception})
55+
self.span.set_tag("error", True)
56+
ec = self.span.tags.get('ec', 0)
57+
self.span.set_tag("ec", ec+1)
58+
59+
# def process_template_response(self, request, response):
60+
# logger.warn("process template response")
61+
#
62+
# def process_view(self, request, view_func, view_args, view_kwargs):
63+
# logger.warn("process_view %s %s %s %s", request, view_func, view_args, view_kwargs)
64+
4265

4366
def hook(module):
44-
""" Hook method to install the Instana middleware into Django """
67+
""" Hook method to install the Instana middleware into Django >= 1.10 """
4568
if "INSTANA_DEV" in os.environ:
4669
print("==============================================================")
4770
print("Instana: Running django hook")
@@ -58,3 +81,21 @@ def hook(module):
5881
DJ_INSTANA_MIDDLEWARE] + module.settings.MIDDLEWARE
5982
else:
6083
print("Instana: Couldn't add InstanaMiddleware to Django")
84+
85+
86+
def hook19(module):
87+
""" Hook method to install the Instana middleware into Django <= 1.9 """
88+
if "INSTANA_DEV" in os.environ:
89+
print("==============================================================")
90+
print("Instana: Running django19 hook")
91+
print("==============================================================")
92+
93+
if DJ_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE_CLASSES:
94+
return
95+
96+
if type(module.settings.MIDDLEWARE_CLASSES) is tuple:
97+
module.settings.MIDDLEWARE_CLASSES = (DJ_INSTANA_MIDDLEWARE,) + module.settings.MIDDLEWARE_CLASSES
98+
elif type(module.settings.MIDDLEWARE_CLASSES) is list:
99+
module.settings.MIDDLEWARE_CLASSES = [DJ_INSTANA_MIDDLEWARE] + module.settings.MIDDLEWARE_CLASSES
100+
else:
101+
print("Instana: Couldn't add InstanaMiddleware to Django")

instana/django19.py

Lines changed: 0 additions & 60 deletions
This file was deleted.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
'opentracing>=1.2.1,<1.3',
1818
'basictracer>=2.2.0'],
1919
entry_points={'django': ['django.core.handlers.base = instana.django:hook'],
20-
'django19': ['django.core.handlers.base = instana.django19:hook'],
20+
'django19': ['django.core.handlers.base = instana.django:hook19'],
2121
'flask': ['flask = instana.flaskana:hook'],
2222
'runtime': ['string = instana.runtime:hook']},
2323
test_suite='nose.collector',

0 commit comments

Comments
 (0)