Skip to content

Commit 19fc89c

Browse files
committed
fix remaining tests
1 parent a0a443d commit 19fc89c

File tree

15 files changed

+74
-67
lines changed

15 files changed

+74
-67
lines changed

splitio/api/impressions.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
from splitio.api import APIException, headers_from_metadata
99
from splitio.api.client import HttpClientException
10+
from splitio.engine.impressions import ImpressionsMode
1011

1112

1213
class ImpressionsAPI(object): # pylint: disable=too-few-public-methods
1314
"""Class that uses an httpClient to communicate with the impressions API."""
1415

15-
def __init__(self, client, apikey, sdk_metadata):
16+
def __init__(self, client, apikey, sdk_metadata, mode=ImpressionsMode.OPTIMIZED):
1617
"""
1718
Class constructor.
1819
@@ -25,6 +26,7 @@ def __init__(self, client, apikey, sdk_metadata):
2526
self._client = client
2627
self._apikey = apikey
2728
self._metadata = headers_from_metadata(sdk_metadata)
29+
self._metadata['SplitSDKImpressionsMode'] = mode.name
2830

2931
@staticmethod
3032
def _build_bulk(impressions):
@@ -47,7 +49,8 @@ def _build_bulk(impressions):
4749
'm': impression.time,
4850
'c': impression.change_number,
4951
'r': impression.label,
50-
'b': impression.bucketing_key
52+
'b': impression.bucketing_key,
53+
'pt': impression.previous_time
5154
}
5255
for impression in imps
5356
]
@@ -74,7 +77,7 @@ def _build_counters(counters):
7477
{
7578
'f': pf_count.feature,
7679
'm': pf_count.timeframe,
77-
'c': pf_count.count
80+
'rc': pf_count.count
7881
} for pf_count in counters
7982
]
8083
}

splitio/client/client.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from splitio.models.events import Event, EventWrapper
1212
from splitio.models.telemetry import get_latency_bucket_index
1313
from splitio.client import input_validator
14+
from splitio.util import utctime_ms
1415

1516

1617
class Client(object): # pylint: disable=too-many-instance-attributes
@@ -115,7 +116,7 @@ def _make_evaluation(self, key, feature, attributes, method_name, metric_name):
115116
result['impression']['label'],
116117
result['impression']['change_number'],
117118
bucketing_key,
118-
start
119+
utctime_ms(),
119120
)
120121

121122
self._record_stats([(impression, attributes)], start, metric_name)
@@ -131,7 +132,7 @@ def _make_evaluation(self, key, feature, attributes, method_name, metric_name):
131132
Label.EXCEPTION,
132133
self._split_storage.get_change_number(),
133134
bucketing_key,
134-
start
135+
utctime_ms(),
135136
)
136137
self._record_stats([(impression, attributes)], start, metric_name)
137138
except Exception: # pylint: disable=broad-except
@@ -178,7 +179,7 @@ def _make_evaluations(self, key, features, attributes, method_name, metric_name)
178179
result['impression']['label'],
179180
result['impression']['change_number'],
180181
bucketing_key,
181-
start)
182+
utctime_ms())
182183

183184
bulk_impressions.append(impression)
184185
treatments[feature] = (result['treatment'], result['configurations'])
@@ -389,7 +390,7 @@ def track(self, key, traffic_type, event_type, value=None, properties=None):
389390
traffic_type_name=traffic_type,
390391
event_type_id=event_type,
391392
value=value,
392-
timestamp=int(time.time()*1000),
393+
timestamp=utctime_ms(),
393394
properties=properties,
394395
)
395396
return self._events_storage.put([EventWrapper(

splitio/client/factory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def __init__( # pylint: disable=too-many-arguments
7474
apikey,
7575
storages,
7676
labels_enabled,
77-
impressions_manager=None,
77+
impressions_manager,
7878
apis=None,
7979
tasks=None,
8080
sdk_ready_flag=None,
@@ -139,7 +139,7 @@ def client(self):
139139
This client is only a set of references to structures hold by the factory.
140140
Creating one a fast operation and safe to be used anywhere.
141141
"""
142-
return Client(self, self._labels_enabled, self._impressions_manager)
142+
return Client(self, self._impressions_manager, self._labels_enabled)
143143

144144
def manager(self):
145145
"""
@@ -249,7 +249,7 @@ def _build_in_memory_factory(api_key, cfg, sdk_url=None, events_url=None): # py
249249
apis = {
250250
'splits': SplitsAPI(http_client, api_key),
251251
'segments': SegmentsAPI(http_client, api_key),
252-
'impressions': ImpressionsAPI(http_client, api_key, sdk_metadata),
252+
'impressions': ImpressionsAPI(http_client, api_key, sdk_metadata, cfg['impressionsMode']),
253253
'events': EventsAPI(http_client, api_key, sdk_metadata),
254254
'telemetry': TelemetryAPI(http_client, api_key, sdk_metadata)
255255
}

splitio/engine/impressions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ def _stringify(self, impression):
6565
:returns: a string representation of the impression
6666
:rtype: str
6767
"""
68-
return self._PATTERN % (impression.matching_key,
69-
impression.feature_name,
70-
impression.treatment,
71-
impression.label,
72-
impression.change_number)
68+
return self._PATTERN % (impression.matching_key if impression.matching_key else 'UNKNOWN',
69+
impression.feature_name if impression.feature_name else 'UNKNOWN',
70+
impression.treatment if impression.treatment else 'UNKNOWN',
71+
impression.label if impression.label else 'UNKNOWN',
72+
impression.change_number if impression.change_number else 0)
7373

7474
def process(self, impression):
7575
"""

splitio/tasks/events_sync.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,9 @@ def _send_events(self):
7070

7171
try:
7272
self._events_api.flush_events(to_send)
73-
except APIException as exc:
74-
self._logger.error(
75-
'Exception raised while reporting events: %s -- %d',
76-
exc.message,
77-
exc.status_code
78-
)
73+
except APIException:
74+
self._logger.error('Exception raised while reporting events')
75+
self._logger.debug('Exception information: ', exc_info=True)
7976
self._add_to_failed_queue(to_send)
8077

8178
def start(self):

splitio/tasks/impressions_sync.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,9 @@ def _send_impressions(self):
7171

7272
try:
7373
self._impressions_api.flush_impressions(to_send)
74-
except APIException as exc:
75-
self._logger.error(
76-
'Exception raised while reporting impressions: %s -- %d',
77-
exc.message,
78-
exc.status_code
79-
)
74+
except APIException:
75+
self._logger.error('Exception raised while reporting impressions')
76+
self._logger.debug('Exception information: ', exc_info=True)
8077
self._add_to_failed_queue(to_send)
8178

8279
def start(self):
@@ -104,7 +101,7 @@ def flush(self):
104101
class ImpressionsCountSyncTask(BaseSynchronizationTask):
105102
"""Impressions synchronization task uses an asynctask.AsyncTask to send impressions."""
106103

107-
_PERIOD = 30 * 60 # 30 minutes
104+
_PERIOD = 5 # 30 * 60 # 30 minutes
108105

109106
def __init__(self, impressions_api, impressions_manager):
110107
"""
@@ -124,14 +121,14 @@ def __init__(self, impressions_api, impressions_manager):
124121
def _send_counters(self):
125122
"""Send impressions from both the failed and new queues."""
126123
to_send = self._impressions_manager.get_counts()
124+
if not to_send:
125+
return
126+
127127
try:
128128
self._impressions_api.flush_counters(to_send)
129-
except APIException as exc:
130-
self._logger.error(
131-
'Exception raised while reporting impressions: %s -- %d',
132-
exc.message,
133-
exc.status_code
134-
)
129+
except APIException:
130+
self._logger.error('Exception raised while reporting impression counts')
131+
self._logger.debug('Exception information: ', exc_info=True)
135132

136133
def start(self):
137134
"""Start executing the impressions synchronization task."""

splitio/tasks/segment_sync.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def _update_segment(self, segment_name):
4646
try:
4747
segment_changes = self._segment_api.fetch_segment(segment_name, since)
4848
except APIException:
49-
self._logger.error('Error fetching segments')
49+
self._logger.error('Exception raised while fetching segment %s', segment_name)
50+
self._logger.debug('Exception information: ', exc_info=True)
5051
return False
5152

5253
if since == -1: # first time fetching the segment

splitio/tasks/split_sync.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def _update_splits(self):
4242
try:
4343
split_changes = self._api.fetch_splits(till)
4444
except APIException:
45-
self._logger.error('Failed to fetch split from servers')
45+
self._logger.error('Exception raised while fetching splits')
46+
self._logger.debug('Exception information: ', exc_info=True)
4647
return False
4748

4849
for split in split_changes.get('splits', []):

splitio/tasks/telemetry_sync.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,23 @@ def _flush_telemetry(self):
3737
self._api.flush_latencies(latencies)
3838
except APIException:
3939
self._logger.error('Failed send telemetry/latencies to split BE.')
40+
self._logger.debug('Exception information: ', exc_info=True)
4041

4142
try:
4243
counters = self._storage.pop_counters()
4344
if counters:
4445
self._api.flush_counters(counters)
4546
except APIException:
4647
self._logger.error('Failed send telemetry/counters to split BE.')
48+
self._logger.debug('Exception information: ', exc_info=True)
4749

4850
try:
4951
gauges = self._storage.pop_gauges()
5052
if gauges:
5153
self._api.flush_gauges(gauges)
5254
except APIException:
5355
self._logger.error('Failed send telemetry/gauges to split BE.')
56+
self._logger.debug('Exception information: ', exc_info=True)
5457

5558
def start(self):
5659
"""Start the task."""

splitio/tasks/util/asynctask.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,13 @@ def _execution_wrapper(self):
102102
except queue.Empty:
103103
# If no message was received, the timeout has expired
104104
# and we're ready for a new execution
105-
if not _safe_run(self._main):
106-
_LOGGER.error(
107-
"An error occurred when executing the task. "
108-
"Retrying after perio expires"
109-
)
105+
pass
106+
107+
if not _safe_run(self._main):
108+
_LOGGER.error(
109+
"An error occurred when executing the task. "
110+
"Retrying after perio expires"
111+
)
110112
finally:
111113
self._cleanup()
112114

0 commit comments

Comments
 (0)