11from __future__ import print_function
22import opentracing as ot
33from instana import internal_tracer
4+ from instana .log import logger
45import opentracing .ext .tags as ext
56import os
67
78
89DJ_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
4366def 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" )
0 commit comments