Add basic instrumentation possibilities to pretix (#271)

* add basic instrumentation possibilities to pretix

* make tabs to spaces

* apply flake8

* implement upstreams suggestions, round 1

* adjust naming of redis-connection

* address noredis

* add view for metrics

* implement HTTP basic auth in front of metrics-endpoint

* rename labelset

* make flake8-clean

* implement upstreams suggestions, round 2

* correct minor slipups

* fix missing return

* let isort add an empty line

* implement test for counter

* implement upstream suggestions, round 3

* correct typo

* implement first test for view

* finish view-test

* fix deprecated keyword

* implement upstream-suggestions, round 4

* implement test for gauge

* test exceptions as well

* add db-decorator
This commit is contained in:
Jonas Große Sundrup
2016-11-20 14:46:45 +01:00
committed by Raphael Michel
parent d308821c4c
commit d3327b1e45
6 changed files with 354 additions and 1 deletions

150
src/pretix/base/metrics.py Executable file
View File

@@ -0,0 +1,150 @@
from django.conf import settings
if settings.HAS_REDIS:
import django_redis
redis = django_redis.get_redis_connection("redis")
REDIS_KEY_PREFIX = "pretix_metrics_"
class Metric(object):
"""
Base Metrics Object
"""
def __init__(self, name, helpstring, labelnames=None):
self.name = name
self.helpstring = helpstring
self.labelnames = labelnames or []
def __repr__(self):
return self.name + "{" + ",".join(self.labelnames) + "}"
def _check_label_consistency(self, labels):
"""
Checks if the given labels provides exactly the labels that are required.
"""
# test if every required label is provided
for labelname in self.labelnames:
if labelname not in labels:
raise ValueError("Label {0} not specified.".format(labelname))
# now test if no further labels are required
if len(labels) != len(self.labelnames):
raise ValueError("Unknown labels used: {}".format(", ".join(set(labels) - set(self.labelnames))))
def _construct_metric_identifier(self, metricname, labels=None):
"""
Constructs the scrapable metricname usable in the output format.
"""
if not labels:
return metricname
else:
named_labels = []
for labelname in self.labelnames:
named_labels.append('{}="{}",'.format(labelname, labels[labelname]))
return metricname + "{" + ",".join(named_labels) + "}"
def _inc_in_redis(self, key, amount):
"""
Increments given key in Redis.
"""
rkey = REDIS_KEY_PREFIX + key
if settings.HAS_REDIS:
redis.incrbyfloat(rkey, amount)
def _set_in_redis(self, key, value):
"""
Sets given key in Redis.
"""
rkey = REDIS_KEY_PREFIX + key
if settings.HAS_REDIS:
redis.set(rkey, value)
class Counter(Metric):
"""
Counter Metric Object
Counters can only be increased, they can neither be set to a specific value
nor decreased.
"""
def inc(self, amount=1, **kwargs):
"""
Increments Counter by given amount for the labels specified in kwargs.
"""
if amount < 0:
raise ValueError("Counter cannot be increased by negative values.")
self._check_label_consistency(kwargs)
fullmetric = self._construct_metric_identifier(self.name, kwargs)
self._inc_in_redis(fullmetric, amount)
class Gauge(Metric):
"""
Gauge Metric Object
Gauges can be set to a specific value, increased and decreased.
"""
def set(self, value=1, **kwargs):
"""
Sets Gauge to a specific value for the labels specified in kwargs.
"""
self._check_label_consistency(kwargs)
fullmetric = self._construct_metric_identifier(self.name, kwargs)
self._set_in_redis(fullmetric, value)
def inc(self, amount=1, **kwargs):
"""
Increments Gauge by given amount for the labels specified in kwargs.
"""
if amount < 0:
raise ValueError("Amount must be greater than zero. Otherwise use dec().")
self._check_label_consistency(kwargs)
fullmetric = self._construct_metric_identifier(self.name, kwargs)
self._inc_in_redis(fullmetric, amount)
def dec(self, amount=1, **kwargs):
"""
Decrements Gauge by given amount for the labels specified in kwargs.
"""
if amount < 0:
raise ValueError("Amount must be greater than zero. Otherwise use inc().")
self._check_label_consistency(kwargs)
fullmetric = self._construct_metric_identifier(self.name, kwargs)
self._inc_in_redis(fullmetric, amount * -1)
def metric_values():
"""
Produces the scrapable textformat to be presented to the monitoring system
"""
if not settings.HAS_REDIS:
return ""
metrics = {}
for key in redis.scan_iter(match=REDIS_KEY_PREFIX + "*"):
dkey = key.decode("utf-8")
_, _, output_key = dkey.split("_", 2)
value = float(redis.get(dkey).decode("utf-8"))
metrics[output_key] = value
return metrics
"""
Provided metrics
"""
http_requests_total = Counter("http_requests_total", "Total number of HTTP requests made.", ["code", "handler", "method"])
# usage: http_requests_total.inc(code="200", handler="/foo", method="GET")

View File

@@ -0,0 +1,43 @@
from django.conf import settings
from django.http import HttpResponse
from .. import metrics
def unauthed_response():
content = "<html><title>Forbidden</title><body>You are not authorized to view this page.</body></html>"
response = HttpResponse(content, content_type="text/html")
response["WWW-Authenticate"] = 'Basic realm="metrics"'
response.status_code = 401
return response
def serve_metrics(request):
if not settings.METRICS_ENABLED:
return unauthed_response()
# check if the user is properly authorized:
if "HTTP_AUTHORIZATION" not in request.META:
return unauthed_response()
method, credentials = request.META["HTTP_AUTHORIZATION"].split(" ", 1)
if method.lower() != "basic":
return unauthed_response()
user, passphrase = credentials.strip().decode("base64").split(":", 1)
if user != settings.METRICS_USER:
return unauthed_response()
if passphrase != settings.METRICS_PASSPHRASE:
return unauthed_response()
# ok, the request passed the authentication-barrier, let's hand out the metrics:
m = metrics.metric_values()
output = []
for metric, value in m:
output.append("{} {}".format(metric, str(value)))
content = "\n".join(output)
return HttpResponse(content)