Skip to content

Commit

Permalink
Merge pull request #9 from ocadotechnology/fixes
Browse files Browse the repository at this point in the history
fixes for initialization
  • Loading branch information
bhs authored Dec 30, 2017
2 parents 9b33c75 + 1d7aa68 commit f0f5ab1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
12 changes: 12 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ In order to implement tracing in your system, add the following lines of code to
# only valid if OPENTRACING_TRACE_ALL == True
OPENTRACING_TRACED_ATTRIBUTES = ['arg1', 'arg2'],
# Callable that returns an `opentracing.Tracer` implementation.
OPENTRACING_TRACER_CALLABLE = 'opentracing.Tracer'
# Parameters for the callable (Depending on the tracer implementation chosen)
OPENTRACING_TRACER_PARAMETERS = {
'example-parameter-host': 'collector',
}
If you want to directly override the `DjangoTracer` used, you can use the following. This may cause import loops (See #10)

.. code-block:: python
# some_opentracing_tracer can be any valid OpenTracing tracer implementation
OPENTRACING_TRACER = django_opentracing.DjangoTracer(some_opentracing_tracer),
Expand Down
11 changes: 5 additions & 6 deletions django_opentracing/middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.conf import settings
import opentracing
from django.conf import settings
from django_opentracing.tracer import initialize_global_tracer
try:
# Django >= 1.10
from django.utils.deprecation import MiddlewareMixin
Expand All @@ -18,10 +18,9 @@ def __init__(self, get_response=None):
- Is it better to place all tracing info in the settings file, or to require a tracing.py file with configurations?
- Also, better to have try/catch with empty tracer or just fail fast if there's no tracer specified
'''
if hasattr(settings, 'OPENTRACING_TRACER'):
self._tracer = settings.OPENTRACING_TRACER
else:
self._tracer = opentracing.Tracer()
self.get_response = get_response
initialize_global_tracer()
self._tracer = settings.OPENTRACING_TRACER

def process_view(self, request, view_func, view_args, view_kwargs):
# determine whether this middleware should be applied
Expand Down
44 changes: 41 additions & 3 deletions django_opentracing/tracer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from django.conf import settings
from django.utils.module_loading import import_string
import opentracing
import threading

class DjangoTracer(object):
'''
@param tracer the OpenTracing tracer to be used
to trace requests using this DjangoTracer
'''
def __init__(self, tracer):
self._tracer = tracer
def __init__(self, tracer=None):
self._tracer_implementation = None
if tracer:
self._tracer_implementation = tracer
self._current_spans = {}
if not hasattr(settings, 'OPENTRACING_TRACE_ALL'):
self._trace_all = False
Expand All @@ -16,6 +20,13 @@ def __init__(self, tracer):
else:
self._trace_all = True

@property
def _tracer(self):
if self._tracer_implementation:
return self._tracer_implementation
else:
return opentracing.tracer

def get_span(self, request):
'''
@param request
Expand Down Expand Up @@ -85,4 +96,31 @@ def _apply_tracing(self, request, view_func, attributes):
def _finish_tracing(self, request):
span = self._current_spans.pop(request, None)
if span is not None:
span.finish()
span.finish()


def initialize_global_tracer():
'''
Initialisation as per https://github.com/opentracing/opentracing-python/blob/9f9ef02d4ef7863fb26d3534a38ccdccf245494c/opentracing/__init__.py#L36
Here the global tracer object gets initialised once from Django settings.
'''
# Short circuit without taking a lock
if initialize_global_tracer.complete:
return
with initialize_global_tracer.lock:
if initialize_global_tracer.complete:
return
if hasattr(settings, 'OPENTRACING_TRACER'):
# Backwards compatibility with the old way of defining the tracer
opentracing.tracer = settings.OPENTRACING_TRACER._tracer
else:
tracer_callable = getattr(settings, 'OPENTRACING_TRACER_CALLABLE', 'opentracing.Tracer')
tracer_parameters = getattr(settings, 'OPENTRACING_TRACER_PARAMETERS', {})
opentracing.tracer = import_string(tracer_callable)(**tracer_parameters)
settings.OPENTRACING_TRACER = DjangoTracer()
initialize_global_tracer.complete = True


initialize_global_tracer.lock = threading.Lock()
initialize_global_tracer.complete = False

0 comments on commit f0f5ab1

Please sign in to comment.