def test_initialize(self): h_a = Histogram('test_value_a', 'Testing roller a', registry=self.registry) h_b = Histogram('test_value_b', 'Testing roller b', registry=self.registry) r_a = HistogramRoller(h_a, registry=self.registry, roller_registry=self.roller_registry) r_b = HistogramRoller(h_b, registry=self.registry, roller_registry=self.roller_registry) t = start_update_daemon(roller_registry=self.roller_registry) self.assertEqual(len(t.rollers), 2)
def test_wait_period(self): h_a = Histogram('test_value_a', 'Testing roller a', registry=self.registry) h_b = Histogram('test_value_b', 'Testing roller b', registry=self.registry) h_c = Histogram('test_value_c', 'Testing roller c', registry=self.registry) r_a = HistogramRoller(h_a, registry=self.registry, roller_registry=self.roller_registry) r_b = HistogramRoller(h_b, registry=self.registry, roller_registry=self.roller_registry, options={'update_seconds': 10}) r_c = HistogramRoller(h_c, registry=self.registry, roller_registry=self.roller_registry, options={'update_seconds': 2}) t = PrometheusRollingMetricsUpdater() self.assertEqual(len(t.rollers), 0) self.assertEqual(t.wait_period, MAX_WAIT_PERIOD) t.add(r_a) self.assertEqual(len(t.rollers), 1) self.assertEqual(t.wait_period, 5) t.add(r_b) self.assertEqual(len(t.rollers), 2) self.assertEqual(t.wait_period, 5) t.add(r_c) self.assertEqual(len(t.rollers), 3) self.assertEqual(t.wait_period, 1) t.remove(r_a) self.assertEqual(len(t.rollers), 2) self.assertEqual(t.wait_period, 2)
def test_initialize(self): h = Histogram('test_value', 'Testing roller', registry=self.registry) roller = HistogramRoller(h, registry=self.registry) n_buckets = 0 for name, _, _ in self.get_hist_samples(): if name.endswith('_bucket'): n_buckets += 1 n_created_guages = 0 for _, _, _ in self.get_rolled_samples(): n_created_guages += 1 # Check that roller gauges don't exist until values are added self.assertTrue(n_buckets > 0) self.assertTrue(n_created_guages == 0) self.assertEqual(roller.name, 'test_value_sum_rolled')
def test_collect(self): h = Histogram('test_value', 'Testing roller', registry=self.registry) roller = HistogramRoller(h, registry=self.registry) # Get values roller.collect() n_buckets = 0 for _, _, _ in self.get_hist_samples(): n_buckets += 1 n_created_guages = 0 for _, _, _ in self.get_rolled_samples(): n_created_guages += 1 self.assertTrue(n_buckets > 0) self.assertTrue(n_created_guages > 0) self.assertEqual(n_buckets, n_created_guages) # Check that roller values are still 0.0 after initial collection for name, labels, value in self.get_rolled_samples(): self.assertEqual(value, 0.0) # Add some samples for i in range(100): h.observe(pow(2, i/10 - 2)) # Collect hisogram values hist_values = dict() for name, labels, value in self.get_hist_samples(): hist_values[labels['le']] = value # Make sure they are still equal after collection for name, labels, value in self.get_rolled_samples(): self.assertEqual(value, 0.0) roller.collect() for name, labels, value in self.get_rolled_samples(): self.assertEqual(value, hist_values[labels['le']])
def update_seconds_not_divisible_by_1_exception(): h = Histogram('test_value', 'Testing roller', registry=self.registry) roller = HistogramRoller(h, registry=self.registry, options={ 'update_seconds': 2.5 })
def update_seconds_lt_1_exception(): h = Histogram('test_value', 'Testing roller', registry=self.registry) roller = HistogramRoller(h, registry=self.registry, options={ 'update_seconds': 0 })
def wrong_type_exception(): c = Counter('test_value', 'Testing roller', registry=self.registry) roller = HistogramRoller(c, registry=self.registry)
def test_customize_reducer(self): h = Histogram('test_value', 'Testing roller', registry=self.registry) roller_max = HistogramRoller(h, registry=self.registry, options={ 'reducer': 'max' }) roller_min = HistogramRoller(h, registry=self.registry, options={ 'reducer': 'sum' }) def always_one(*args, **kwargs): return 1 roller_one = HistogramRoller(h, registry=self.registry, options={ 'reducer': always_one }) for state in [2.6, 4.7, 3.8, 2.8]: h.observe(state) roller_max.collect() roller_min.collect() roller_one.collect() # Deltas = 1, 1, 1 nchecks = 0 for m in self.registry.collect(): if m.name.endswith('max_rolled'): for name, labels, val in m.samples: if labels['le'] == '5.0': nchecks += 1 self.assertEqual(val, 1.0) self.assertTrue(nchecks > 0) nchecks = 0 for m in self.registry.collect(): if m.name.endswith('sum_rolled'): for name, labels, val in m.samples: if labels['le'] == '5.0': self.assertEqual(val, 3.0) nchecks += 1 self.assertTrue(nchecks > 0) nchecks = 0 for m in self.registry.collect(): if m.name.endswith('always_one_rolled'): for name, labels, val in m.samples: if labels['le'] == '5.0': self.assertEqual(val, 1.0) nchecks += 1 self.assertTrue(nchecks > 0)