def send_partial_transaction()

in elasticapm/contrib/serverless/aws.py [0:0]


    def send_partial_transaction(self) -> None:
        """
        We synchronously send the (partial) transaction to the Lambda Extension
        so that the transaction can be reported even if the lambda runtime times
        out before we can report the transaction.

        This is pretty specific to the HTTP transport. If we ever add other
        transports, we will need to clean this up.
        """
        global REGISTER_PARTIAL_TRANSACTIONS
        if (
            REGISTER_PARTIAL_TRANSACTIONS
            and os.environ.get("ELASTIC_APM_LAMBDA_APM_SERVER")
            and ("localhost" in self.client.config.server_url or "127.0.0.1" in self.client.config.server_url)
        ):
            transaction = execution_context.get_transaction()
            transport = self.client._transport
            logger.debug("Sending partial transaction and early metadata to the lambda extension...")
            data = transport._json_serializer({"metadata": self.client.build_metadata()}) + "\n"
            data += transport._json_serializer({"transaction": transaction.to_dict()})
            partial_transaction_url = urllib.parse.urljoin(
                self.client.config.server_url
                if self.client.config.server_url.endswith("/")
                else self.client.config.server_url + "/",
                "register/transaction",
            )
            try:
                transaction.pause_sampling = True
                transport.send(
                    data,
                    custom_url=partial_transaction_url,
                    custom_headers={
                        "x-elastic-aws-request-id": self.context.aws_request_id,
                        "Content-Type": "application/vnd.elastic.apm.transaction+ndjson",
                    },
                )
            except Exception as e:
                if re.match(r"HTTP [4,5]\d\d", str(e)):
                    REGISTER_PARTIAL_TRANSACTIONS = False
                    logger.info(
                        "APM Lambda Extension does not support partial transactions. "
                        "Disabling partial transaction registration."
                    )
                else:
                    logger.warning("Failed to send partial transaction to APM Lambda Extension", exc_info=True)
            finally:
                transaction.pause_sampling = False