예제 #1
0
def test_counter(monkeypatch):

    fake_redis = FakeRedis()

    monkeypatch.setattr(metrics, "redis", fake_redis, raising=False)

    # now test
    fullname_GET = metrics.http_requests_total._construct_metric_identifier(
        'http_requests_total', {
            "code": "200",
            "handler": "/foo",
            "method": "GET"
        })
    fullname_POST = metrics.http_requests_total._construct_metric_identifier(
        'http_requests_total', {
            "code": "200",
            "handler": "/foo",
            "method": "POST"
        })
    metrics.http_requests_total.inc(code="200", handler="/foo", method="GET")
    assert fake_redis.storage[metrics.REDIS_KEY_PREFIX + fullname_GET] == 1
    metrics.http_requests_total.inc(code="200", handler="/foo", method="GET")
    assert fake_redis.storage[metrics.REDIS_KEY_PREFIX + fullname_GET] == 2
    metrics.http_requests_total.inc(7,
                                    code="200",
                                    handler="/foo",
                                    method="GET")
    assert fake_redis.storage[metrics.REDIS_KEY_PREFIX + fullname_GET] == 9
    metrics.http_requests_total.inc(7,
                                    code="200",
                                    handler="/foo",
                                    method="POST")
    assert fake_redis.storage[metrics.REDIS_KEY_PREFIX + fullname_GET] == 9
    assert fake_redis.storage[metrics.REDIS_KEY_PREFIX + fullname_POST] == 7

    with pytest.raises(ValueError):
        metrics.http_requests_total.inc(-4,
                                        code="200",
                                        handler="/foo",
                                        method="POST")

    with pytest.raises(ValueError):
        metrics.http_requests_total.inc(-4,
                                        code="200",
                                        handler="/foo",
                                        method="POST",
                                        too="much")

    # test dimensionless counters
    dimless_counter = metrics.Counter("dimless_counter",
                                      "this is a helpstring")
    fullname_dimless = dimless_counter._construct_metric_identifier(
        'dimless_counter')
    dimless_counter.inc(20)
    assert fake_redis.storage[metrics.REDIS_KEY_PREFIX +
                              fullname_dimless] == 20
예제 #2
0
def test_counter(monkeypatch):

    fake_redis = FakeRedis()

    monkeypatch.setattr(metrics, "redis", fake_redis, raising=False)
    pretix_view_requests_total = metrics.Counter("pretix_view_requests_total", "Total number of HTTP requests made.",
                                                 ["status_code", "method", "url_name"])

    # now test
    fullname_get = pretix_view_requests_total._construct_metric_identifier(
        'pretix_view_requests_total', {"status_code": "200", "url_name": "foo", "method": "GET"}
    )
    fullname_post = pretix_view_requests_total._construct_metric_identifier(
        'pretix_view_requests_total', {"status_code": "200", "url_name": "foo", "method": "POST"}
    )
    pretix_view_requests_total.inc(status_code="200", url_name="foo", method="GET")
    assert fake_redis.storage[fullname_get] == 1
    pretix_view_requests_total.inc(status_code="200", url_name="foo", method="GET")
    assert fake_redis.storage[fullname_get] == 2
    pretix_view_requests_total.inc(7, status_code="200", url_name="foo", method="GET")
    assert fake_redis.storage[fullname_get] == 9
    pretix_view_requests_total.inc(7, status_code="200", url_name="foo", method="POST")
    assert fake_redis.storage[fullname_get] == 9
    assert fake_redis.storage[fullname_post] == 7

    with pytest.raises(ValueError):
        pretix_view_requests_total.inc(-4, status_code="200", url_name="foo", method="POST")

    with pytest.raises(ValueError):
        pretix_view_requests_total.inc(-4, status_code="200", url_name="foo", method="POST", too="much")

    # test dimensionless counters
    dimless_counter = metrics.Counter("dimless_counter", "this is a helpstring")
    fullname_dimless = dimless_counter._construct_metric_identifier('dimless_counter')
    dimless_counter.inc(20)
    assert fake_redis.storage[fullname_dimless] == 20
예제 #3
0
def test_metrics_view(monkeypatch, client):

    fake_redis = FakeRedis()
    monkeypatch.setattr(metricsview.metrics, "redis", fake_redis, raising=False)

    counter_value = 3
    pretix_view_requests_total = metrics.Counter("pretix_view_requests_total", "Total number of HTTP requests made.",
                                                 ["status_code", "method", "url_name"])
    fullname = pretix_view_requests_total._construct_metric_identifier(
        'http_requests_total',
        {"status_code": "200", "url_name": "foo", "method": "GET"}
    )
    pretix_view_requests_total.inc(counter_value, status_code="200", url_name="foo", method="GET")

    # test unauthorized-page
    assert "You are not authorized" in client.get('/metrics').content.decode('utf-8')
    assert "{} {}".format(fullname, counter_value) not in client.get('/metrics')

    # test metrics-view
    basic_auth = {"HTTP_AUTHORIZATION": base64.b64encode(bytes("foo:bar", "utf-8"))}
    assert "{} {}".format(fullname, counter_value) not in client.get("/metrics", headers=basic_auth)