mirror of
https://github.com/pretix/pretix.git
synced 2026-05-06 15:24:02 +00:00
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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user