diff --git a/ldclient/async_client.py b/ldclient/async_client.py index 9c9f975d..ec7a30f5 100644 --- a/ldclient/async_client.py +++ b/ldclient/async_client.py @@ -3,6 +3,7 @@ """ import asyncio +import math import traceback from typing import Any, Callable, Dict, List, Optional, Tuple from uuid import uuid4 @@ -355,10 +356,13 @@ def track(self, event_name: str, context: Context, data: Optional[Any] = None, m :param context: the evaluation context associated with the event :param data: optional additional data associated with the event :param metric_value: a numeric value used by the LaunchDarkly experimentation feature in - numeric custom metrics; can be omitted if this event is used by only non-numeric metrics + numeric custom metrics; can be omitted if this event is used by only non-numeric metrics. + If it is NaN or infinite, a warning is logged and the event is not recorded """ if not context.valid: log.warning("Invalid context for track (%s)" % context.error) + elif isinstance(metric_value, float) and not math.isfinite(metric_value): + log.warning('track called with non-finite metric value for event "%s"; event was not recorded' % event_name) else: self._send_event(self._event_factory_default.new_custom_event(event_name, context, data, metric_value)) diff --git a/ldclient/client.py b/ldclient/client.py index a9b5a2a9..daf67bf2 100644 --- a/ldclient/client.py +++ b/ldclient/client.py @@ -2,6 +2,7 @@ This submodule contains the client class that provides most of the SDK functionality. """ +import math import os import threading import traceback @@ -352,11 +353,14 @@ def track(self, event_name: str, context: Context, data: Optional[Any] = None, m :param context: the evaluation context associated with the event :param data: optional additional data associated with the event :param metric_value: a numeric value used by the LaunchDarkly experimentation feature in - numeric custom metrics; can be omitted if this event is used by only non-numeric metrics + numeric custom metrics; can be omitted if this event is used by only non-numeric metrics. + If it is NaN or infinite, a warning is logged and the event is not recorded """ self._check_forked() if not context.valid: log.warning("Invalid context for track (%s)" % context.error) + elif isinstance(metric_value, float) and not math.isfinite(metric_value): + log.warning('track called with non-finite metric value for event "%s"; event was not recorded' % event_name) else: self._send_event(self._event_factory_default.new_custom_event(event_name, context, data, metric_value)) diff --git a/ldclient/testing/test_async_client.py b/ldclient/testing/test_async_client.py index 876b6b40..d4d9607f 100644 --- a/ldclient/testing/test_async_client.py +++ b/ldclient/testing/test_async_client.py @@ -277,6 +277,28 @@ async def test_track_sends_event(): assert event.key == 'my-event' +@pytest.mark.asyncio +@pytest.mark.parametrize('metric_value', [float('nan'), float('inf'), float('-inf')]) +async def test_track_non_finite_metric_value(metric_value, caplog): + """track() drops a custom event whose metric value is NaN or infinite.""" + store = MockAsyncFeatureStore() + store._initialized = True + + mock_ep = MockAsyncEventProcessor() + config = AsyncConfig( + "test-sdk-key", + feature_store=store, + update_processor_class=MockAsyncUpdateProcessor, + event_processor_class=lambda _cfg: mock_ep, + send_events=True, + ) + async with AsyncLDClient(config) as client: + client.track('my-event', Context.create('user-1'), metric_value=metric_value) + + assert mock_ep.events == [] + assert 'non-finite metric value for event "my-event"' in caplog.text + + @pytest.mark.asyncio async def test_data_source_status_provider_accessible(): """data_source_status_provider is not None after start().""" diff --git a/ldclient/testing/test_ldclient_events.py b/ldclient/testing/test_ldclient_events.py index 3bc25917..50192bc2 100644 --- a/ldclient/testing/test_ldclient_events.py +++ b/ldclient/testing/test_ldclient_events.py @@ -1,3 +1,5 @@ +import pytest + from ldclient.client import Config, Context, LDClient from ldclient.evaluation import EvaluationDetail from ldclient.feature_store import InMemoryFeatureStore @@ -155,6 +157,15 @@ def test_track_with_metric_value(): assert e.metric_value == 1.5 +@pytest.mark.parametrize('metric_value', [float('nan'), float('inf'), float('-inf')]) +def test_track_non_finite_metric_value(metric_value, caplog): + with make_client() as client: + count_events(client) + client.track('my_event', context, metric_value=metric_value) + assert count_events(client) == 0 + assert 'non-finite metric value for event "my_event"' in caplog.text + + def test_track_invalid_context(): with make_client() as client: client.track('my_event', Context.create(''))