예제 #1
0
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"}
예제 #2
0
def test_metrics_multithreaded():
    metricset = MetricsSet(MetricsRegistry(0, lambda x: None))
    pool = Pool(5)

    def target():
        for i in range(500):
            metricset.counter("x").inc(i + 1)
            time.sleep(0.0000001)

    [pool.apply_async(target, ()) for i in range(10)]
    pool.close()
    pool.join()
    expected = 10 * ((500 * 501) / 2)
    assert metricset.counter("x").val == expected
예제 #3
0
def test_metrics_flushed_on_shutdown(sending_elasticapm_client):
    # this is ugly, we need an API for this at some point...
    metricset = MetricsSet(sending_elasticapm_client._metrics)
    sending_elasticapm_client._metrics._metricsets["foo"] = metricset
    metricset.counter("x").inc()
    sending_elasticapm_client.close()
    assert sending_elasticapm_client.httpserver.payloads
    for item in sending_elasticapm_client.httpserver.payloads[0]:
        try:
            assert item["metricset"]["samples"]["x"]["value"] == 1
            break
        except KeyError:
            pass
    else:
        assert False, "no item found with matching dict path metricset.samples.x.value"
예제 #4
0
def test_metric_limit(caplog, elasticapm_client):
    m = MetricsSet(MetricsRegistry(elasticapm_client))
    with caplog.at_level(logging.WARNING, logger="elasticapm.metrics"):
        for i in range(2):
            counter = m.counter("counter", some_label=i)
            gauge = m.gauge("gauge", some_label=i)
            timer = m.timer("timer", some_label=i)
            if i == 0:
                assert isinstance(timer, Timer)
                assert isinstance(gauge, Gauge)
                assert isinstance(counter, Counter)
            else:
                assert isinstance(timer, NoopMetric)
                assert isinstance(gauge, NoopMetric)
                assert isinstance(counter, NoopMetric)
    assert_any_record_contains(caplog.records, "The limit of 3 metricsets has been reached", "elasticapm.metrics")
예제 #5
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]
예제 #6
0
def test_metric_limit(caplog):
    m = MetricsSet(MetricsRegistry(0, lambda x: None))
    with caplog.at_level(logging.WARNING, logger="elasticapm.metrics"):
        for i in range(2):
            counter = m.counter("counter", some_label=i)
            gauge = m.gauge("gauge", some_label=i)
            timer = m.timer("timer", some_label=i)
            if i == 0:
                assert isinstance(timer, Timer)
                assert isinstance(gauge, Gauge)
                assert isinstance(counter, Counter)
            else:
                assert isinstance(timer, NoopMetric)
                assert isinstance(gauge, NoopMetric)
                assert isinstance(counter, NoopMetric)

    assert len(caplog.records) == 1
    record = caplog.records[0]
    assert "The limit of 3 metricsets has been reached" in record.message
예제 #7
0
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
예제 #8
0
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
예제 #9
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
예제 #10
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