forked from CGM_Public/pretix_original
Metrics: Replace redundant metrics by aliases
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import math
|
||||
from collections import defaultdict
|
||||
|
||||
from django.apps import apps
|
||||
from django.conf import settings
|
||||
@@ -186,20 +187,28 @@ class Histogram(Metric):
|
||||
|
||||
def metric_values():
|
||||
"""
|
||||
Produces the scrapable textformat to be presented to the monitoring system
|
||||
Produces the the values to be presented to the monitoring system
|
||||
"""
|
||||
metrics = {}
|
||||
metrics = defaultdict(dict)
|
||||
|
||||
# Metrics from redis
|
||||
if settings.HAS_REDIS:
|
||||
for key, value in redis.hscan_iter(REDIS_KEY):
|
||||
dkey = key.decode("utf-8")
|
||||
splitted = dkey.split("{", 2)
|
||||
value = float(value.decode("utf-8"))
|
||||
metrics[dkey] = value
|
||||
metrics[splitted[0]]["{" + splitted[1]] = value
|
||||
|
||||
# Aliases
|
||||
aliases = {
|
||||
'pretix_view_requests_total': 'pretix_view_duration_seconds_count'
|
||||
}
|
||||
for a, atarget in aliases.items():
|
||||
metrics[a] = metrics[atarget]
|
||||
|
||||
# Throwaway metrics
|
||||
for m in apps.get_models(): # Count all models
|
||||
metrics['pretix_model_instances{model="%s"}' % m._meta] = m.objects.count()
|
||||
metrics['pretix_model_instances']['{model="%s"}' % m._meta] = m.objects.count()
|
||||
|
||||
return metrics
|
||||
|
||||
@@ -207,8 +216,6 @@ def metric_values():
|
||||
"""
|
||||
Provided metrics
|
||||
"""
|
||||
pretix_view_requests_total = Counter("pretix_view_requests_total", "Total number of HTTP requests made.",
|
||||
["status_code", "method", "url_name"])
|
||||
pretix_view_duration_seconds = Histogram("pretix_view_duration_seconds", "Return time of views.",
|
||||
["status_code", "method", "url_name"])
|
||||
pretix_task_runs_total = Counter("pretix_task_runs_total", "Total calls to a celery task",
|
||||
|
||||
@@ -38,8 +38,9 @@ def serve_metrics(request):
|
||||
m = metrics.metric_values()
|
||||
|
||||
output = []
|
||||
for metric, value in m.items():
|
||||
output.append("{} {}".format(metric, str(value)))
|
||||
for metric, sub in m.items():
|
||||
for label, value in sub.items():
|
||||
output.append("{}{} {}".format(metric, label, str(value)))
|
||||
|
||||
content = "\n".join(output) + "\n"
|
||||
|
||||
|
||||
@@ -2,9 +2,7 @@ import time
|
||||
|
||||
from django.urls import resolve
|
||||
|
||||
from pretix.base.metrics import (
|
||||
pretix_view_duration_seconds, pretix_view_requests_total,
|
||||
)
|
||||
from pretix.base.metrics import pretix_view_duration_seconds
|
||||
|
||||
|
||||
class MetricsMiddleware(object):
|
||||
@@ -30,8 +28,6 @@ class MetricsMiddleware(object):
|
||||
t0 = time.perf_counter()
|
||||
resp = self.get_response(request)
|
||||
tdiff = time.perf_counter() - t0
|
||||
pretix_view_requests_total.inc(1, status_code=resp.status_code, method=request.method,
|
||||
url_name=url.namespace + ':' + url.url_name)
|
||||
pretix_view_duration_seconds.observe(tdiff, status_code=resp.status_code, method=request.method,
|
||||
url_name=url.namespace + ':' + url.url_name)
|
||||
|
||||
|
||||
@@ -34,29 +34,31 @@ 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 = metrics.pretix_view_requests_total._construct_metric_identifier(
|
||||
fullname_get = pretix_view_requests_total._construct_metric_identifier(
|
||||
'pretix_view_requests_total', {"status_code": "200", "url_name": "foo", "method": "GET"}
|
||||
)
|
||||
fullname_post = metrics.pretix_view_requests_total._construct_metric_identifier(
|
||||
fullname_post = pretix_view_requests_total._construct_metric_identifier(
|
||||
'pretix_view_requests_total', {"status_code": "200", "url_name": "foo", "method": "POST"}
|
||||
)
|
||||
metrics.pretix_view_requests_total.inc(status_code="200", url_name="foo", method="GET")
|
||||
pretix_view_requests_total.inc(status_code="200", url_name="foo", method="GET")
|
||||
assert fake_redis.storage[fullname_get] == 1
|
||||
metrics.pretix_view_requests_total.inc(status_code="200", url_name="foo", method="GET")
|
||||
pretix_view_requests_total.inc(status_code="200", url_name="foo", method="GET")
|
||||
assert fake_redis.storage[fullname_get] == 2
|
||||
metrics.pretix_view_requests_total.inc(7, status_code="200", url_name="foo", method="GET")
|
||||
pretix_view_requests_total.inc(7, status_code="200", url_name="foo", method="GET")
|
||||
assert fake_redis.storage[fullname_get] == 9
|
||||
metrics.pretix_view_requests_total.inc(7, status_code="200", url_name="foo", method="POST")
|
||||
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):
|
||||
metrics.pretix_view_requests_total.inc(-4, status_code="200", url_name="foo", method="POST")
|
||||
pretix_view_requests_total.inc(-4, status_code="200", url_name="foo", method="POST")
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
metrics.pretix_view_requests_total.inc(-4, status_code="200", url_name="foo", method="POST", too="much")
|
||||
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")
|
||||
@@ -166,11 +168,13 @@ def test_metrics_view(monkeypatch, client):
|
||||
monkeypatch.setattr(metricsview.metrics, "redis", fake_redis, raising=False)
|
||||
|
||||
counter_value = 3
|
||||
fullname = metrics.pretix_view_requests_total._construct_metric_identifier(
|
||||
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"}
|
||||
)
|
||||
metricsview.metrics.pretix_view_requests_total.inc(counter_value, 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')
|
||||
|
||||
Reference in New Issue
Block a user