def setUp(self): self.some_redis = mock.MagicMock() self.some_count_str = 'some_count' self.some_delta = mock.MagicMock() self.some_operation_str = 'some_operation' self.some_bucket_index = 15 self.some_gauge_str = 'some_gauge' self.some_value = mock.MagicMock() self.a_metrics_cache = RedisMetricsCache(self.some_redis) self.build_metrics_from_cache_response_mock = self.patch_object( self.a_metrics_cache, '_build_metrics_from_cache_response')
def setUp(self): self.some_script = 'some_script' self.some_num_keys = mock.MagicMock() self.some_key = mock.MagicMock() self.some_other_key = mock.MagicMock() self.some_value = mock.MagicMock() self.some_redis = mock.MagicMock() self.a_metrics_cache = RedisMetricsCache(self.some_redis)
def run(arguments): try: config = parse_config_file(arguments['<config_file>']) redis = get_redis(config) metrics_cache = RedisMetricsCache(redis) sdk_api = api_factory(config) report_metrics(metrics_cache, sdk_api) except: logger.exception('Exception caught posting metrics')
def _report_metrics(seconds, config): try: while True: redis = get_redis(config) metrics_cache = RedisMetricsCache(redis) sdk_api = api_factory(config) report_metrics(metrics_cache, sdk_api) time.sleep(seconds) except: logger.exception('Exception caught posting metrics')
def __init__(self, redis, config): """A Broker implementation that uses Redis as its backend. :param redis: A redis broker :type redis: StrctRedis""" super(RedisBroker, self).__init__(config) split_cache = RedisSplitCache(redis) split_fetcher = CacheBasedSplitFetcher(split_cache) impressions_cache = RedisImpressionsCache(redis) treatment_log = CacheBasedTreatmentLog(impressions_cache) metrics_cache = RedisMetricsCache(redis) delegate_metrics = CacheBasedMetrics(metrics_cache) metrics = AsyncMetrics(delegate_metrics) self._split_fetcher = split_fetcher self._treatment_log = treatment_log self._metrics = metrics self._event_storage = RedisEventsCache(redis)
def setUp(self): self.some_redis = mock.MagicMock() self.a_metrics_cache = RedisMetricsCache(self.some_redis)
class RedisMetricsCacheBuildMetricsFromCacheResponseTests(TestCase): def setUp(self): self.some_redis = mock.MagicMock() self.a_metrics_cache = RedisMetricsCache(self.some_redis) def test_returns_default_empty_dict_on_none_response(self): """Test that _build_metrics_from_cache_response returns the empty default dict if response is None""" self.assertDictEqual({'count': [], 'gauge': []}, self.a_metrics_cache._build_metrics_from_cache_response(None)) def test_returns_default_empty_dict_on_empty_response(self): """Test that _build_metrics_from_cache_response returns the empty default dict if response is empty""" self.assertDictEqual({'count': [], 'gauge': []}, self.a_metrics_cache._build_metrics_from_cache_response([])) def test_returns_count_metrics(self): """Test that _build_metrics_from_cache_response returns count metrics""" some_count = 'some_count' some_count_value = mock.MagicMock() some_other_count = 'some_other_count' some_other_count_value = mock.MagicMock() count_metrics = [ 'SPLITIO/python-'+__version__+'/unknown/count.some_count', some_count_value, 'SPLITIO/python-'+__version__+'/unknown/count.some_other_count', some_other_count_value ] result_count_metrics = [{'name': some_count, 'delta': some_count_value}, {'name': some_other_count, 'delta': some_other_count_value}] self.assertDictEqual({'count': result_count_metrics, 'gauge': []}, self.a_metrics_cache._build_metrics_from_cache_response(count_metrics)) def test_returns_time_metrics(self): """Test that _build_metrics_from_cache_response returns time metrics""" some_time = 'some_time' some_time_latencies = [0] * 23 some_time_latencies[2] = mock.MagicMock() some_time_latencies[13] = mock.MagicMock() some_other_time = 'some_other_time' some_other_time_latencies = [0] * 23 some_other_time_latencies[0] = mock.MagicMock() some_other_time_latencies[1] = mock.MagicMock() some_other_time_latencies[20] = mock.MagicMock() time_metrics = defaultdict(lambda: [0] * len(BUCKETS)) time_metrics[some_time] = some_time_latencies time_metrics[some_other_time] = some_other_time_latencies result_time_metris = [{'name': some_other_time, 'latencies': some_other_time_latencies}, {'name': some_time, 'latencies': some_time_latencies}] self.assertListEqual(result_time_metris, self.a_metrics_cache._build_metrics_times_data(time_metrics)) def test_returns_gauge_metrics(self): """Test that _build_metrics_from_cache_response returns gauge metrics""" some_gauge = 'some_gauge' some_gauge_value = mock.MagicMock() some_other_gauge = 'some_other_gauge' some_other_gauge_value = mock.MagicMock() gauge_metrics = [ 'SPLITIO/python-'+__version__+'/unknown/gauge.some_gauge', some_gauge_value, 'SPLITIO/python-'+__version__+'/unknown/gauge.some_other_gauge', some_other_gauge_value ] result_gauge_metrics = [{'name': some_gauge, 'value': some_gauge_value}, {'name': some_other_gauge, 'value': some_other_gauge_value}] self.assertDictEqual({'count': [], 'gauge': result_gauge_metrics}, self.a_metrics_cache._build_metrics_from_cache_response(gauge_metrics))
class RedisMetricsCacheTests(TestCase, MockUtilsMixin): def setUp(self): self.some_redis = mock.MagicMock() self.some_count_str = 'some_count' self.some_delta = mock.MagicMock() self.some_operation_str = 'some_operation' self.some_bucket_index = 15 self.some_gauge_str = 'some_gauge' self.some_value = mock.MagicMock() self.a_metrics_cache = RedisMetricsCache(self.some_redis) self.build_metrics_from_cache_response_mock = self.patch_object( self.a_metrics_cache, '_build_metrics_from_cache_response') def test_get_latency_calls_get(self): """Test that get_latency calls get in last position (22)""" self.a_metrics_cache.get_latency(self.some_operation_str) self.some_redis.get.assert_called_with( 'SPLITIO/python-'+__version__+'/unknown/latency.some_operation.bucket.{}' .format(22) ) def test_get_latency_returns_result(self): """Test that get_latency returns the result of calling get""" self.some_redis.get.return_value = mock.MagicMock() result = [self.some_redis.get.return_value] * 23 self.assertListEqual(result, self.a_metrics_cache.get_latency(self.some_operation_str)) def test_get_latency_sets_empty_results_to_zero(self): """Test that get_latency sets the missing results from get to zero""" self.some_redis.get.return_value = None self.assertEqual(0, self.a_metrics_cache.get_latency(self.some_operation_str)[13]) def test_get_latency_bucket_counter_calls_get(self): """Test that get_latency_bucket_counter calls get""" self.a_metrics_cache.get_latency_bucket_counter(self.some_operation_str, self.some_bucket_index) self.some_redis.get.assert_called_once_with( 'SPLITIO/python-'+__version__+'/unknown/latency.{0}.bucket.{1}'.format( self.some_operation_str, self.some_bucket_index)) def test_get_latency_bucket_counter_returns_get_result(self): """Test that get_latency_bucket_counter returns the result of calling get""" self.assertEqual(1, self.a_metrics_cache.get_latency_bucket_counter(self.some_operation_str, self.some_bucket_index)) def test_get_latency_bucket_counter_returns_0_on_missing_value(self): """Test that get_latency_bucket_counter returns 0 if the bucket value is not cached""" self.some_redis.get.return_value = None self.assertEqual(0, self.a_metrics_cache.get_latency_bucket_counter(self.some_operation_str, self.some_bucket_index)) def test_set_gauge_calls_hset(self): """Test that set_gauge calls hset""" self.a_metrics_cache.set_gauge(self.some_gauge_str, self.some_value) self.some_redis.hset('SPLITIO.metrics.metric', 'gauge.some_gauge', self.some_value) def test_set_latency_bucket_counter_calls_set(self): """Test that set_latency_bucket_counter calls conditional eval with set latency bucket counter script""" self.a_metrics_cache.set_latency_bucket_counter(self.some_operation_str, self.some_bucket_index, self.some_value) self.some_redis.set.assert_called_once_with( self.a_metrics_cache._get_latency_bucket_key( self.some_operation_str, bucket_number=self.some_bucket_index), self.some_value) def test_increment_latency_bucket_counter_calls_delta(self): """Test that increment_latency_bucket_counter calls with the increment latency bucket counter script""" self.a_metrics_cache.increment_latency_bucket_counter(self.some_operation_str, self.some_bucket_index, self.some_delta) self.some_redis.incr.assert_called_once_with( self.a_metrics_cache._get_latency_bucket_key( self.some_operation_str, self.some_bucket_index), self.some_delta) def test_increment_latency_bucket_counter_calls_default_delta(self): """Test that increment_latency_bucket_counter calls conditional eval with the increment latency bucket counter script with the default delta""" self.a_metrics_cache.increment_latency_bucket_counter(self.some_operation_str, self.some_bucket_index) self.some_redis.incr.assert_called_once_with( self.a_metrics_cache._get_latency_bucket_key( self.some_operation_str, self.some_bucket_index ), 1 )