diff --git a/ldclient/impl/events/event_processor_common.py b/ldclient/impl/events/event_processor_common.py index 75f634b1..29f7e703 100644 --- a/ldclient/impl/events/event_processor_common.py +++ b/ldclient/impl/events/event_processor_common.py @@ -5,6 +5,7 @@ and async (async_event_processor.py) implementations. """ +import json from calendar import timegm from collections import namedtuple from email.utils import parsedate @@ -97,7 +98,11 @@ def __init__(self, config: PrivateAttributesConfig): ) def make_output_events(self, events: List[Any], summary: EventSummary): - events_out = [self.make_output_event(e) for e in events] + events_out = [] + for e in events: + event_out = self.make_output_event(e) + if event_out is not None: + events_out.append(event_out) if not summary.is_empty(): events_out.append(self.make_summary_event(summary)) return events_out @@ -131,6 +136,11 @@ def make_output_event(self, e: Any): 'context': self._process_context(e.context, True), } if e.data is not None: + try: + json.dumps(e.data, allow_nan=False) + except (TypeError, ValueError): + log.warning('Custom event "%s" has invalid JSON data and will not be sent', e.key) + return None out['data'] = e.data if e.metric_value is not None: out['metricValue'] = e.metric_value diff --git a/ldclient/testing/impl/events/test_async_event_processor.py b/ldclient/testing/impl/events/test_async_event_processor.py index f6d67632..2b676209 100644 --- a/ldclient/testing/impl/events/test_async_event_processor.py +++ b/ldclient/testing/impl/events/test_async_event_processor.py @@ -153,6 +153,20 @@ async def test_individual_feature_event_is_queued_with_index_event(): assert output[2]['kind'] == 'summary' +@pytest.mark.parametrize("value", [float('nan'), float('inf'), float('-inf')]) +async def test_non_finite_custom_data_does_not_invalidate_batch(value): + mock_http = MockAioHttp() + async with make_processor(mock_http) as ep: + ep.send_event(EventInputCustom(timestamp, context, 'invalid', {'nested': [value]}, None)) + ep.send_event(EventInputCustom(timestamp, context, 'valid', {'nested': [1.5]}, None)) + + output = await flush_and_get_events(ep, mock_http) + json.dumps(output, allow_nan=False) + custom_events = [event for event in output if event['kind'] == 'custom'] + assert [event['key'] for event in custom_events] == ['valid'] + assert custom_events[0]['data'] == {'nested': [1.5]} + + async def test_custom_event_is_queued(): mock_http = MockAioHttp() async with make_processor(mock_http) as ep: diff --git a/ldclient/testing/impl/events/test_event_processor.py b/ldclient/testing/impl/events/test_event_processor.py index 2c2f462b..cafb0f01 100644 --- a/ldclient/testing/impl/events/test_event_processor.py +++ b/ldclient/testing/impl/events/test_event_processor.py @@ -539,6 +539,19 @@ def test_nontracked_events_are_summarized(): } +@pytest.mark.parametrize("value", [float('nan'), float('inf'), float('-inf')]) +def test_non_finite_custom_data_does_not_invalidate_batch(value): + with DefaultTestProcessor() as ep: + ep.send_event(EventInputCustom(timestamp, context, 'invalid', {'nested': [value]}, None)) + ep.send_event(EventInputCustom(timestamp, context, 'valid', {'nested': [1.5]}, None)) + + output = flush_and_get_events(ep) + json.dumps(output, allow_nan=False) + custom_events = [event for event in output if event['kind'] == 'custom'] + assert [event['key'] for event in custom_events] == ['valid'] + assert custom_events[0]['data'] == {'nested': [1.5]} + + def test_custom_event_is_queued_with_user(): with DefaultTestProcessor() as ep: e = EventInputCustom(timestamp, context, 'eventkey', {'thing': 'stuff '}, 1.5)