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) -> 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 if (isinstance(span, Span) and span.status is None and span._set_status_on_exception # pylint:disable=protected-access # noqa ): span.set_status( Status( canonical_code=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) 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) -> Iterator[trace_api.Span]: """See `opentelemetry.trace.Tracer.use_span`.""" try: span_snapshot = self.source.get_current_span() self.source._current_span_slot.set( # pylint:disable=protected-access span) try: yield span finally: self.source._current_span_slot.set( # pylint:disable=protected-access span_snapshot) except Exception as error: # pylint: disable=broad-except if (span.status is None and span._set_status_on_exception # pylint:disable=protected-access # noqa ): span.set_status( Status( canonical_code=StatusCanonicalCode.UNKNOWN, description="{}: {}".format( type(error).__name__, error), )) raise finally: if end_on_exit: span.end()
def _apply_response(span: Span, response: urllib3.response.HTTPResponse) -> None: if not span.is_recording(): return span.set_attribute("http.status_code", response.status) span.set_attribute("http.status_text", response.reason) span.set_status(Status(http_status_to_status_code(response.status)))
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()
def _apply_response(span: Span, response: urllib3.response.HTTPResponse): if not span.is_recording(): return span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, response.status) span.set_status(Status(http_status_to_status_code(response.status)))