def test_gauge_last(self): self.pid = 123 g1 = Gauge('g1last', 'help', registry=None, multiprocess_mode='last') g1.set(1) self.pid = 456 g1.set(2) self.assertEqual(2, self.registry.get_sample_value('g1last'))
def test_gauge_min(self): g1 = Gauge('g', 'help', registry=None, multiprocess_mode='min') core._ValueClass = core._MultiProcessValue(lambda: 456) g2 = Gauge('g', 'help', registry=None, multiprocess_mode='min') self.assertEqual(0, self.registry.get_sample_value('g')) g1.set(1) g2.set(2) self.assertEqual(1, self.registry.get_sample_value('g'))
def test_gauge_max(self): g1 = Gauge('g', 'help', registry=None, multiprocess_mode='max') values.ValueClass = MultiProcessValue(lambda: 456) g2 = Gauge('g', 'help', registry=None, multiprocess_mode='max') self.assertEqual(0, self.registry.get_sample_value('g')) g1.set(1) g2.set(2) self.assertEqual(2, self.registry.get_sample_value('g'))
class TestGauge(unittest.TestCase): def setUp(self): self.registry = CollectorRegistry() self.gauge = Gauge('g', 'help', registry=self.registry) def test_gauge(self): self.assertEqual(0, self.registry.get_sample_value('g')) self.gauge.inc() self.assertEqual(1, self.registry.get_sample_value('g')) self.gauge.dec(3) self.assertEqual(-2, self.registry.get_sample_value('g')) self.gauge.set(9) self.assertEqual(9, self.registry.get_sample_value('g')) def test_inprogress_function_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) @self.gauge.track_inprogress() def f(): self.assertEqual(1, self.registry.get_sample_value('g')) self.assertEqual(([], None, None, None), inspect.getargspec(f)) f() self.assertEqual(0, self.registry.get_sample_value('g')) def test_inprogress_block_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) with self.gauge.track_inprogress(): self.assertEqual(1, self.registry.get_sample_value('g')) self.assertEqual(0, self.registry.get_sample_value('g')) def test_gauge_function(self): x = {} self.gauge.set_function(lambda: len(x)) self.assertEqual(0, self.registry.get_sample_value('g')) self.gauge.inc() self.assertEqual(0, self.registry.get_sample_value('g')) x['a'] = None self.assertEqual(1, self.registry.get_sample_value('g')) def test_time_function_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) @self.gauge.time() def f(): time.sleep(.001) self.assertEqual(([], None, None, None), inspect.getargspec(f)) f() self.assertNotEqual(0, self.registry.get_sample_value('g')) def test_time_block_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) with self.gauge.time(): time.sleep(.001) self.assertNotEqual(0, self.registry.get_sample_value('g'))
def test_gauge_livesum(self): g1 = Gauge('g', 'help', registry=None, multiprocess_mode='livesum') values.ValueClass = MultiProcessValue(lambda: 456) g2 = Gauge('g', 'help', registry=None, multiprocess_mode='livesum') self.assertEqual(0, self.registry.get_sample_value('g')) g1.set(1) g2.set(2) self.assertEqual(3, self.registry.get_sample_value('g')) mark_process_dead(123, os.environ['PROMETHEUS_MULTIPROC_DIR']) self.assertEqual(2, self.registry.get_sample_value('g'))
def test_gauge_livesum(self): g1 = Gauge('g', 'help', registry=None, multiprocess_mode='livesum') core._ValueClass = core._MultiProcessValue(lambda: 456) g2 = Gauge('g', 'help', registry=None, multiprocess_mode='livesum') self.assertEqual(0, self.registry.get_sample_value('g')) g1.set(1) g2.set(2) self.assertEqual(3, self.registry.get_sample_value('g')) mark_process_dead(123, os.environ['prometheus_multiproc_dir']) self.assertEqual(2, self.registry.get_sample_value('g'))
def test_gauge_max(self): self.pid = 123 g1 = Gauge('gmax', 'help', registry=None, multiprocess_mode='max') self.pid = 456 g2 = Gauge('gmax', 'help', registry=None, multiprocess_mode='max') self.assertEqual(0, self.registry.get_sample_value('gmax', {'hostname':hostname})) self.pid = 123 g1.set(1) self.pid = 456 g2.set(2) self.assertEqual(2, self.registry.get_sample_value('gmax', {'hostname':hostname}))
def test_gauge_all(self): g1 = Gauge('g', 'help', registry=None) core._ValueClass = core._MultiProcessValue(lambda: 456) g2 = Gauge('g', 'help', registry=None) self.assertEqual(0, self.registry.get_sample_value('g', {'pid': '123'})) self.assertEqual(0, self.registry.get_sample_value('g', {'pid': '456'})) g1.set(1) g2.set(2) mark_process_dead(123, os.environ['prometheus_multiproc_dir']) self.assertEqual(1, self.registry.get_sample_value('g', {'pid': '123'})) self.assertEqual(2, self.registry.get_sample_value('g', {'pid': '456'}))
def test_gauge_livesum(self): self.pid = 123 g1 = Gauge('gls', 'help', registry=None, multiprocess_mode='livesum') self.pid = 456 g2 = Gauge('gls', 'help', registry=None, multiprocess_mode='livesum') self.assertEqual(0, self.registry.get_sample_value('gls', {'hostname':hostname})) self.pid = 123 g1.set(1) self.pid = 456 g2.set(2) self.assertEqual(3, self.registry.get_sample_value('gls', {'hostname':hostname})) from prometheus_client.distributed import mark_distributed_process_dead mark_distributed_process_dead(123) self.assertEqual(2, self.registry.get_sample_value('gls', {'hostname':hostname}))
def test_gauge_all(self): g1 = Gauge('g', 'help', registry=None) values.ValueClass = MultiProcessValue(lambda: 456) g2 = Gauge('g', 'help', registry=None) self.assertEqual(0, self.registry.get_sample_value('g', {'pid': '123'})) self.assertEqual(0, self.registry.get_sample_value('g', {'pid': '456'})) g1.set(1) g2.set(2) mark_process_dead(123) self.assertEqual(1, self.registry.get_sample_value('g', {'pid': '123'})) self.assertEqual(2, self.registry.get_sample_value('g', {'pid': '456'}))
def test_gauge_all(self): values.ValueClass = MultiProcessValue(lambda: 123) g1 = Gauge('g', 'help', registry=None, multiprocess_mode='all') values.ValueClass = MultiProcessValue(lambda: 456) g2 = Gauge('g', 'help', registry=None, multiprocess_mode='all') self.assertEqual(0, self.registry.get_sample_value('g', {'pid': '123'})) self.assertEqual(0, self.registry.get_sample_value('g', {'pid': '456'})) g1.set(1) g2.set(2) archive_metrics() mark_process_dead(123, os.environ['prometheus_multiproc_dir']) self.assertEqual(1, self.registry.get_sample_value('g', {'pid': '123'})) self.assertEqual(2, self.registry.get_sample_value('g', {'pid': '456'}))
def test_aggregates_live_and_archived_metrics(self): pid = 456 values.ValueClass = MultiProcessValue(lambda: pid) def files(): fs = os.listdir(os.environ['prometheus_multiproc_dir']) fs.sort() return fs c1 = Counter('c1', 'c1', registry=None) c1.inc(1) self.assertIn('counter_456.db', files()) archive_metrics() self.assertNotIn('counter_456.db', files()) self.assertEqual(1, self.registry.get_sample_value('c1_total')) pid = 789 values.ValueClass = MultiProcessValue(lambda: pid) c1 = Counter('c1', 'c1', registry=None) c1.inc(2) g1 = Gauge('g1', 'g1', registry=None, multiprocess_mode="liveall") g1.set(5) self.assertIn('counter_789.db', files()) # Pretend that pid 789 is live archive_metrics(aggregate_only=True) # The live counter should be merged with the archived counter, and the # liveall gauge should be included self.assertIn('counter_789.db', files()) self.assertIn('gauge_liveall_789.db', files()) self.assertEqual(3, self.registry.get_sample_value('c1_total')) self.assertEqual( 5, self.registry.get_sample_value('g1', labels={u'pid': u'789'})) # Now pid 789 is dead archive_metrics() # The formerly live counter's value should be archived, and the # liveall gauge should be removed completely self.assertNotIn('counter_789.db', files()) self.assertNotIn('gauge_liveall_789.db', files()) self.assertEqual(3, self.registry.get_sample_value('c1_total')) self.assertEqual( None, self.registry.get_sample_value('g1', labels={u'pid': u'789'}))
def test_gauge_latest(self): self.assertEqual(None, self.registry.get_sample_value('g')) g1 = Gauge('g', 'G', registry=None, multiprocess_mode=Gauge.LATEST) g1.set(0) self.assertEqual(0, self.registry.get_sample_value('g')) g1.set(123) self.assertEqual(123, self.registry.get_sample_value('g')) t0 = time.time() g1.set(1, timestamp=t0) self.assertEqual(1, self.registry.get_sample_value('g')) archive_metrics() self.assertEqual(1, self.registry.get_sample_value('g')) values.ValueClass = MultiProcessValue(lambda: '456789') g2 = Gauge('g', 'G', registry=None, multiprocess_mode=Gauge.LATEST) t1 = t0 - time.time() g2.set(2, timestamp=t1) self.assertEqual(1, self.registry.get_sample_value('g')) archive_metrics() self.assertEqual(1, self.registry.get_sample_value('g'))
class TestGauge(unittest.TestCase): def setUp(self): self.registry = CollectorRegistry() self.gauge = Gauge('g', 'help', registry=self.registry) def test_gauge(self): self.assertEqual(0, self.registry.get_sample_value('g')) self.gauge.inc() self.assertEqual(1, self.registry.get_sample_value('g')) self.gauge.dec(3) self.assertEqual(-2, self.registry.get_sample_value('g')) self.gauge.set(9) self.assertEqual(9, self.registry.get_sample_value('g')) def test_inprogress_function_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) @self.gauge.track_inprogress() def f(): self.assertEqual(1, self.registry.get_sample_value('g')) self.assertEqual(([], None, None, None), inspect.getargspec(f)) f() self.assertEqual(0, self.registry.get_sample_value('g')) def test_inprogress_block_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) with self.gauge.track_inprogress(): self.assertEqual(1, self.registry.get_sample_value('g')) self.assertEqual(0, self.registry.get_sample_value('g')) def test_gauge_function(self): x = {} self.gauge.set_function(lambda: len(x)) self.assertEqual(0, self.registry.get_sample_value('g')) self.gauge.inc() self.assertEqual(0, self.registry.get_sample_value('g')) x['a'] = None self.assertEqual(1, self.registry.get_sample_value('g')) def test_time_function_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) @self.gauge.time() def f(): time.sleep(.001) self.assertEqual(([], None, None, None), inspect.getargspec(f)) f() self.assertNotEqual(0, self.registry.get_sample_value('g')) def test_function_decorator_multithread(self): self.assertEqual(0, self.registry.get_sample_value('g')) workers = 2 pool = ThreadPoolExecutor(max_workers=workers) @self.gauge.time() def f(duration): time.sleep(duration) expected_duration = 1 pool.submit(f, expected_duration) time.sleep(0.7 * expected_duration) pool.submit(f, expected_duration * 2) time.sleep(expected_duration) rounding_coefficient = 0.9 adjusted_expected_duration = expected_duration * rounding_coefficient self.assertLess(adjusted_expected_duration, self.registry.get_sample_value('g')) pool.shutdown(wait=True) def test_time_block_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) with self.gauge.time(): time.sleep(.001) self.assertNotEqual(0, self.registry.get_sample_value('g'))
class TestGauge(unittest.TestCase): def setUp(self): self.registry = CollectorRegistry() self.gauge = Gauge('g', 'help', registry=self.registry) self.gauge_with_label = Gauge('g2', 'help', labelnames=("label1",), registry=self.registry) def test_repr(self): self.assertEqual(repr(self.gauge), "prometheus_client.metrics.Gauge(g)") def test_gauge(self): self.assertEqual(0, self.registry.get_sample_value('g')) self.gauge.inc() self.assertEqual(1, self.registry.get_sample_value('g')) self.gauge.dec(3) self.assertEqual(-2, self.registry.get_sample_value('g')) self.gauge.set(9) self.assertEqual(9, self.registry.get_sample_value('g')) def test_inc_not_observable(self): """.inc() must fail if the gauge is not observable.""" assert_not_observable(self.gauge_with_label.inc) def test_dec_not_observable(self): """.dec() must fail if the gauge is not observable.""" assert_not_observable(self.gauge_with_label.dec) def test_set_not_observable(self): """.set() must fail if the gauge is not observable.""" assert_not_observable(self.gauge_with_label.set, 1) def test_inprogress_function_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) @self.gauge.track_inprogress() def f(): self.assertEqual(1, self.registry.get_sample_value('g')) self.assertEqual(([], None, None, None), getargspec(f)) f() self.assertEqual(0, self.registry.get_sample_value('g')) def test_inprogress_block_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) with self.gauge.track_inprogress(): self.assertEqual(1, self.registry.get_sample_value('g')) self.assertEqual(0, self.registry.get_sample_value('g')) def test_gauge_function(self): x = {} self.gauge.set_function(lambda: len(x)) self.assertEqual(0, self.registry.get_sample_value('g')) self.gauge.inc() self.assertEqual(0, self.registry.get_sample_value('g')) x['a'] = None self.assertEqual(1, self.registry.get_sample_value('g')) def test_set_function_not_observable(self): """.set_function() must fail if the gauge is not observable.""" assert_not_observable(self.gauge_with_label.set_function, lambda: 1) def test_time_function_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) @self.gauge.time() def f(): time.sleep(.001) self.assertEqual(([], None, None, None), getargspec(f)) f() self.assertNotEqual(0, self.registry.get_sample_value('g')) def test_function_decorator_multithread(self): self.assertEqual(0, self.registry.get_sample_value('g')) workers = 2 pool = ThreadPoolExecutor(max_workers=workers) @self.gauge.time() def f(duration): time.sleep(duration) expected_duration = 1 pool.submit(f, expected_duration) time.sleep(0.7 * expected_duration) pool.submit(f, expected_duration * 2) time.sleep(expected_duration) rounding_coefficient = 0.9 adjusted_expected_duration = expected_duration * rounding_coefficient self.assertLess(adjusted_expected_duration, self.registry.get_sample_value('g')) pool.shutdown(wait=True) def test_time_block_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) with self.gauge.time(): time.sleep(.001) self.assertNotEqual(0, self.registry.get_sample_value('g')) def test_time_block_decorator_with_label(self): value = self.registry.get_sample_value self.assertEqual(None, value('g2', {'label1': 'foo'})) with self.gauge_with_label.time() as metric: metric.labels('foo') self.assertLess(0, value('g2', {'label1': 'foo'})) def test_track_in_progress_not_observable(self): g = Gauge('test', 'help', labelnames=('label',), registry=self.registry) assert_not_observable(g.track_inprogress) def test_timer_not_observable(self): g = Gauge('test', 'help', labelnames=('label',), registry=self.registry) def manager(): with g.time(): pass assert_not_observable(manager)
class TestGauge(unittest.TestCase): def setUp(self): self.registry = CollectorRegistry() self.gauge = Gauge('g', 'help', registry=self.registry) def test_repr(self): self.assertEqual(repr(self.gauge), "prometheus_client.metrics.Gauge(g)") def test_gauge(self): self.assertEqual(0, self.registry.get_sample_value('g')) self.gauge.inc() self.assertEqual(1, self.registry.get_sample_value('g')) self.gauge.dec(3) self.assertEqual(-2, self.registry.get_sample_value('g')) self.gauge.set(9) self.assertEqual(9, self.registry.get_sample_value('g')) def test_inprogress_function_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) @self.gauge.track_inprogress() def f(): self.assertEqual(1, self.registry.get_sample_value('g')) self.assertEqual(([], None, None, None), getargspec(f)) f() self.assertEqual(0, self.registry.get_sample_value('g')) def test_inprogress_block_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) with self.gauge.track_inprogress(): self.assertEqual(1, self.registry.get_sample_value('g')) self.assertEqual(0, self.registry.get_sample_value('g')) def test_gauge_function(self): x = {} self.gauge.set_function(lambda: len(x)) self.assertEqual(0, self.registry.get_sample_value('g')) self.gauge.inc() self.assertEqual(0, self.registry.get_sample_value('g')) x['a'] = None self.assertEqual(1, self.registry.get_sample_value('g')) def test_time_function_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) @self.gauge.time() def f(): time.sleep(.001) self.assertEqual(([], None, None, None), getargspec(f)) f() self.assertNotEqual(0, self.registry.get_sample_value('g')) def test_function_decorator_multithread(self): self.assertEqual(0, self.registry.get_sample_value('g')) workers = 2 pool = ThreadPoolExecutor(max_workers=workers) @self.gauge.time() def f(duration): time.sleep(duration) expected_duration = 1 pool.submit(f, expected_duration) time.sleep(0.7 * expected_duration) pool.submit(f, expected_duration * 2) time.sleep(expected_duration) rounding_coefficient = 0.9 adjusted_expected_duration = expected_duration * rounding_coefficient self.assertLess(adjusted_expected_duration, self.registry.get_sample_value('g')) pool.shutdown(wait=True) def test_time_block_decorator(self): self.assertEqual(0, self.registry.get_sample_value('g')) with self.gauge.time(): time.sleep(.001) self.assertNotEqual(0, self.registry.get_sample_value('g')) def test_track_in_progress_not_observable(self): g = Gauge('test', 'help', labelnames=('label',), registry=self.registry) try: g.track_inprogress() except ValueError as e: self.assertIn('missing label values', str(e)) def test_timer_not_observable(self): g = Gauge('test', 'help', labelnames=('label',), registry=self.registry) try: g.time() except ValueError as e: self.assertIn('missing label values', str(e))
def write( self, path, groups_scanned=0, projects_scanned=0, projects_skipped=0, projects_no_language=0, ): total_languages = len(self.metrics.items()) total_percent = sum( [float(percent) for _, percent in self.metrics.items()]) logger.debug(f"{total_percent}% total scanned") relative_languages = { language_name: (float(language) / projects_scanned) for language_name, language in self.metrics.items() } gauge = Gauge( "languages_percent", "Languages scanned in percent", labelnames=["language"], registry=self.registry, ) language_items = relative_languages.items() for language_name, language in language_items: logger.info(f"Adding {language_name} as label") gauge.labels(language_name).set(round(language, 2)) total_languages_scanned_gauge = Gauge("languages_scanned_total", "Total languages scanned", registry=self.registry) total_languages_scanned_gauge.set(total_languages) project_scanned_gauge = Gauge("projects_scanned_total", "Total projects scanned", registry=self.registry) project_scanned_gauge.set(projects_scanned) projects_skipped_gauge = Gauge("projects_skipped_total", "Total projects skipped", registry=self.registry) projects_skipped_gauge.set(projects_skipped) projects_no_language_gauge = Gauge( "projects_no_language_total", "Projects without language detected", registry=self.registry, ) projects_no_language_gauge.set(projects_no_language) groups_scanned_gauge = Gauge("groups_scanned_total", "Total groups scanned", registry=self.registry) groups_scanned_gauge.set(groups_scanned) if Path(path).is_dir(): path = Path(path) / "metrics.txt" if Path.exists(Path(path).parents[0]): write_to_textfile(path, self.registry) logger.info(f"Metrics written to {path}") else: logger.error(f"Could not write metrics to {path}")