Skip to content

Commit

Permalink
[EventHubs] add logging.info to warn the usage of partition key of no…
Browse files Browse the repository at this point in the history
…n-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
  • Loading branch information
yunhaoling authored Mar 4, 2021
1 parent 419f659 commit bb46884
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
9 changes: 9 additions & 0 deletions sdk/eventhub/azure-eventhub/azure/eventhub/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions sdk/eventhub/azure-eventhub/azure/eventhub/_producer_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<azure.eventhub.exceptions.AuthenticationError>`
:class:`ConnectError<azure.eventhub.exceptions.ConnectError>`
Expand All @@ -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 "
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<azure.eventhub.exceptions.AuthenticationError>`
:class:`ConnectError<azure.eventhub.exceptions.ConnectError>`
Expand All @@ -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 "
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bb46884

Please sign in to comment.