-
Notifications
You must be signed in to change notification settings - Fork 51
Django updates #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c96b899
5540a13
30a9fc4
78ecefe
75d5c47
ae7f378
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
from .middleware import OpenTracingMiddleware | ||
from .tracer import DjangoTracer | ||
from .tracer import DjangoTracer, get_current_span, get_tracer |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,30 @@ | ||
from builtins import object, str | ||
from django.conf import settings | ||
import opentracing | ||
|
||
django_tracer = None | ||
|
||
def get_tracer(): | ||
return opentracing.tracer | ||
|
||
def get_current_span(request): | ||
# this lets django rest framework work seamlessly since they wrap the request | ||
if hasattr(request, '_request'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is something we could add, yes. |
||
request = request._request | ||
if django_tracer != None: | ||
return django_tracer.get_span(request) | ||
else: | ||
return None | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So far we had been saving the span for each request in a dictionary, as part of the |
||
class DjangoTracer(object): | ||
''' | ||
@param tracer the OpenTracing tracer to be used | ||
to trace requests using this DjangoTracer | ||
''' | ||
def __init__(self, tracer): | ||
global django_tracer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I don't like the idea of having a global here. We have it for other connectors, but for frameworks we have this toplevel-tracer approach, which is most likely easier/better. As before, if there's any need for this change, we can take it into consideration. |
||
django_tracer = self | ||
self._tracer = tracer | ||
self._current_spans = {} | ||
if not hasattr(settings, 'OPENTRACING_TRACE_ALL'): | ||
|
@@ -16,9 +34,9 @@ def __init__(self, tracer): | |
else: | ||
self._trace_all = True | ||
|
||
def get_span(self, request): | ||
def get_span(self, request): | ||
''' | ||
@param request | ||
@param request | ||
Returns the span tracing this request | ||
''' | ||
return self._current_spans.get(request, None) | ||
|
@@ -53,11 +71,11 @@ def _apply_tracing(self, request, view_func, attributes): | |
''' | ||
# strip headers for trace info | ||
headers = {} | ||
for k,v in request.META.iteritems(): | ||
for k,v in list(request.META.items()): | ||
k = k.lower().replace('_','-') | ||
if k.startswith('http-'): | ||
k = k[5:] | ||
headers[k] = v | ||
headers[k] = v | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually spaces/styling changes shouldn't exist (unless we are trying to follow |
||
|
||
# start new span from trace info | ||
span = None | ||
|
@@ -79,10 +97,9 @@ def _apply_tracing(self, request, view_func, attributes): | |
payload = str(getattr(request, attr)) | ||
if payload: | ||
span.set_tag(attr, payload) | ||
|
||
return span | ||
return span | ||
|
||
def _finish_tracing(self, request): | ||
span = self._current_spans.pop(request, None) | ||
span = self._current_spans.pop(request, None) | ||
if span is not None: | ||
span.finish() | ||
span.finish() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
import test_middleware | ||
from __future__ import absolute_import | ||
from . import test_middleware |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there's need for this function, because of it always returning
opentracing.tracer
- moreover, I don't think we need to expose it in the public api. Or is there any use scenario where you need it?