Example #1
0
File: server.py Project: ziq211/hue
    def get_span(self, http_request, update_span_func):
        span = http_server.before_request(http_server.TornadoRequestWrapper(request=http_request),
                                          self.tracer)
        if update_span_func:
            update_span_func(span)

        return span
Example #2
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 test_middleware(with_peer_tags):
    request = mock.MagicMock()
    request.full_url = 'http://localhost:12345/test'
    request.operation = 'my-test'
    if with_peer_tags:
        request.remote_ip = 'localhost'
        request.remote_port = 12345
        request.caller_name = 'test_middleware'
    else:
        request.remote_ip = None
        request.remote_port = None
        request.caller_name = None

    tracer = opentracing.tracer
    span = mock.MagicMock()
    with mock.patch.object(tracer, 'start_trace',
                           return_value=span) as start_trace, \
            mock.patch.object(tracer, 'trace_context_from_text',
                              return_value=None):
        span2 = http_server.before_request(request=request, tracer=tracer)
        assert span == span2
        start_trace.assert_called_with(operation_name='my-test')
        span.set_tag.assert_any_call('client.http.url', request.full_url)
        if with_peer_tags:
            span.set_tag.assert_any_call(tags.PEER_HOST_IPV4, 'localhost')
            span.set_tag.assert_any_call(tags.PEER_PORT, 12345)
            span.set_tag.assert_any_call(tags.PEER_SERVICE, 'test_middleware')

    # now test server when it looks like there is a trace in the headers
    span = mock.MagicMock()
    context = mock.MagicMock()
    with mock.patch.object(tracer, 'join_trace',
                           return_value=span) as join_trace, \
            mock.patch.object(tracer, 'trace_context_from_text',
                              return_value=context):
        span2 = http_server.before_request(request=request, tracer=tracer)
        assert span == span2
        join_trace.assert_called_with(operation_name='my-test',
                                      parent_trace_context=context)
        span.set_tag.assert_any_call('client.http.url', request.full_url)
        if with_peer_tags:
            span.set_tag.assert_any_call(tags.PEER_HOST_IPV4, 'localhost')
            span.set_tag.assert_any_call(tags.PEER_PORT, 12345)
            span.set_tag.assert_any_call(tags.PEER_SERVICE, 'test_middleware')
def test_middleware(with_peer_tags, with_context):
    """
    Tests http_server.before_request call

    :param with_peer_tags: whether Request object exposes peer properties
    :param with_context: whether the inbound request contains tracing context
    :return:
    """
    request = mock.MagicMock()
    request.full_url = 'http://localhost:12345/test'
    request.operation = 'my-test'
    if with_peer_tags:
        request.remote_ip = 'localhost'
        request.remote_port = 12345
        request.caller_name = 'test_middleware'
    else:
        request.remote_ip = None
        request.remote_port = None
        request.caller_name = None

    tracer = opentracing.tracer
    if with_context:
        span_ctx = mock.MagicMock()
    else:
        span_ctx = None
    p_extract = mock.patch.object(tracer, 'extract', return_value=span_ctx)
    span = mock.MagicMock()
    p_start_span = mock.patch.object(tracer, 'start_span', return_value=span)
    with p_extract as extract_call, p_start_span as start_span_call:
        span2 = http_server.before_request(request=request, tracer=tracer)
        assert span == span2
        extract_call.assert_called_with(
            format=Format.HTTP_HEADERS, carrier={})
        expected_tags = {
            'http.url': 'http://localhost:12345/test',
            'span.kind': 'server'
        }
        if with_peer_tags:
            expected_tags.update({
                'peer.service': 'test_middleware',
                'span.kind': 'server',
                'peer.ipv4': 'localhost',
                'peer.port': 12345,
            })
        start_span_call.assert_called_with(
            operation_name='my-test',
            tags=expected_tags,
            child_of=span_ctx
        )
Example #5
0
def test_middleware(with_peer_tags, with_context):
    """
    Tests http_server.before_request call

    :param with_peer_tags: whether Request object exposes peer properties
    :param with_context: whether the inbound request contains tracing context
    :return:
    """
    request = mock.MagicMock()
    request.method = 'GET'
    request.full_url = 'http://localhost:12345/test'
    request.operation = 'my-test'
    if with_peer_tags:
        request.remote_ip = 'localhost'
        request.remote_port = 12345
        request.caller_name = 'test_middleware'
    else:
        request.remote_ip = None
        request.remote_port = None
        request.caller_name = None

    tracer = opentracing.tracer
    if with_context:
        span_ctx = mock.MagicMock()
    else:
        span_ctx = None
    p_extract = mock.patch.object(tracer, 'extract', return_value=span_ctx)
    span = mock.MagicMock()
    p_start_span = mock.patch.object(tracer, 'start_span', return_value=span)
    with p_extract as extract_call, p_start_span as start_span_call:
        span2 = http_server.before_request(request=request, tracer=tracer)
        assert span == span2
        extract_call.assert_called_with(format=Format.HTTP_HEADERS, carrier={})
        expected_tags = {
            'http.method': 'GET',
            'http.url': 'http://localhost:12345/test',
            'span.kind': 'server',
        }
        if with_peer_tags:
            expected_tags.update({
                'peer.service': 'test_middleware',
                'span.kind': 'server',
                'peer.ipv4': 'localhost',
                'peer.port': 12345,
            })
        start_span_call.assert_called_with(operation_name='my-test',
                                           tags=expected_tags,
                                           child_of=span_ctx)
 def __call__(self, request, handler, next_mw):
     # TODO find out if the route can be read from handler
     self._initializer.initialize_tracer()
     request_wrapper = http_server.TornadoRequestWrapper(request=request)
     span = http_server.before_request(request=request_wrapper)
     span.set_operation_name("{0}: {1}".format(request.method,
                                               handler.__class__.__name__))
     try:
         with request_context.span_in_stack_context(span=span):
             next_mw_future = next_mw()  # cannot yield inside StackContext
         yield next_mw_future
     except Exception as e:
         span.set_tag('error', True)
         span.log_event(event='error', payload=e)
         raise
     finally:
         span.finish()
Example #7
0
    def handle_trace_request(self, http_request, trace_request, span_handler, response_writer):

        span = http_server.before_request(http_server.TornadoRequestWrapper(request=http_request),
                                          self.tracer)
        if span_handler:
            span_handler(span)

        traceId = "%x" % span.trace_id
        observed_span = ObservedSpan(traceId, span.is_sampled(),
                                     span.get_baggage_item(constants.baggage_key))

        tr = TraceResponse(span=observed_span)

        if trace_request and trace_request.downstream is not None:
            self.call_downstream(span, trace_request, tr, response_writer)
        else:
            response_writer.write(serializer.obj_to_json(tr))
            response_writer.finish()
Example #8
0
    def wsgi_tracing_middleware(environ, start_response):
        if environ['PATH_INFO'] == '/metrics':
            return prometheus_app(environ, start_response)

        # TODO find out if the route can be retrieved from somewhere
        request = WSGIRequestWrapper.from_wsgi_environ(environ)
        span = before_request(request=request, tracer=tracer)
        nm = '%s %s %s' % (environ['wsgi.url_scheme'].upper(),
                           request.operation.upper(), environ['PATH_INFO'])
        nm = _NORM_RE.sub('-', nm)
        span.set_operation_name(nm)

        # Wrapper around the real start_response object to log
        # additional information to opentracing Span
        def start_response_wrapper(status, response_headers, exc_info=None):
            span.set_tag('error', exc_info is not None)
            span.set_tag('http.status_code', status[:3])
            span.finish()

            return start_response(status, response_headers)

        with opentracing_instrumentation.span_in_context(span):
            return other_wsgi(environ, start_response_wrapper)
Example #9
0
 def get(self):
     request = TornadoRequestWrapper(self.request)
     with before_request(request, tracer=opentracing.tracer) as span:
         self.write('{:x}'.format(span.context.trace_id))
         self.set_status(200)
 def get(self):
     request = TornadoRequestWrapper(self.request)
     with before_request(request, tracer=opentracing.tracer) as span:
         self.write('{:x}'.format(span.context.trace_id))
         self.set_status(200)