Skip to content

Commit f77c78c

Browse files
authored
Merge pull request #10 from instana/older_django
Backport Django support to older version 1.9
2 parents 0e4e4a9 + 5396ac2 commit f77c78c

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ For this BETA, we currently support tracing of Django and Flask applications or
2424

2525
## Django
2626

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

2929
`export AUTOWRAPT_BOOTSTRAP=django`
3030

31+
For Django version 1.9.x, instead set:
32+
33+
`export AUTOWRAPT_BOOTSTRAP=django19`
34+
3135
## Flask
3236

3337
To enable the Flask instrumentation, set the following environment variable in your _application boot environment_ and then restart your application:

instana/django19.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from __future__ import print_function
2+
import opentracing as ot
3+
from instana import tracer, options
4+
import opentracing.ext.tags as ext
5+
6+
7+
DJ19_INSTANA_MIDDLEWARE = 'instana.django19.InstanaMiddleware19'
8+
9+
10+
class InstanaMiddleware19(object):
11+
""" Django 1.9 Middleware to provide request tracing for Instana """
12+
def __init__(self):
13+
opts = options.Options(service="Django")
14+
ot.global_tracer = tracer.InstanaTracer(opts)
15+
self.span = None
16+
self
17+
18+
def process_request(self, request):
19+
env = request.environ
20+
if 'HTTP_X_INSTANA_T' in env and 'HTTP_X_INSTANA_S' in env:
21+
ctx = ot.global_tracer.extract(ot.Format.HTTP_HEADERS, env)
22+
span = ot.global_tracer.start_span("django", child_of=ctx)
23+
else:
24+
span = ot.global_tracer.start_span("django")
25+
26+
span.set_tag(ext.HTTP_URL, env['PATH_INFO'])
27+
span.set_tag("http.params", env['QUERY_STRING'])
28+
span.set_tag(ext.HTTP_METHOD, request.method)
29+
span.set_tag("http.host", env['HTTP_HOST'])
30+
self.span = span
31+
32+
def process_response(self, request, response):
33+
if self.span:
34+
self.span.set_tag(ext.HTTP_STATUS_CODE, response.status_code)
35+
ot.global_tracer.inject(self.span.context, ot.Format.HTTP_HEADERS, response)
36+
self.span.finish()
37+
self.span = None
38+
return response
39+
40+
41+
42+
def hook(module):
43+
""" Hook method to install the Instana middleware into Django """
44+
if DJ19_INSTANA_MIDDLEWARE in module.settings.MIDDLEWARE_CLASSES:
45+
return
46+
47+
if type(module.settings.MIDDLEWARE_CLASSES) is tuple:
48+
module.settings.MIDDLEWARE_CLASSES = (DJ19_INSTANA_MIDDLEWARE,) + module.settings.MIDDLEWARE_CLASSES
49+
elif type(module.settings.MIDDLEWARE_CLASSES) is list:
50+
module.settings.MIDDLEWARE_CLASSES = [DJ19_INSTANA_MIDDLEWARE] + module.settings.MIDDLEWARE_CLASSES
51+
else:
52+
print("Instana: Couldn't add InstanaMiddleware to Django")

instana/meter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ def collect_snapshot(self):
150150
try:
151151
if "FLASK_APP" in os.environ:
152152
appname = os.environ["FLASK_APP"]
153+
elif "DJANGO_SETTINGS_MODULE" in os.environ:
154+
appname = os.environ["DJANGO_SETTINGS_MODULE"].split('.')[0]
153155
else:
154156
appname = os.path.basename(sys.executable)
155157

instana/propagator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def inject(self, span_context, carrier):
1616
try:
1717
trace_id = util.id_to_header(span_context.trace_id)
1818
span_id = util.id_to_header(span_context.span_id)
19-
if type(carrier) is dict:
19+
if type(carrier) is dict or hasattr(carrier, "__dict__"):
2020
carrier[field_name_trace_id] = trace_id
2121
carrier[field_name_span_id] = span_id
2222
elif type(carrier) is list:
@@ -32,7 +32,7 @@ def inject(self, span_context, carrier):
3232

3333
def extract(self, carrier): # noqa
3434
try:
35-
if type(carrier) is dict:
35+
if type(carrier) is dict or hasattr(carrier, "__dict__"):
3636
dc = carrier
3737
elif type(carrier) is list:
3838
dc = dict(carrier)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
'basictracer>=2.2.0',
1919
'psutil>=5.1.3'],
2020
entry_points={'django': ['django.core.handlers.base = instana.django:hook'],
21+
'django19': ['django.core.handlers.base = instana.django19:hook'],
2122
'flask': ['flask.cli = instana.flaskana:hook'],
2223
'runtime': ['string = instana.runtime:hook']},
2324
test_suite='nose.collector',

0 commit comments

Comments
 (0)