From bb468841d2249fea93f7cc8883d685ae1d8b81cc Mon Sep 17 00:00:00 2001 From: "Adam Ling (MSFT)" Date: Thu, 4 Mar 2021 11:03:06 -0800 Subject: [PATCH] [EventHubs] add logging.info to warn the usage of partition key of non-string type (#17057) * add a logging to warn the usage of partition key of non-string type * move the logging info into eventdatabatch to avoid duplicate logging when sending list, also updated the wording * remove unused six module --- .../azure-eventhub/azure/eventhub/_common.py | 9 +++++++++ .../azure/eventhub/_producer_client.py | 13 +++++++++---- .../azure/eventhub/aio/_producer_client_async.py | 13 +++++++++---- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/sdk/eventhub/azure-eventhub/azure/eventhub/_common.py b/sdk/eventhub/azure-eventhub/azure/eventhub/_common.py index d15196dd8cf3..7f7f967ac63f 100644 --- a/sdk/eventhub/azure-eventhub/azure/eventhub/_common.py +++ b/sdk/eventhub/azure-eventhub/azure/eventhub/_common.py @@ -350,6 +350,15 @@ class EventDataBatch(object): def __init__(self, max_size_in_bytes=None, partition_id=None, partition_key=None): # type: (Optional[int], Optional[str], Optional[Union[str, bytes]]) -> None + + if partition_key and not isinstance(partition_key, (six.text_type, six.binary_type)): + _LOGGER.info( + "WARNING: Setting partition_key of non-string value on the events to be sent is discouraged " + "as the partition_key will be ignored by the Event Hub service and events will be assigned " + "to all partitions using round-robin. Furthermore, there are SDKs for consuming events which expect " + "partition_key to only be string type, they might fail to parse the non-string value." + ) + self.max_size_in_bytes = max_size_in_bytes or constants.MAX_MESSAGE_LENGTH_BYTES self.message = BatchMessage(data=[], multi_messages=False, properties=None) self._partition_id = partition_id diff --git a/sdk/eventhub/azure-eventhub/azure/eventhub/_producer_client.py b/sdk/eventhub/azure-eventhub/azure/eventhub/_producer_client.py index c3f139337eb9..9ae9dad761e4 100644 --- a/sdk/eventhub/azure-eventhub/azure/eventhub/_producer_client.py +++ b/sdk/eventhub/azure-eventhub/azure/eventhub/_producer_client.py @@ -222,8 +222,10 @@ def send_batch(self, event_data_batch, **kwargs): A `TypeError` will be raised if partition_key is specified and event_data_batch is an `EventDataBatch` because `EventDataBatch` itself has partition_key. If both partition_id and partition_key are provided, the partition_id will take precedence. - **WARNING: Please DO NOT pass a partition_key of non-string type. The Event Hub service ignores partition_key - of non-string type, in which case events will be assigned to all partitions using round-robin.** + **WARNING: Setting partition_key of non-string value on the events to be sent is discouraged + as the partition_key will be ignored by the Event Hub service and events will be assigned + to all partitions using round-robin. Furthermore, there are SDKs for consuming events which expect + partition_key to only be string type, they might fail to parse the non-string value.** :rtype: None :raises: :class:`AuthenticationError` :class:`ConnectError` @@ -246,6 +248,7 @@ def send_batch(self, event_data_batch, **kwargs): """ partition_id = kwargs.get("partition_id") partition_key = kwargs.get("partition_key") + if isinstance(event_data_batch, EventDataBatch): if partition_id or partition_key: raise TypeError("partition_id and partition_key should be None when sending an EventDataBatch " @@ -283,8 +286,10 @@ def create_batch(self, **kwargs): :keyword str partition_key: With the given partition_key, event data will be sent to a particular partition of the Event Hub decided by the service. If both partition_id and partition_key are provided, the partition_id will take precedence. - **WARNING: Please DO NOT pass a partition_key of non-string type. The Event Hub service ignores partition_key - of non-string type, in which case events will be assigned to all partitions using round-robin.** + **WARNING: Setting partition_key of non-string value on the events to be sent is discouraged + as the partition_key will be ignored by the Event Hub service and events will be assigned + to all partitions using round-robin. Furthermore, there are SDKs for consuming events which expect + partition_key to only be string type, they might fail to parse the non-string value.** :keyword int max_size_in_bytes: The maximum size of bytes data that an EventDataBatch object can hold. By default, the value is determined by your Event Hubs tier. :rtype: ~azure.eventhub.EventDataBatch diff --git a/sdk/eventhub/azure-eventhub/azure/eventhub/aio/_producer_client_async.py b/sdk/eventhub/azure-eventhub/azure/eventhub/aio/_producer_client_async.py index 6caa8e9f5144..7035060a75ff 100644 --- a/sdk/eventhub/azure-eventhub/azure/eventhub/aio/_producer_client_async.py +++ b/sdk/eventhub/azure-eventhub/azure/eventhub/aio/_producer_client_async.py @@ -253,8 +253,10 @@ async def send_batch( A `TypeError` will be raised if partition_key is specified and event_data_batch is an `EventDataBatch` because `EventDataBatch` itself has partition_key. If both partition_id and partition_key are provided, the partition_id will take precedence. - **WARNING: Please DO NOT pass a partition_key of non-string type. The Event Hub service ignores partition_key - of non-string type, in which case events will be assigned to all partitions using round-robin.** + **WARNING: Setting partition_key of non-string value on the events to be sent is discouraged + as the partition_key will be ignored by the Event Hub service and events will be assigned + to all partitions using round-robin. Furthermore, there are SDKs for consuming events which expect + partition_key to only be string type, they might fail to parse the non-string value.** :rtype: None :raises: :class:`AuthenticationError` :class:`ConnectError` @@ -277,6 +279,7 @@ async def send_batch( """ partition_id = kwargs.get("partition_id") partition_key = kwargs.get("partition_key") + if isinstance(event_data_batch, EventDataBatch): if partition_id or partition_key: raise TypeError("partition_id and partition_key should be None when sending an EventDataBatch " @@ -318,8 +321,10 @@ async def create_batch( :param str partition_key: With the given partition_key, event data will be sent to a particular partition of the Event Hub decided by the service. If both partition_id and partition_key are provided, the partition_id will take precedence. - **WARNING: Please DO NOT pass a partition_key of non-string type. The Event Hub service ignores partition_key - of non-string type, in which case events will be assigned to all partitions using round-robin.** + **WARNING: Setting partition_key of non-string value on the events to be sent is discouraged + as the partition_key will be ignored by the Event Hub service and events will be assigned + to all partitions using round-robin. Furthermore, there are SDKs for consuming events which expect + partition_key to only be string type, they might fail to parse the non-string value.** :param int max_size_in_bytes: The maximum size of bytes data that an EventDataBatch object can hold. By default, the value is determined by your Event Hubs tier. :rtype: ~azure.eventhub.EventDataBatch