Skip to content
Open
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
6 changes: 5 additions & 1 deletion ldclient/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import asyncio
import math
import traceback
from typing import Any, Callable, Dict, List, Optional, Tuple
from uuid import uuid4
Expand Down Expand Up @@ -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))

Expand Down
6 changes: 5 additions & 1 deletion ldclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down
22 changes: 22 additions & 0 deletions ldclient/testing/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()."""
Expand Down
11 changes: 11 additions & 0 deletions ldclient/testing/test_ldclient_events.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from ldclient.client import Config, Context, LDClient
from ldclient.evaluation import EvaluationDetail
from ldclient.feature_store import InMemoryFeatureStore
Expand Down Expand Up @@ -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(''))
Expand Down
Loading