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
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
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
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
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