def test_get_binary_annotations():
    def set_extra_binary_annotations(req, resp):
        return {'k': 'v'}
    registry = mock.Mock(
        settings={
            'zipkin.set_extra_binary_annotations': set_extra_binary_annotations
        })
    request = mock.Mock(path='/path', path_qs='/path?time=now')
    request.registry = registry
    response = mock.Mock()

    annotations = logging_helper.get_binary_annotations(request, response)
    expected = {
        'http.uri': '/path',
        'http.uri.qs': '/path?time=now',
        'k': 'v',
        'response_status_code': str(response.status_code),
    }
    assert annotations == expected

    # Try it again with no callback specified
    request.registry.settings = {}
    del expected['k']
    annotations = logging_helper.get_binary_annotations(request, response)
    assert annotations == expected
Exemple #2
0
    def tween(request):
        # Creates zipkin attributes, attaches a zipkin_trace_id attr to the
        # request, and pushes the attrs onto threadlocal stack.
        zipkin_attrs = create_zipkin_attr(request)
        push_zipkin_attrs(zipkin_attrs)

        try:
            # If this request isn't sampled, don't go through the work
            # of initializing the rest of the zipkin attributes
            if not zipkin_attrs.is_sampled:
                return handler(request)

            # If the request IS sampled, we create thrift objects and
            # enter zipkin logging context
            thrift_endpoint = create_endpoint(request)
            log_handler = ZipkinLoggerHandler(zipkin_attrs)
            with ZipkinLoggingContext(zipkin_attrs, thrift_endpoint,
                                      log_handler, request) as context:
                response = handler(request)
                context.response_status_code = response.status_code
                context.binary_annotations_dict = get_binary_annotations(
                        request, response)

                return response
        finally:
            # Regardless of what happens in the request we want to pop attrs
            pop_zipkin_attrs()
def test_get_binary_annotations():
    def set_extra_binary_annotations(req, resp):
        return {'k': 'v'}
    registry = mock.Mock(
        settings={
            'zipkin.set_extra_binary_annotations': set_extra_binary_annotations
        })
    request = mock.Mock(path='/path', path_qs='/path?time=now')
    request.registry = registry
    response = mock.Mock()

    annotations = logging_helper.get_binary_annotations(request, response)
    expected = {'http.uri': '/path', 'http.uri.qs': '/path?time=now', 'k': 'v'}
    assert annotations == expected

    # Try it again with no callback specified
    request.registry.settings = {}
    del expected['k']
    annotations = logging_helper.get_binary_annotations(request, response)
    assert annotations == expected
Exemple #4
0
    def tween(request):
        zipkin_attrs = create_zipkin_attr(request)
        endpoint_attrs = create_endpoint(request)
        log_handler = ZipkinLoggerHandler(zipkin_attrs)
        with ZipkinLoggingContext(zipkin_attrs, endpoint_attrs, log_handler,
                                  request) as context:
            response = handler(request)
            context.response_status_code = response.status_code
            context.binary_annotations_dict = get_binary_annotations(
                    request, response)

            return response
Exemple #5
0
    def tween(request):
        zipkin_attrs = create_zipkin_attr(request)
        endpoint_attrs = create_endpoint(request)
        log_handler = ZipkinLoggerHandler(zipkin_attrs)
        with ZipkinLoggingContext(zipkin_attrs, endpoint_attrs, log_handler,
                                  request) as context:
            response = handler(request)
            context.response_status_code = response.status_code
            context.binary_annotations_dict = get_binary_annotations(
                request, response)

            return response
Exemple #6
0
    def tween(request):
        zipkin_attrs = create_zipkin_attr(request)

        # If this request isn't sampled, don't go through the work
        # of initializing the rest of the zipkin attributes
        if not zipkin_attrs.is_sampled:
            return handler(request)

        # If the request IS sampled, we create thrift objects, store
        # thread-local variables, etc, to enter zipkin logging context
        thrift_endpoint = create_endpoint(request)
        log_handler = ZipkinLoggerHandler(zipkin_attrs)
        with ZipkinLoggingContext(zipkin_attrs, thrift_endpoint, log_handler, request) as context:
            response = handler(request)
            context.response_status_code = response.status_code
            context.binary_annotations_dict = get_binary_annotations(request, response)

            return response
Exemple #7
0
    def tween(request):
        zipkin_attrs = create_zipkin_attr(request)

        # If this request isn't sampled, don't go through the work
        # of initializing the rest of the zipkin attributes
        if not zipkin_attrs.is_sampled:
            return handler(request)

        # If the request IS sampled, we create thrift objects, store
        # thread-local variables, etc, to enter zipkin logging context
        thrift_endpoint = create_endpoint(request)
        log_handler = ZipkinLoggerHandler(zipkin_attrs)
        with ZipkinLoggingContext(zipkin_attrs, thrift_endpoint, log_handler,
                                  request) as context:
            response = handler(request)
            context.response_status_code = response.status_code
            context.binary_annotations_dict = get_binary_annotations(
                    request, response)

            return response