Skip to content

Commit 4a84c5d

Browse files
authored
Merge pull request #67 from instana/load-middleware
Avoid pre-emptively loading Django middleware
2 parents 828438e + bd8a2ef commit 4a84c5d

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

instana/django.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from instana import internal_tracer
44
from instana.log import logger
55
import opentracing.ext.tags as ext
6+
import wrapt
67
import os
78

89

@@ -49,53 +50,57 @@ def process_response(self, request, response):
4950
return response
5051

5152
def process_exception(self, request, exception):
52-
logger.warn("process exception")
5353
if self.span:
5454
self.span.log_kv({'message': exception})
5555
self.span.set_tag("error", True)
5656
ec = self.span.tags.get('ec', 0)
5757
self.span.set_tag("ec", ec+1)
5858

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)
59+
def load_middleware_wrapper(wrapped, instance, args, kwargs):
60+
try:
61+
from django.conf import settings
6462

63+
if DJ_INSTANA_MIDDLEWARE in settings.MIDDLEWARE:
64+
return
65+
66+
if type(settings.MIDDLEWARE) is tuple:
67+
settings.MIDDLEWARE = (DJ_INSTANA_MIDDLEWARE,) + settings.MIDDLEWARE
68+
elif type(settings.MIDDLEWARE) is list:
69+
settings.MIDDLEWARE = [DJ_INSTANA_MIDDLEWARE] + settings.MIDDLEWARE
70+
else:
71+
logger.warn("Instana: Couldn't add InstanaMiddleware to Django")
72+
73+
return wrapped(*args, **kwargs)
74+
75+
except Exception as e:
76+
logger.warn("Instana: Couldn't add InstanaMiddleware to Django: ", e)
6577

6678
def hook(module):
6779
""" Hook method to install the Instana middleware into Django >= 1.10 """
6880
if "INSTANA_DEV" in os.environ:
6981
print("==============================================================")
7082
print("Instana: Running django hook")
7183
print("==============================================================")
72-
73-
if DJ_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE:
74-
return
75-
76-
if type(module.settings.MIDDLEWARE) is tuple:
77-
module.settings.MIDDLEWARE = (
78-
DJ_INSTANA_MIDDLEWARE,) + module.settings.MIDDLEWARE
79-
elif type(module.settings.MIDDLEWARE) is list:
80-
module.settings.MIDDLEWARE = [
81-
DJ_INSTANA_MIDDLEWARE] + module.settings.MIDDLEWARE
82-
else:
83-
print("Instana: Couldn't add InstanaMiddleware to Django")
84+
wrapt.wrap_function_wrapper(module, 'BaseHandler.load_middleware', load_middleware_wrapper)
8485

8586

8687
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
88+
try:
89+
""" Hook method to install the Instana middleware into Django <= 1.9 """
90+
if "INSTANA_DEV" in os.environ:
91+
print("==============================================================")
92+
print("Instana: Running django19 hook")
93+
print("==============================================================")
94+
95+
if DJ_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE_CLASSES:
96+
return
97+
98+
if type(module.settings.MIDDLEWARE_CLASSES) is tuple:
99+
module.settings.MIDDLEWARE_CLASSES = (DJ_INSTANA_MIDDLEWARE,) + module.settings.MIDDLEWARE_CLASSES
100+
elif type(module.settings.MIDDLEWARE_CLASSES) is list:
101+
module.settings.MIDDLEWARE_CLASSES = [DJ_INSTANA_MIDDLEWARE] + module.settings.MIDDLEWARE_CLASSES
102+
else:
103+
logger.warn("Instana: Couldn't add InstanaMiddleware to Django")
95104

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")
105+
except Exception as e:
106+
logger.warn("Instana: Couldn't add InstanaMiddleware to Django: ", e)

0 commit comments

Comments
 (0)