def test_histogram_protected(context, registry): histogram = metrics.Histogram( name='test_histogram_protected', documentation='test histogram', labelnames=['label'], statsd='{name}.{label}', registry=registry, ) histogram.prometheus = 'THIS WILL RAISE' histogram.observe(1.0, label='label') assert context.logs.exists(msg='Failed to collect histogram metric')
def test_histogram(context, registry): histogram = metrics.Histogram( name='test_histogram', documentation='test histogram', labelnames=['label'], statsd='{name}.{label}', registry=registry, ) labels = {'label': 'value'} count = registry.get_metric('test_histogram_count', **labels) sum_ = registry.get_metric('test_histogram_sum', **labels) bucket = registry.get_metric('test_histogram_bucket', le='2.5', **labels) histogram.observe(2.0, **labels) assert context.statsd[0] == 'test.histogram.value:2.000000|ms' assert registry.get_metric('test_histogram_count', **labels) - count == 1.0 assert registry.get_metric('test_histogram_sum', **labels) - sum_ == 2.0 after_bucket = registry.get_metric( 'test_histogram_bucket', le='2.5', **labels) assert after_bucket - bucket == 1.0
def test_prometheus_cleanup(registry): pid = 1 def getpid(): return pid # override use of os.getpid. _ValueClass is recreated after every test, # so we don't need to clean up from prometheus_client import core core._ValueClass = core._MultiProcessValue(getpid) histogram = metrics.Histogram( name='histogram', documentation='test histogram', labelnames=['foo', 'bar', 'baz'], statsd='{name}.{label}', registry=registry, ) counter = metrics.Counter( name='counter', documentation='test counter', labelnames=['foo', 'bar', 'baz'], statsd='{name}.{label}', registry=registry, ) from prometheus_client.multiprocess import MultiProcessCollector collector = MultiProcessCollector(registry) labels = {'foo': 'foo', 'bar': 'bar', 'baz': 'baz'} def collect(): return {m.name: m for m in collector.collect()} def files(): return list(sorted(os.listdir(os.environ['prometheus_multiproc_dir']))) counter.inc(1, **labels) histogram.observe(0.5, **labels) histogram.observe(2.5, **labels) assert files() == [ 'counter_1.db', 'histogram_1.db', ] before = collect() metrics.prometheus_cleanup_worker(pid) after = collect() assert files() == [ 'counter_archive.db', 'histogram_archive.db', ] assert before == after # magic! pid += 1 # new worker, create some new metrics, check they are all combined counter.inc(2, **labels) histogram.observe(0.5, **labels) histogram.observe(2.5, **labels) later = collect() assert files() == [ 'counter_2.db', 'counter_archive.db', 'histogram_2.db', 'histogram_archive.db', ] # check counter is correct assert later['counter'].samples == [ Sample(counter_name('counter_total'), labels, 3.0), ] expected_histogram = [ Sample('histogram_bucket', dict(le='0.005', **labels), 0.0), Sample('histogram_bucket', dict(le='0.01', **labels), 0.0), Sample('histogram_bucket', dict(le='0.025', **labels), 0.0), Sample('histogram_bucket', dict(le='0.05', **labels), 0.0), Sample('histogram_bucket', dict(le='0.075', **labels), 0.0), Sample('histogram_bucket', dict(le='0.1', **labels), 0.0), Sample('histogram_bucket', dict(le='0.25', **labels), 0.0), Sample('histogram_bucket', dict(le='0.5', **labels), 2.0), Sample('histogram_bucket', dict(le='0.75', **labels), 2.0), Sample('histogram_bucket', dict(le='1.0', **labels), 2.0), Sample('histogram_bucket', dict(le='2.5', **labels), 4.0), Sample('histogram_bucket', dict(le='5.0', **labels), 4.0), Sample('histogram_bucket', dict(le='7.5', **labels), 4.0), Sample('histogram_bucket', dict(le='10.0', **labels), 4.0), Sample('histogram_bucket', dict(le='+Inf', **labels), 4.0), Sample('histogram_count', labels, 4.0), Sample('histogram_sum', labels, 6.0), ] # check histogram is correct later['histogram'].samples.sort(key=metrics.histogram_sorter) assert later['histogram'].samples == expected_histogram # check the final files produce the correct numbers metrics.prometheus_cleanup_worker(pid) final = collect() assert files() == [ 'counter_archive.db', 'histogram_archive.db', ] final['histogram'].samples.sort(key=metrics.histogram_sorter) assert later == final