Skip to content

Commit

Permalink
Merge pull request #67 from instana/load-middleware
Browse files Browse the repository at this point in the history
Avoid pre-emptively loading Django middleware
  • Loading branch information
pglombardo authored Mar 19, 2018
2 parents 828438e + bd8a2ef commit 4a84c5d
Showing 1 changed file with 37 additions and 32 deletions.
69 changes: 37 additions & 32 deletions instana/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from instana import internal_tracer
from instana.log import logger
import opentracing.ext.tags as ext
import wrapt
import os


Expand Down Expand Up @@ -49,53 +50,57 @@ def process_response(self, request, response):
return response

def process_exception(self, request, exception):
logger.warn("process exception")
if self.span:
self.span.log_kv({'message': exception})
self.span.set_tag("error", True)
ec = self.span.tags.get('ec', 0)
self.span.set_tag("ec", ec+1)

# def process_template_response(self, request, response):
# logger.warn("process template response")
#
# def process_view(self, request, view_func, view_args, view_kwargs):
# logger.warn("process_view %s %s %s %s", request, view_func, view_args, view_kwargs)
def load_middleware_wrapper(wrapped, instance, args, kwargs):
try:
from django.conf import settings

if DJ_INSTANA_MIDDLEWARE in settings.MIDDLEWARE:
return

if type(settings.MIDDLEWARE) is tuple:
settings.MIDDLEWARE = (DJ_INSTANA_MIDDLEWARE,) + settings.MIDDLEWARE
elif type(settings.MIDDLEWARE) is list:
settings.MIDDLEWARE = [DJ_INSTANA_MIDDLEWARE] + settings.MIDDLEWARE
else:
logger.warn("Instana: Couldn't add InstanaMiddleware to Django")

return wrapped(*args, **kwargs)

except Exception as e:
logger.warn("Instana: Couldn't add InstanaMiddleware to Django: ", e)

def hook(module):
""" Hook method to install the Instana middleware into Django >= 1.10 """
if "INSTANA_DEV" in os.environ:
print("==============================================================")
print("Instana: Running django hook")
print("==============================================================")

if DJ_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE:
return

if type(module.settings.MIDDLEWARE) is tuple:
module.settings.MIDDLEWARE = (
DJ_INSTANA_MIDDLEWARE,) + module.settings.MIDDLEWARE
elif type(module.settings.MIDDLEWARE) is list:
module.settings.MIDDLEWARE = [
DJ_INSTANA_MIDDLEWARE] + module.settings.MIDDLEWARE
else:
print("Instana: Couldn't add InstanaMiddleware to Django")
wrapt.wrap_function_wrapper(module, 'BaseHandler.load_middleware', load_middleware_wrapper)


def hook19(module):
""" Hook method to install the Instana middleware into Django <= 1.9 """
if "INSTANA_DEV" in os.environ:
print("==============================================================")
print("Instana: Running django19 hook")
print("==============================================================")

if DJ_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE_CLASSES:
return
try:
""" Hook method to install the Instana middleware into Django <= 1.9 """
if "INSTANA_DEV" in os.environ:
print("==============================================================")
print("Instana: Running django19 hook")
print("==============================================================")

if DJ_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE_CLASSES:
return

if type(module.settings.MIDDLEWARE_CLASSES) is tuple:
module.settings.MIDDLEWARE_CLASSES = (DJ_INSTANA_MIDDLEWARE,) + module.settings.MIDDLEWARE_CLASSES
elif type(module.settings.MIDDLEWARE_CLASSES) is list:
module.settings.MIDDLEWARE_CLASSES = [DJ_INSTANA_MIDDLEWARE] + module.settings.MIDDLEWARE_CLASSES
else:
logger.warn("Instana: Couldn't add InstanaMiddleware to Django")

if type(module.settings.MIDDLEWARE_CLASSES) is tuple:
module.settings.MIDDLEWARE_CLASSES = (DJ_INSTANA_MIDDLEWARE,) + module.settings.MIDDLEWARE_CLASSES
elif type(module.settings.MIDDLEWARE_CLASSES) is list:
module.settings.MIDDLEWARE_CLASSES = [DJ_INSTANA_MIDDLEWARE] + module.settings.MIDDLEWARE_CLASSES
else:
print("Instana: Couldn't add InstanaMiddleware to Django")
except Exception as e:
logger.warn("Instana: Couldn't add InstanaMiddleware to Django: ", e)

0 comments on commit 4a84c5d

Please sign in to comment.