|
3 | 3 | from instana import internal_tracer |
4 | 4 | from instana.log import logger |
5 | 5 | import opentracing.ext.tags as ext |
| 6 | +import wrapt |
6 | 7 | import os |
7 | 8 |
|
8 | 9 |
|
@@ -49,53 +50,57 @@ def process_response(self, request, response): |
49 | 50 | return response |
50 | 51 |
|
51 | 52 | def process_exception(self, request, exception): |
52 | | - logger.warn("process exception") |
53 | 53 | if self.span: |
54 | 54 | self.span.log_kv({'message': exception}) |
55 | 55 | self.span.set_tag("error", True) |
56 | 56 | ec = self.span.tags.get('ec', 0) |
57 | 57 | self.span.set_tag("ec", ec+1) |
58 | 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) |
| 59 | +def load_middleware_wrapper(wrapped, instance, args, kwargs): |
| 60 | + try: |
| 61 | + from django.conf import settings |
64 | 62 |
|
| 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) |
65 | 77 |
|
66 | 78 | def hook(module): |
67 | 79 | """ Hook method to install the Instana middleware into Django >= 1.10 """ |
68 | 80 | if "INSTANA_DEV" in os.environ: |
69 | 81 | print("==============================================================") |
70 | 82 | print("Instana: Running django hook") |
71 | 83 | 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) |
84 | 85 |
|
85 | 86 |
|
86 | 87 | 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") |
95 | 104 |
|
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