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, ) -> 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 _enrich_span( span: Span, queue_name: str, queue_url: str, conversation_id: Optional[str] = None, operation: Optional[MessagingOperationValues] = None, message_id: Optional[str] = None, ) -> None: if not span.is_recording(): return span.set_attribute(SpanAttributes.MESSAGING_SYSTEM, "aws.sqs") span.set_attribute(SpanAttributes.MESSAGING_DESTINATION, queue_name) span.set_attribute( SpanAttributes.MESSAGING_DESTINATION_KIND, MessagingDestinationKindValues.QUEUE.value, ) span.set_attribute(SpanAttributes.MESSAGING_URL, queue_url) if operation: span.set_attribute(SpanAttributes.MESSAGING_OPERATION, operation.value) if conversation_id: span.set_attribute(SpanAttributes.MESSAGING_CONVERSATION_ID, conversation_id) if message_id: span.set_attribute(SpanAttributes.MESSAGING_MESSAGE_ID, message_id)
def _populate_span( self, span: trace_api.Span, cursor, *args: typing.Tuple[typing.Any, typing.Any] ): if not span.is_recording(): return statement = self.get_statement(cursor, args) span.set_attribute( SpanAttributes.DB_SYSTEM, self._db_api_integration.database_system ) span.set_attribute( SpanAttributes.DB_NAME, self._db_api_integration.database ) span.set_attribute(SpanAttributes.DB_STATEMENT, statement) for ( attribute_key, attribute_value, ) in self._db_api_integration.span_attributes.items(): span.set_attribute(attribute_key, attribute_value) if self._db_api_integration.capture_parameters and len(args) > 1: span.set_attribute("db.statement.parameters", str(args[1]))
def _set_attributes_from_url(span: trace.Span, url): """Set connection tags from the url. return true if successful.""" if span.is_recording(): if url.host: span.set_attribute(_HOST, url.host) if url.port: span.set_attribute(_PORT, url.port) if url.database: span.set_attribute(_DB, url.database) return bool(url.host)
def _set_attributes_from_cursor(span: trace.Span, vendor, cursor): """Attempt to set db connection attributes by introspecting the cursor.""" if not span.is_recording(): return if vendor == "postgresql": # pylint: disable=import-outside-toplevel from psycopg2.extensions import parse_dsn if hasattr(cursor, "connection") and hasattr(cursor.connection, "dsn"): dsn = getattr(cursor.connection, "dsn", None) if dsn: data = parse_dsn(dsn) span.set_attribute(_DB, data.get("dbname")) span.set_attribute(_HOST, data.get("host")) span.set_attribute(_PORT, int(data.get("port")))
def _populate_span(self, span: trace_api.Span, *args: typing.Tuple[typing.Any, typing.Any]): if not span.is_recording(): return statement = args[0] if args else "" span.set_attribute("component", self._db_api_integration.database_component) span.set_attribute("db.type", self._db_api_integration.database_type) span.set_attribute("db.instance", self._db_api_integration.database) span.set_attribute("db.statement", statement) for ( attribute_key, attribute_value, ) in self._db_api_integration.span_attributes.items(): span.set_attribute(attribute_key, attribute_value) if len(args) > 1: span.set_attribute("db.statement.parameters", str(args[1]))
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)))