def modify_span_sqs_pre()

in elasticapm/instrumentation/packages/botocore.py [0:0]


def modify_span_sqs_pre(span, args, kwargs) -> None:
    operation_name = kwargs.get("operation_name", args[0])
    if span.id:
        trace_parent = span.transaction.trace_parent.copy_from(span_id=span.id)
    else:
        # this is a dropped span, use transaction id instead
        transaction = execution_context.get_transaction()
        trace_parent = transaction.trace_parent.copy_from(span_id=transaction.id)
    attributes = {constants.TRACEPARENT_HEADER_NAME: {"DataType": "String", "StringValue": trace_parent.to_string()}}
    if trace_parent.tracestate:
        attributes[constants.TRACESTATE_HEADER_NAME] = {"DataType": "String", "StringValue": trace_parent.tracestate}
    if len(args) > 1:
        if operation_name in ("SendMessage", "SendMessageBatch"):
            attributes_count = len(attributes)
            if operation_name == "SendMessage":
                messages = [args[1]]
            else:
                messages = args[1]["Entries"]
            for message in messages:
                message["MessageAttributes"] = message.get("MessageAttributes") or {}
                if len(message["MessageAttributes"]) + attributes_count <= SQS_MAX_ATTRIBUTES:
                    message["MessageAttributes"].update(attributes)
                else:
                    logger.info("Not adding disttracing headers to message due to attribute limit reached")
        elif operation_name == "ReceiveMessage":
            message_attributes = args[1].setdefault("MessageAttributeNames", [])
            if "All" not in message_attributes:
                message_attributes.extend([constants.TRACEPARENT_HEADER_NAME, constants.TRACESTATE_HEADER_NAME])