def test_histograms_keep_correct_number_of_data_when_set_max_data_values_called( self): test_histogram = meters.Histogram("test_histogram") test_histogram.set_max_data_values(25) @test_histogram.summarize_method def x(f): return f for q in range(100): x(q) loop = asyncio.get_event_loop() actual = loop.run_until_complete( loop.create_task(test_histogram.get_metrics())) useful = [ metric for metric in actual if metric.name in ["test_histogram_sum", "test_histogram_count"] ] expected = [ meters.Metric(name='test_histogram_sum', tags=(), value=2175, prometheus_type='histogram'), meters.Metric(name='test_histogram_count', tags=(), value=25, prometheus_type='histogram') ] self.assertEqual(useful, expected)
def test_get_all_metrics_is_return_expected_values(self): registry.MeterRegistry._meters = dict() test_gauge = meters.Gauge("test_gauge") test_gauge.set_gauge_function(lambda: 1) self.assertEqual(registry.MeterRegistry.get_all_metrics(), [ meters.Metric( name='test_gauge', tags=(), value=1, prometheus_type='gauge') ])
def test_gauge_when_gauge_function_returns_dictionary(self): test_gauge = meters.Gauge("Test Gauge") test_gauge.set_gauge_function(lambda: { "value1": 2, "value2": 3 }, "key") loop = asyncio.get_event_loop() actual = loop.run_until_complete( loop.create_task(test_gauge.get_metrics())) expected = [ meters.Metric(name='Test Gauge', tags=(('key', 'value1'), ), value=2, prometheus_type='gauge'), meters.Metric(name='Test Gauge', tags=(('key', 'value2'), ), value=3, prometheus_type='gauge') ] self.assertEqual(actual, expected)
def test_gauge_when_gauge_function_returns_number(self): test_gauge = meters.Gauge("Test Gauge") test_gauge.set_gauge_function(lambda: 1) loop = asyncio.get_event_loop() actual = loop.run_until_complete( loop.create_task(test_gauge.get_metrics())) expected = [ meters.Metric(name='Test Gauge', tags=(), value=1, prometheus_type='gauge') ] self.assertEqual(actual, expected)
def test_counters_increment_as_expected(self): test_counter = meters.Counter("test_counter") test_counter.increment() test_counter.increment() loop = asyncio.get_event_loop() actual = loop.run_until_complete( loop.create_task(test_counter.get_metrics())) expected = [ meters.Metric(name='test_counter', tags=(), value=2, prometheus_type='counter') ] self.assertEqual(actual, expected)
def test_counters_wrap_functions_as_expected(self): test_counter = meters.Counter("test_counter") @test_counter.auto_count_method def bogus_function(): pass bogus_function() loop = asyncio.get_event_loop() actual = loop.run_until_complete( loop.create_task(test_counter.get_metrics())) expected = [ meters.Metric(name='test_counter', tags=(), value=1, prometheus_type='counter') ] self.assertEqual(actual, expected)
def test_timers_wrap_functions_as_expected(self): test_timer = meters.Timer("test_timer") @test_timer.timer def bogus_function(): sleep(.001) bogus_function() loop = asyncio.get_event_loop() actual = loop.run_until_complete( loop.create_task(test_timer.get_metrics())) expected = [ meters.Metric(name='test_timer_time_sum', tags=(), value=0.001, prometheus_type='summary') ] self.assertAlmostEqual(actual[0].value, expected[0].value, 3) self.assertEqual(actual[0].name, expected[0].name) self.assertEqual(actual[0].tags, expected[0].tags) self.assertEqual(actual[0].prometheus_type, expected[0].prometheus_type)
def test_histograms_return_all_values_as_expected_with_defaults(self): test_histogram = meters.Histogram("test_histogram") @test_histogram.summarize_method def x(f): return f for q in range(100): x(q) loop = asyncio.get_event_loop() actual = loop.run_until_complete( loop.create_task(test_histogram.get_metrics())) expected = [ meters.Metric(name='test_histogram_sum', tags=(), value=4950, prometheus_type='histogram'), meters.Metric(name='test_histogram_count', tags=(), value=100, prometheus_type='histogram'), meters.Metric(name='test_histogram_bucket', tags=(('le', '9.9'), ), value=10, prometheus_type='histogram'), meters.Metric(name='test_histogram_bucket', tags=(('le', '19.8'), ), value=10, prometheus_type='histogram'), meters.Metric(name='test_histogram_bucket', tags=(('le', '29.700000000000003'), ), value=10, prometheus_type='histogram'), meters.Metric(name='test_histogram_bucket', tags=(('le', '39.6'), ), value=10, prometheus_type='histogram'), meters.Metric(name='test_histogram_bucket', tags=(('le', '49.5'), ), value=10, prometheus_type='histogram'), meters.Metric(name='test_histogram_bucket', tags=(('le', '59.400000000000006'), ), value=10, prometheus_type='histogram'), meters.Metric(name='test_histogram_bucket', tags=(('le', '69.3'), ), value=10, prometheus_type='histogram'), meters.Metric(name='test_histogram_bucket', tags=(('le', '79.2'), ), value=10, prometheus_type='histogram'), meters.Metric(name='test_histogram_bucket', tags=(('le', '89.10000000000001'), ), value=10, prometheus_type='histogram'), meters.Metric(name='test_histogram_bucket', tags=(('le', '99.0'), ), value=10, prometheus_type='histogram'), meters.Metric(name='test_histogram_percentile', tags=(('percentile', '50'), ), value=49.5, prometheus_type='gauge'), meters.Metric(name='test_histogram_percentile', tags=(('percentile', '75'), ), value=74.25, prometheus_type='gauge'), meters.Metric(name='test_histogram_percentile', tags=(('percentile', '90'), ), value=89.10000000000001, prometheus_type='gauge'), meters.Metric(name='test_histogram_percentile', tags=(('percentile', '95'), ), value=94.05, prometheus_type='gauge'), meters.Metric(name='test_histogram_percentile', tags=(('percentile', '99'), ), value=98.01, prometheus_type='gauge') ] self.assertEqual(actual, expected)
async def get_metrics(self) -> [meters.Metric]: total_metrics = [ meters.Metric(self.name + "_time_sum", key, 1, "summary") for key, _ in self.total_time.items() ] return total_metrics