def use_span( self, span: trace_api.Span, end_on_exit: bool = False, ) -> Iterator[trace_api.Span]: try: token = context_api.attach(context_api.set_value(SPAN_KEY, span)) try: yield span finally: context_api.detach(token) except Exception as exc: # pylint: disable=broad-except # Record the exception as an event if isinstance(span, Span) and span.is_recording(): # pylint:disable=protected-access if span._record_exception: span.record_exception(exc) # Records status if use_span is used # i.e. with tracer.start_as_current_span() as span: if (span.status.status_code is StatusCode.UNSET and span._set_status_on_exception): span.set_status( Status( status_code=StatusCode.ERROR, description="{}: {}".format( type(exc).__name__, exc), )) raise finally: if end_on_exit: span.end()
def use_span( self, span: trace_api.Span, end_on_exit: bool = False, record_exception: bool = True, ) -> Iterator[trace_api.Span]: try: token = context_api.attach(context_api.set_value(SPAN_KEY, span)) try: yield span finally: context_api.detach(token) except Exception as error: # pylint: disable=broad-except # pylint:disable=protected-access if isinstance(span, Span): if record_exception: span.record_exception(error) if span.status is None and span._set_status_on_exception: span.set_status( Status( canonical_code=getattr( error, EXCEPTION_STATUS_FIELD, StatusCanonicalCode.UNKNOWN, ), description="{}: {}".format( type(error).__name__, error), )) raise finally: if end_on_exit: span.end()
def use_span( self, span: trace_api.Span, end_on_exit: bool = False, record_exception: bool = True, ) -> Iterator[trace_api.Span]: try: token = context_api.attach(context_api.set_value(SPAN_KEY, span)) try: yield span finally: context_api.detach(token) except Exception as error: # pylint: disable=broad-except # pylint:disable=protected-access if isinstance(span, Span): if record_exception: span.record_exception(error) # Records status if use_span is used # i.e. with tracer.start_as_current_span() as span: if ( span.status.status_code is StatusCode.UNSET and span._set_status_on_exception ): span.set_status( Status( status_code=getattr( error, EXCEPTION_STATUS_FIELD, StatusCode.ERROR, ), description="{}: {}".format( type(error).__name__, error ), ) ) raise finally: if end_on_exit: span.end()