Skip to content

Commit

Permalink
Merge pull request #10 from instana/older_django
Browse files Browse the repository at this point in the history
Backport Django support to older version 1.9
  • Loading branch information
pglombardo authored Jul 28, 2017
2 parents 0e4e4a9 + 5396ac2 commit f77c78c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ For this BETA, we currently support tracing of Django and Flask applications or

## Django

To enable the Django instrumentation, set the following environment variable in your _application boot environment_ and then restart your application:
For Django versions >= 1.10 set the following environment variable in your _application boot environment_ and then restart your application:

`export AUTOWRAPT_BOOTSTRAP=django`

For Django version 1.9.x, instead set:

`export AUTOWRAPT_BOOTSTRAP=django19`

## Flask

To enable the Flask instrumentation, set the following environment variable in your _application boot environment_ and then restart your application:
Expand Down
52 changes: 52 additions & 0 deletions instana/django19.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from __future__ import print_function
import opentracing as ot
from instana import tracer, options
import opentracing.ext.tags as ext


DJ19_INSTANA_MIDDLEWARE = 'instana.django19.InstanaMiddleware19'


class InstanaMiddleware19(object):
""" Django 1.9 Middleware to provide request tracing for Instana """
def __init__(self):
opts = options.Options(service="Django")
ot.global_tracer = tracer.InstanaTracer(opts)
self.span = None
self

def process_request(self, request):
env = request.environ
if 'HTTP_X_INSTANA_T' in env and 'HTTP_X_INSTANA_S' in env:
ctx = ot.global_tracer.extract(ot.Format.HTTP_HEADERS, env)
span = ot.global_tracer.start_span("django", child_of=ctx)
else:
span = ot.global_tracer.start_span("django")

span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
span.set_tag("http.params", env['QUERY_STRING'])
span.set_tag(ext.HTTP_METHOD, request.method)
span.set_tag("http.host", env['HTTP_HOST'])
self.span = span

def process_response(self, request, response):
if self.span:
self.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
ot.global_tracer.inject(self.span.context, ot.Format.HTTP_HEADERS, response)
self.span.finish()
self.span = None
return response



def hook(module):
""" Hook method to install the Instana middleware into Django """
if DJ19_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE_CLASSES:
return

if type(module.settings.MIDDLEWARE_CLASSES) is tuple:
module.settings.MIDDLEWARE_CLASSES = (DJ19_INSTANA_MIDDLEWARE,) + module.settings.MIDDLEWARE_CLASSES
elif type(module.settings.MIDDLEWARE_CLASSES) is list:
module.settings.MIDDLEWARE_CLASSES = [DJ19_INSTANA_MIDDLEWARE] + module.settings.MIDDLEWARE_CLASSES
else:
print("Instana: Couldn't add InstanaMiddleware to Django")
2 changes: 2 additions & 0 deletions instana/meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ def collect_snapshot(self):
try:
if "FLASK_APP" in os.environ:
appname = os.environ["FLASK_APP"]
elif "DJANGO_SETTINGS_MODULE" in os.environ:
appname = os.environ["DJANGO_SETTINGS_MODULE"].split('.')[0]
else:
appname = os.path.basename(sys.executable)

Expand Down
4 changes: 2 additions & 2 deletions instana/propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def inject(self, span_context, carrier):
try:
trace_id = util.id_to_header(span_context.trace_id)
span_id = util.id_to_header(span_context.span_id)
if type(carrier) is dict:
if type(carrier) is dict or hasattr(carrier, "__dict__"):
carrier[field_name_trace_id] = trace_id
carrier[field_name_span_id] = span_id
elif type(carrier) is list:
Expand All @@ -32,7 +32,7 @@ def inject(self, span_context, carrier):

def extract(self, carrier): # noqa
try:
if type(carrier) is dict:
if type(carrier) is dict or hasattr(carrier, "__dict__"):
dc = carrier
elif type(carrier) is list:
dc = dict(carrier)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'basictracer>=2.2.0',
'psutil>=5.1.3'],
entry_points={'django': ['django.core.handlers.base = instana.django:hook'],
'django19': ['django.core.handlers.base = instana.django19:hook'],
'flask': ['flask.cli = instana.flaskana:hook'],
'runtime': ['string = instana.runtime:hook']},
test_suite='nose.collector',
Expand Down

0 comments on commit f77c78c

Please sign in to comment.