def test_metrics_not_collected_if_zero_and_reset(): m = MetricsSet(MetricsRegistry(0, lambda x: None)) counter = m.counter("counter", reset_on_collect=False) resetting_counter = m.counter("resetting_counter", reset_on_collect=True) gauge = m.gauge("gauge", reset_on_collect=False) resetting_gauge = m.gauge("resetting_gauge", reset_on_collect=True) timer = m.timer("timer", reset_on_collect=False) resetting_timer = m.timer("resetting_timer", reset_on_collect=True) counter.inc(), resetting_counter.inc() gauge.val = 5 resetting_gauge.val = 5 timer.update(1, 1) resetting_timer.update(1, 1) data = list(m.collect()) more_data = list(m.collect()) assert set(data[0]["samples"].keys()) == { "counter", "resetting_counter", "gauge", "resetting_gauge", "timer.count", "timer.sum.us", "resetting_timer.count", "resetting_timer.sum.us", } assert set(more_data[0]["samples"].keys()) == {"counter", "gauge", "timer.count", "timer.sum.us"}
def test_metrics_counter(): metricset = MetricsSet(MetricsRegistry(0, lambda x: None)) metricset.counter("x").inc() data = next(metricset.collect()) assert data["samples"]["x"]["value"] == 1 metricset.counter("x").inc(10) data = next(metricset.collect()) assert data["samples"]["x"]["value"] == 11 metricset.counter("x").dec(10) data = next(metricset.collect()) assert data["samples"]["x"]["value"] == 1 metricset.counter("x").dec() data = next(metricset.collect()) assert data["samples"]["x"]["value"] == 0
def test_metrics_counter(elasticapm_client): metricset = MetricsSet(MetricsRegistry(elasticapm_client)) metricset.counter("x").inc() data = next(metricset.collect()) assert data["samples"]["x"]["value"] == 1 metricset.counter("x").inc(10) data = next(metricset.collect()) assert data["samples"]["x"]["value"] == 11 metricset.counter("x").dec(10) data = next(metricset.collect()) assert data["samples"]["x"]["value"] == 1 metricset.counter("x").dec() data = next(metricset.collect()) assert data["samples"]["x"]["value"] == 0
def test_metrics_counter(): metricset = MetricsSet() metricset.counter("x").inc() data = metricset.collect() assert data["samples"]["x"]["value"] == 1 metricset.counter("x").inc(10) data = metricset.collect() assert data["samples"]["x"]["value"] == 11 metricset.counter("x").dec(10) data = metricset.collect() assert data["samples"]["x"]["value"] == 1 metricset.counter("x").dec() data = metricset.collect() assert data["samples"]["x"]["value"] == 0
def test_metrics_histogram(elasticapm_client): metricset = MetricsSet(MetricsRegistry(elasticapm_client)) hist = metricset.histogram("x", buckets=[1, 10, 100]) assert len(hist.buckets) == 4 hist.update(0.3) hist.update(1) hist.update(5) hist.update(20) hist.update(100) hist.update(1000) data = list(metricset.collect()) assert len(data) == 1 d = data[0] assert d["samples"]["x"]["counts"] == [2, 1, 2, 1] assert d["samples"]["x"]["values"] == [0.5, 5.5, 55.0, 100]
def test_metrics_labels(): metricset = MetricsSet(MetricsRegistry(0, lambda x: None)) metricset.counter("x", mylabel="a").inc() metricset.counter("y", mylabel="a").inc() metricset.counter("x", mylabel="b").inc().inc() metricset.counter("x", mylabel="b", myotherlabel="c").inc() metricset.counter("x", mylabel="a").dec() data = list(metricset.collect()) asserts = 0 for d in data: if d["tags"] == {"mylabel": "a"}: assert d["samples"]["x"]["value"] == 0 assert d["samples"]["y"]["value"] == 1 asserts += 1 elif d["tags"] == {"mylabel": "b"}: assert d["samples"]["x"]["value"] == 2 asserts += 1 elif d["tags"] == {"mylabel": "b", "myotherlabel": "c"}: assert d["samples"]["x"]["value"] == 1 asserts += 1 assert asserts == 3