def generic_rpc_response_handler(self, response_headers: dict, response_body, span: Span) -> Span: '''Add extended response rpc data to span''' logger.debug( 'Entering BaseInstrumentationWrapper.genericRpcResponseHandler().') try: # is the span currently recording? if not span.is_recording(): return span logger.debug('Span is Recording!') lowercased_headers = self.lowercase_headers(response_headers) # Log rpc metadata if requested? if self._process_response_headers: logger.debug('Dumping Response Headers:') self.add_headers_to_span(self.RPC_RESPONSE_METADATA_PREFIX, span, lowercased_headers) # Log rpc body if requested if self._process_response_body: response_body_str = str(response_body) logger.debug('Processing response body') response_body_str = self.grab_first_n_bytes(response_body_str) span.set_attribute(self.RPC_RESPONSE_BODY_PREFIX, response_body_str) except: # pylint: disable=W0702 logger.debug( 'An error occurred in genericResponseHandler: exception=%s, stacktrace=%s', sys.exc_info()[0], traceback.format_exc()) # Not rethrowing to avoid causing runtime errors finally: return span # pylint: disable=W0150
def _generic_handler( self, record_headers: bool, header_prefix: str, # pylint:disable=R0913 record_body: bool, body_prefix: str, span: Span, headers: dict, body): logger.debug('Entering BaseInstrumentationWrapper.generic_handler().') try: # pylint: disable=R1702 if not span.is_recording(): return span logger.debug('Span is Recording!') lowercased_headers = self.lowercase_headers(headers) if record_headers: self.add_headers_to_span(header_prefix, span, lowercased_headers) if record_body: content_type = self.eligible_based_on_content_type( lowercased_headers) if content_type is False: return span body_str = None if isinstance(body, bytes): body_str = body.decode('UTF8', 'backslashreplace') else: body_str = body request_body_str = self.grab_first_n_bytes(body_str) span.set_attribute(body_prefix, request_body_str) except: # pylint: disable=W0702 logger.debug( 'An error occurred in genericRequestHandler: exception=%s, stacktrace=%s', sys.exc_info()[0], traceback.format_exc()) finally: return span # pylint: disable=W0150
def _apply_response_attributes(span: Span, result): if result is None or not span.is_recording(): return metadata = result.get("ResponseMetadata") if metadata is None: return request_id = metadata.get("RequestId") if request_id is None: headers = metadata.get("HTTPHeaders") if headers is not None: request_id = (headers.get("x-amzn-RequestId") or headers.get("x-amz-request-id") or headers.get("x-amz-id-2")) if request_id: # TODO: update when semantic conventions exist span.set_attribute("aws.request_id", request_id) retry_attempts = metadata.get("RetryAttempts") if retry_attempts is not None: # TODO: update when semantic conventinos exists span.set_attribute("retry_attempts", retry_attempts) status_code = metadata.get("HTTPStatusCode") if status_code is not None: span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code)
def _enrich_span( span: Span, channel: Channel, properties: BasicProperties, task_destination: str, operation: Optional[MessagingOperationValues] = None, ) -> None: span.set_attribute(SpanAttributes.MESSAGING_SYSTEM, "rabbitmq") if operation: span.set_attribute(SpanAttributes.MESSAGING_OPERATION, operation.value) else: span.set_attribute(SpanAttributes.MESSAGING_TEMP_DESTINATION, True) span.set_attribute(SpanAttributes.MESSAGING_DESTINATION, task_destination) if properties.message_id: span.set_attribute(SpanAttributes.MESSAGING_MESSAGE_ID, properties.message_id) if properties.correlation_id: span.set_attribute(SpanAttributes.MESSAGING_CONVERSATION_ID, properties.correlation_id) if not hasattr(channel.connection, "params"): span.set_attribute(SpanAttributes.NET_PEER_NAME, channel.connection._impl.params.host) span.set_attribute(SpanAttributes.NET_PEER_PORT, channel.connection._impl.params.port) else: span.set_attribute(SpanAttributes.NET_PEER_NAME, channel.connection.params.host) span.set_attribute(SpanAttributes.NET_PEER_PORT, channel.connection.params.port)
def _apply_status_code(span: Span, status_code: int) -> None: if not span.is_recording(): return span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code) span.set_status(Status(http_status_to_status_code(status_code)))
def add_headers_to_span(self, prefix: str, span: Span, headers: dict): # pylint:disable=R0201 '''set header attributes on the span''' for header_key, header_value in headers.items(): span.set_attribute(f"{prefix}{header_key}", header_value)
def _before_exec(span: Span, fn: callable): span.set_attribute("user_cookie_id", soda_telemetry.user_cookie_id)