Skip to content

Commit 35c9d16

Browse files
authored
Django: Support live reloading of middleware (#181)
1 parent 89911a1 commit 35c9d16

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

instana/instrumentation/django/middleware.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import
22

3+
import os
34
import sys
45

56
import opentracing as ot
@@ -22,6 +23,7 @@ class InstanaMiddleware(MiddlewareMixin):
2223
""" Django Middleware to provide request tracing for Instana """
2324
def __init__(self, get_response=None):
2425
self.get_response = get_response
26+
super(InstanaMiddleware, self).__init__(get_response=get_response)
2527

2628
def process_request(self, request):
2729
try:
@@ -124,5 +126,14 @@ def load_middleware_wrapper(wrapped, instance, args, kwargs):
124126
if 'django' in sys.modules:
125127
logger.debug("Instrumenting django")
126128
wrapt.wrap_function_wrapper('django.core.handlers.base', 'BaseHandler.load_middleware', load_middleware_wrapper)
129+
130+
if 'INSTANA_MAGIC' in os.environ:
131+
# If we are instrumenting via AutoTrace (in an already running process), then the
132+
# WSGI middleware has to be live reloaded.
133+
from django.core.servers.basehttp import get_internal_wsgi_application
134+
wsgiapp = get_internal_wsgi_application()
135+
wsgiapp.load_middleware()
136+
127137
except Exception:
138+
logger.debug("django.middleware:", exc_info=True)
128139
pass

0 commit comments

Comments
 (0)