Metrics: Replace redundant metrics by aliases

This commit is contained in:
Raphael Michel
2017-03-27 22:24:35 +02:00
parent bcdb4fd000
commit 2f6e36c504
4 changed files with 31 additions and 23 deletions

View File

@@ -1,4 +1,5 @@
import math import math
from collections import defaultdict
from django.apps import apps from django.apps import apps
from django.conf import settings from django.conf import settings
@@ -186,20 +187,28 @@ class Histogram(Metric):
def metric_values(): 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 # Metrics from redis
if settings.HAS_REDIS: if settings.HAS_REDIS:
for key, value in redis.hscan_iter(REDIS_KEY): for key, value in redis.hscan_iter(REDIS_KEY):
dkey = key.decode("utf-8") dkey = key.decode("utf-8")
splitted = dkey.split("{", 2)
value = float(value.decode("utf-8")) 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 # Throwaway metrics
for m in apps.get_models(): # Count all models 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 return metrics
@@ -207,8 +216,6 @@ def metric_values():
""" """
Provided metrics 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.", pretix_view_duration_seconds = Histogram("pretix_view_duration_seconds", "Return time of views.",
["status_code", "method", "url_name"]) ["status_code", "method", "url_name"])
pretix_task_runs_total = Counter("pretix_task_runs_total", "Total calls to a celery task", pretix_task_runs_total = Counter("pretix_task_runs_total", "Total calls to a celery task",

View File

@@ -38,8 +38,9 @@ def serve_metrics(request):
m = metrics.metric_values() m = metrics.metric_values()
output = [] output = []
for metric, value in m.items(): for metric, sub in m.items():
output.append("{} {}".format(metric, str(value))) for label, value in sub.items():
output.append("{}{} {}".format(metric, label, str(value)))
content = "\n".join(output) + "\n" content = "\n".join(output) + "\n"

View File

@@ -2,9 +2,7 @@ import time
from django.urls import resolve from django.urls import resolve
from pretix.base.metrics import ( from pretix.base.metrics import pretix_view_duration_seconds
pretix_view_duration_seconds, pretix_view_requests_total,
)
class MetricsMiddleware(object): class MetricsMiddleware(object):
@@ -30,8 +28,6 @@ class MetricsMiddleware(object):
t0 = time.perf_counter() t0 = time.perf_counter()
resp = self.get_response(request) resp = self.get_response(request)
tdiff = time.perf_counter() - t0 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, pretix_view_duration_seconds.observe(tdiff, status_code=resp.status_code, method=request.method,
url_name=url.namespace + ':' + url.url_name) url_name=url.namespace + ':' + url.url_name)

View File

@@ -34,29 +34,31 @@ def test_counter(monkeypatch):
fake_redis = FakeRedis() fake_redis = FakeRedis()
monkeypatch.setattr(metrics, "redis", fake_redis, raising=False) 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 # 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"} '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"} '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 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 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 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_get] == 9
assert fake_redis.storage[fullname_post] == 7 assert fake_redis.storage[fullname_post] == 7
with pytest.raises(ValueError): 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): 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 # test dimensionless counters
dimless_counter = metrics.Counter("dimless_counter", "this is a helpstring") 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) monkeypatch.setattr(metricsview.metrics, "redis", fake_redis, raising=False)
counter_value = 3 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', 'http_requests_total',
{"status_code": "200", "url_name": "foo", "method": "GET"} {"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 # test unauthorized-page
assert "You are not authorized" in client.get('/metrics').content.decode('utf-8') assert "You are not authorized" in client.get('/metrics').content.decode('utf-8')