def test_backwards_compatible(self):
     span = opentracing.tracer.start_span(operation_name='test')
     mgr = RequestContextManager(span)  # span as positional arg
     assert mgr._context.span == span
     mgr = RequestContextManager(context=span)  # span context arg
     assert mgr._context.span == span
     mgr = RequestContextManager(span=span)  # span as span arg
     assert mgr._context.span == span
Beispiel #2
0
    def _start_trace(self):
        if not self.initialized:
            self.tracer = self.cfg.initialize_tracer()
            self.ftracer = FlaskTracer(self.tracer, False, self.app)
            self.initialized = True
        span = self.ftracer.get_span(request)
        if span is None:
            span = self.tracer.start_span(request.endpoint)
        mgr = RequestContextManager(span=span)
        mgr.__enter__()

        request.span = span
        request.mgr = mgr
Beispiel #3
0
 def __call__(self, request):
     new_request = WSGIRequestWrapper.from_wsgi_environ(request.environ)
     info = {"path": request.path, "method": request.method}
     with before_request(new_request) as span:
         span.log_kv(info)
         with RequestContextManager(span) as ctxt:
             response = request.get_response(self.application)
             return self.process_response(response)
 def get_current_context(self):
     """Returns a Context that has parent_tracing attribute"""
     context = RequestContextManager.current_context()
     if not context:
         return None
     if not hasattr(context, 'parent_tracing'):
         context = MockRequestContext(
             span=context.span,
             parent_tracing=SpanWrapper(span=context.span)
         )
     return context
Beispiel #5
0
 def enter_request_context(cls):
     span = opentracing.tracer.get_span()
     cls.set_request_tags(
         span,
         request.method,
         request.url,
         request.user_agent.string,
         request.content_type,
         request.data,
     )
     request.tracing_context = RequestContextManager(span)
     request.tracing_context.__enter__()
Beispiel #6
0
 def process_view(self, request, view_func, view_args, view_kwargs):
     super(IntracingDjangoMiddleware,
           self).process_view(request, view_func, view_args, view_kwargs)
     span = opentracing.tracer.get_span(request)
     self.set_request_tags(
         span,
         request.method,
         request.get_raw_uri(),
         request.META.get('HTTP_USER_AGENT'),
         request.content_type,
         self._get_request_body(request),
     )
     request.tracing_context = RequestContextManager(span)
     request.tracing_context.__enter__()
Beispiel #7
0
    def _apply_tracing(self, request, view_func, attributes):

        # strip headers for trace info
        headers = {}
        for k, v in six.iteritems(request.META):
            k = k.lower().replace('_', '-')
            if k.startswith('http-'):
                k = k[5:]
            headers[k] = v

        # start new span from trace info
        span = None
        operation_name = view_func.__name__
        try:
            span_ctx = self._tracer.extract(opentracing.Format.HTTP_HEADERS,
                                            headers)
            span = self._tracer.start_span(operation_name=operation_name,
                                           child_of=span_ctx)
        except (opentracing.InvalidCarrierException,
                opentracing.SpanContextCorruptedException) as e:
            span = self._tracer.start_span(operation_name=operation_name)
        if span is None:
            span = self._tracer.start_span(operation_name=operation_name)

        # add span to current spans
        self._current_spans[request] = span

        span.set_tag(tags.COMPONENT, 'django')
        span.set_tag(tags.SPAN_KIND, tags.SPAN_KIND_RPC_SERVER)
        span.set_tag(tags.HTTP_METHOD, request.method)
        span.set_tag(tags.HTTP_URL, request.get_full_path())

        # log any traced attributes
        for attr in attributes:
            if hasattr(request, attr):
                payload = str(getattr(request, attr))
                if payload:
                    span.set_tag(attr, payload)

        request.tracing_context_manager = RequestContextManager(span=span)
        request.tracing_context_manager.__enter__()
        return span
Beispiel #8
0
def frontend():
    with RequestContextManager(span=flask_tracer.get_span()):
        requests.get('http://127.0.0.1:8083/backend')
        requests.get('http://127.0.0.1:8084/meaning')
        return 'frontend'
 def mgr():
     return RequestContextManager(span)
 def test_no_span(self):
     ctx = RequestContextManager(context=RequestContext(span='x'))
     assert ctx._context.span == 'x'
     assert RequestContextManager.current_context() is None
 def test_no_span(self):
     ctx = RequestContextManager(span='x')
     assert ctx._span == 'x'
     assert RequestContextManager.current_span() is None
 def test_no_span(self):
     ctx = RequestContextManager(context=RequestContext(span='x'))
     assert ctx._context.span == 'x'
     assert RequestContextManager.current_context() is None