Skip to content
Merged
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
41 changes: 39 additions & 2 deletions splitio/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from random import randint

from splitio.api import SdkApi
from splitio.metrics import (Metrics, AsyncMetrics, ApiMetrics, SDK_GET_TREATMENT)
from splitio.impressions import (TreatmentLog, AsyncTreatmentLog, SelfUpdatingTreatmentLog)
from splitio.splitters import Splitter
from splitio.splits import (SelfRefreshingSplitFetcher, SplitParser, ApiSplitChangeFetcher,
Expand All @@ -26,6 +27,7 @@ def __init__(self):
self._logger = logging.getLogger(self.__class__.__name__)
self._splitter = None
self._treatment_log = None
self._metrics = None

def get_split_fetcher(self):
"""Get the split fetcher implementation. Subclasses need to override this method.
Expand Down Expand Up @@ -54,6 +56,16 @@ def get_treatment_log(self):

return self._treatment_log

def get_metrics(self):
"""Get the metrics implementation.
:return: The metrics implementation.
:rtype: Metrics
"""
if self._metrics is None:
self._metrics = Metrics()

return self._metrics

def get_treatment(self, key, feature, attributes=None):
"""
Get the treatment for a feature and key, with an optional dictionary of attributes. This
Expand All @@ -77,7 +89,7 @@ def get_treatment(self, key, feature, attributes=None):
split = self.get_split_fetcher().fetch(feature)
treatment = self._get_treatment_for_split(split, key, attributes)

self._record_stats(key, feature, treatment, start, 'sdk.getTreatment')
self._record_stats(key, feature, treatment, start, SDK_GET_TREATMENT)

return treatment
except:
Expand All @@ -88,7 +100,7 @@ def _record_stats(self, key, feature, treatment, start, operation):
try:
end = arrow.utcnow().timestamp * 1000
self.get_treatment_log().log(key, feature, treatment, end)
# TODO record metrics
self.get_metrics().time(operation, end - start)
except:
self._logger.exception('Exception caught recording impressions and metrics')

Expand Down Expand Up @@ -175,6 +187,9 @@ def __init__(self, api_key, config=None, sdk_api_base_url=None):
self._split_fetcher_interval = split_fetcher_interval
self._impressions_interval = impressions_interval

self._metrics_max_time_between_calls = min(MAX_INTERVAL, self._config['metricsRefreshRate'])
self._metrics_max_call_count = self._config['maxMetricsCallsBeforeFlush']

self._connection_timeout = self._config['connectionTimeout']
self._read_timeout = self._config['readTimeout']
self._max_impressions_log_size = self._config['maxImpressionsLogSize']
Expand Down Expand Up @@ -228,6 +243,18 @@ def get_treatment_log(self):

return self._treatment_log

def get_metrics(self):
"""Get the metrics implementation.
:return: The metrics implementation.
:rtype: Metrics
"""
if self._metrics is None:
api_metrics = ApiMetrics(self._sdk_api, max_call_count=self._metrics_max_call_count,
max_time_between_calls=self._metrics_max_time_between_calls)
self._metrics = AsyncMetrics(api_metrics)

return self._metrics


class JSONFileClient(Client):
def __init__(self, segment_changes_file_name, split_changes_file_name):
Expand Down Expand Up @@ -278,3 +305,13 @@ def get_treatment_log(self):
self._treatment_log = AsyncTreatmentLog(TreatmentLog())

return self._treatment_log

def get_metrics(self):
"""Get the metrics implementation.
:return: The metrics implementation.
:rtype: Metrics
"""
if self._metrics is None:
self._metrics = AsyncMetrics(Metrics())

return self._metrics
Loading