Skip to content

Commit feac1e4

Browse files
committed
cleanup and fix redis unique keys push
1 parent 8fdef37 commit feac1e4

File tree

6 files changed

+62
-26
lines changed

6 files changed

+62
-26
lines changed

splitio/client/util.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,28 @@
66

77
from splitio.models.telemetry import MethodExceptionsAndLatencies
88

9+
_MAP_METHOD_TO_ENUM = {'treatment': MethodExceptionsAndLatencies.TREATMENT,
10+
'treatments': MethodExceptionsAndLatencies.TREATMENTS,
11+
'treatment_with_config': MethodExceptionsAndLatencies.TREATMENT_WITH_CONFIG,
12+
'treatments_with_config': MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG,
13+
'track': MethodExceptionsAndLatencies.TRACK
14+
}
15+
916
SdkMetadata = namedtuple(
1017
'SdkMetadata',
1118
['sdk_version', 'instance_name', 'instance_ip']
1219
)
1320

1421
def _get_hostname_and_ip(config):
22+
"""
23+
Get current hostname and IP address if config parameters are not set.
24+
25+
:param config: User supplied config augmented with defaults.
26+
:type config: dict
27+
28+
:return: IP address and Hostname
29+
:rtype: Tuple (str, str)
30+
"""
1531
if config.get('IPAddressesEnabled') is False:
1632
return 'NA', 'NA'
1733
ip_from_config = config.get('machineIp')
@@ -35,13 +51,10 @@ def get_metadata(config):
3551
return SdkMetadata(version, hostname, ip_address)
3652

3753
def get_method_constant(method):
38-
if method == 'treatment':
39-
return MethodExceptionsAndLatencies.TREATMENT
40-
elif method == 'treatments':
41-
return MethodExceptionsAndLatencies.TREATMENTS
42-
elif method == 'treatment_with_config':
43-
return MethodExceptionsAndLatencies.TREATMENT_WITH_CONFIG
44-
elif method == 'treatments_with_config':
45-
return MethodExceptionsAndLatencies.TREATMENTS_WITH_CONFIG
46-
elif method == 'track':
47-
return MethodExceptionsAndLatencies.TRACK
54+
"""
55+
Get method name mapped to the Method Enum object
56+
57+
:return: method name
58+
:rtype: str
59+
"""
60+
return _MAP_METHOD_TO_ENUM[method]

splitio/engine/impressions/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@ def set_classes(storage_mode, impressions_mode, api_adapter):
1414
clear_filter_task = None
1515
impressions_count_sync = None
1616
impressions_count_task = None
17+
sender_adapter = None
1718
if storage_mode == 'REDIS':
18-
redis_sender_adapter = RedisSenderAdapter(api_adapter)
19-
api_telemetry_adapter = redis_sender_adapter
20-
api_impressions_adapter = redis_sender_adapter
19+
sender_adapter = RedisSenderAdapter(api_adapter)
20+
api_telemetry_adapter = sender_adapter
21+
api_impressions_adapter = sender_adapter
2122
else:
2223
api_telemetry_adapter = api_adapter['telemetry']
2324
api_impressions_adapter = api_adapter['impressions']
25+
sender_adapter = InMemorySenderAdapter(api_telemetry_adapter)
2426

2527
if impressions_mode == ImpressionsMode.NONE:
2628
imp_counter = ImpressionsCounter()
2729
imp_strategy = StrategyNoneMode(imp_counter)
2830
clear_filter_sync = ClearFilterSynchronizer(imp_strategy.get_unique_keys_tracker())
29-
unique_keys_synchronizer = UniqueKeysSynchronizer(InMemorySenderAdapter(api_telemetry_adapter), imp_strategy.get_unique_keys_tracker())
31+
unique_keys_synchronizer = UniqueKeysSynchronizer(sender_adapter, imp_strategy.get_unique_keys_tracker())
3032
unique_keys_task = UniqueKeysSyncTask(unique_keys_synchronizer.send_all)
3133
clear_filter_task = ClearFilterSyncTask(clear_filter_sync.clear_all)
3234
imp_strategy.get_unique_keys_tracker().set_queue_full_hook(unique_keys_task.flush)

splitio/engine/telemetry.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,12 @@ def pop_latencies(self):
172172
return self._telemetry_storage.pop_latencies()
173173

174174
def pop_formatted_stats(self):
175-
"""Get formatted and reset stats."""
175+
"""
176+
Get formatted and reset stats.
177+
178+
:returns: formatted stats
179+
:rtype: Dict
180+
"""
176181
exceptions = self.pop_exceptions()['methodExceptions']
177182
latencies = self.pop_latencies()['methodLatencies']
178183
return {
@@ -238,7 +243,12 @@ def get_session_length(self):
238243
return self._telemetry_storage.get_session_length()
239244

240245
def pop_formatted_stats(self):
241-
"""Get formatted and reset stats."""
246+
"""
247+
Get formatted and reset stats.
248+
249+
:returns: formatted stats
250+
:rtype: Dict
251+
"""
242252
last_synchronization = self.get_last_synchronization()
243253
http_errors = self.pop_http_errors()['httpErrors']
244254
http_latencies = self.pop_http_latencies()['httpLatencies']

splitio/storage/adapters/redis.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Redis client wrapper with prefix support."""
22
from builtins import str
3-
import logging
43

54
from splitio.version import __version__
65
from splitio.util.host_info import get_ip, get_hostname
@@ -18,10 +17,7 @@ def missing_redis_dependencies(*_, **__):
1817
)
1918
StrictRedis = Sentinel = missing_redis_dependencies
2019

21-
_LOGGER = logging.getLogger(__name__)
2220
TELEMETRY_CONFIG_KEY = 'SPLITIO.telemetry.init'
23-
TELEMETRY_EXCEPTIONS_KEY = 'SPLITIO.telemetry.exceptions'
24-
TELEMETRY_LATENCIES_KEY = 'SPLITIO.telemetry.latencies'
2521

2622
class RedisAdapterException(Exception):
2723
"""Exception to be thrown when a redis command fails with an exception."""
@@ -344,11 +340,11 @@ def rpush(self, key, *values):
344340
def incr(self, name, amount=1):
345341
"""Mimic original redis function but using user custom prefix."""
346342
self._pipe.incr(self._prefix_helper.add_prefix(name), amount)
347-
343+
348344
def hincrby(self, name, key, amount=1):
349345
"""Mimic original redis function but using user custom prefix."""
350346
self._pipe.hincrby(self._prefix_helper.add_prefix(name), key, amount)
351-
347+
352348
def execute(self):
353349
"""Mimic original redis function but using user custom prefix."""
354350
try:

splitio/sync/telemetry.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import json
2-
import logging
3-
_LOGGER = logging.getLogger(__name__)
1+
"""Telemetry Sync Class."""
42

53
from splitio.api.telemetry import TelemetryAPI
64
from splitio.engine.telemetry import TelemetryStorageConsumer
@@ -41,7 +39,12 @@ def synchronize_stats(self):
4139
self._telemetry_api.record_stats(self._build_stats())
4240

4341
def _build_stats(self):
44-
"""Format stats to JSON."""
42+
"""
43+
Format stats to Dict.
44+
45+
:returns: formatted stats
46+
:rtype: Dict
47+
"""
4548
merged_dict = {
4649
'spC': self._split_storage.get_splits_count(),
4750
'seC': self._segment_storage.get_segments_count(),

splitio/util/host_info.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
import socket
33

44
def get_ip():
5+
"""
6+
Fetching current host IP address
7+
8+
:returns: IP address
9+
:rtype: str
10+
"""
511
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
612
try:
713
# doesn't even have to be reachable
@@ -15,6 +21,12 @@ def get_ip():
1521

1622

1723
def get_hostname():
24+
"""
25+
Fetching current host name
26+
27+
:returns: host name
28+
:rtype: str
29+
"""
1830
try:
1931
return socket.gethostname()
2032
except Exception:

0 commit comments

Comments
 (0)