Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion ldclient/impl/events/event_processor_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions ldclient/testing/impl/events/test_async_event_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions ldclient/testing/impl/events/test_event_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading