def _on_finish(func, handler, args, kwargs): tracer = getattr(handler.request, TRACER, None) if not tracer: return func(*args, **kwargs) delattr(handler.request, TRACER) tracer.add_attribute_to_current_span(attribute_key=HTTP_STATUS_CODE, attribute_value=str( handler.get_status())) tracer.finish() execution_context.clean() return func(*args, **kwargs)
def test_clean_span(self): mock_span = mock.Mock() some_value = mock.Mock() execution_context.set_current_span(mock_span) thread_local = threading.local() setattr(thread_local, 'random_non_oc_attr', some_value) execution_context.clean() self.assertNotEqual(mock_span, execution_context.get_current_span()) self.assertEqual(some_value, getattr(thread_local, 'random_non_oc_attr'))
def __call__(self, *args, **kwargs): kwds = kwargs.pop("kwds") span_context_binary = kwargs.pop("span_context_binary") propagator = binary_format.BinaryFormatPropagator() kwargs["span_context"] = propagator.from_header(span_context_binary) _tracer = tracer.Tracer(**kwargs) execution_context.set_opencensus_tracer(_tracer) with _tracer.span(name=threading.current_thread().name): result = self.func(*args, **kwds) execution_context.clean() return result
def test_clean_tracer(self): mock_tracer = mock.Mock() some_value = mock.Mock() execution_context.set_opencensus_tracer(mock_tracer) thread_local = threading.local() setattr(thread_local, 'random_non_oc_attr', some_value) execution_context.clean() self.assertNotEqual(mock_tracer, execution_context.get_opencensus_tracer()) self.assertEqual(some_value, getattr(thread_local, 'random_non_oc_attr'))
def _log_exception(func, handler, args, kwargs): value = args[1] if len(args) == 3 else None if value is None: return func(*args, **kwargs) tracer = getattr(handler.request, TRACER, None) if not tracer: return func(*args, **kwargs) delattr(handler.request, TRACER) if not isinstance(value, HTTPError) or 500 <= value.status_code <= 599: tracer.add_attribute_to_current_span(attribute_key=HTTP_STATUS_CODE, attribute_value=str( handler.get_status())) tracer.finish() execution_context.clean() return func(*args, **kwargs)