mirror of
https://github.com/pretix/pretix.git
synced 2026-05-09 15:54:03 +00:00
Add metrics for request/task counting
This commit is contained in:
@@ -146,5 +146,7 @@ def metric_values():
|
|||||||
"""
|
"""
|
||||||
Provided metrics
|
Provided metrics
|
||||||
"""
|
"""
|
||||||
http_requests_total = Counter("http_requests_total", "Total number of HTTP requests made.", ["code", "handler", "method"])
|
http_view_requests = Counter("http_view_requests", "Total number of HTTP requests made.",
|
||||||
# usage: http_requests_total.inc(code="200", handler="/foo", method="GET")
|
["status_code", "method", "url_name"])
|
||||||
|
celery_task_runs = Counter("celery_task_runs", "Total calls to a celery task",
|
||||||
|
["task_name", "status"])
|
||||||
|
|||||||
@@ -15,12 +15,12 @@ import time
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
|
||||||
|
from pretix.base.metrics import celery_task_runs
|
||||||
from pretix.celery_app import app
|
from pretix.celery_app import app
|
||||||
|
|
||||||
|
|
||||||
class ProfiledTask(app.Task):
|
class ProfiledTask(app.Task):
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
|
|
||||||
if settings.PROFILING_RATE > 0 and random.random() < settings.PROFILING_RATE / 100:
|
if settings.PROFILING_RATE > 0 and random.random() < settings.PROFILING_RATE / 100:
|
||||||
profiler = cProfile.Profile()
|
profiler = cProfile.Profile()
|
||||||
profiler.enable()
|
profiler.enable()
|
||||||
@@ -35,6 +35,23 @@ class ProfiledTask(app.Task):
|
|||||||
else:
|
else:
|
||||||
return super().__call__(*args, **kwargs)
|
return super().__call__(*args, **kwargs)
|
||||||
|
|
||||||
|
def on_failure(self, exc, task_id, args, kwargs, einfo):
|
||||||
|
if settings.METRICS_ENABLED:
|
||||||
|
expected = False
|
||||||
|
for t in self.throws:
|
||||||
|
if isinstance(exc, t):
|
||||||
|
expected = True
|
||||||
|
break
|
||||||
|
celery_task_runs.inc(1, task_name=self.name, status="expected-error" if expected else "error")
|
||||||
|
|
||||||
|
return super().on_failure(exc, task_id, args, kwargs, einfo)
|
||||||
|
|
||||||
|
def on_success(self, retval, task_id, args, kwargs):
|
||||||
|
if settings.METRICS_ENABLED:
|
||||||
|
celery_task_runs.inc(1, task_name=self.name, status="success")
|
||||||
|
|
||||||
|
return super().on_success(retval, task_id, args, kwargs)
|
||||||
|
|
||||||
|
|
||||||
class TransactionAwareTask(ProfiledTask):
|
class TransactionAwareTask(ProfiledTask):
|
||||||
"""
|
"""
|
||||||
|
|||||||
0
src/pretix/helpers/metrics/__init__.py
Normal file
0
src/pretix/helpers/metrics/__init__.py
Normal file
30
src/pretix/helpers/metrics/middleware.py
Normal file
30
src/pretix/helpers/metrics/middleware.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
from django.urls import resolve
|
||||||
|
|
||||||
|
from pretix.base.metrics import http_view_requests
|
||||||
|
|
||||||
|
|
||||||
|
class MetricsMiddleware(object):
|
||||||
|
blacklist = (
|
||||||
|
'/healthcheck/',
|
||||||
|
'/jsi18n/',
|
||||||
|
'/metrics',
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self, get_response):
|
||||||
|
self.get_response = get_response
|
||||||
|
# One-time configuration and initialization.
|
||||||
|
|
||||||
|
def __call__(self, request):
|
||||||
|
# Code to be executed for each request before
|
||||||
|
# the view (and later middleware) are called.
|
||||||
|
for b in self.blacklist:
|
||||||
|
if b in request.path:
|
||||||
|
return self.get_response(request)
|
||||||
|
|
||||||
|
url = resolve(request.path_info)
|
||||||
|
|
||||||
|
resp = self.get_response(request)
|
||||||
|
http_view_requests.inc(1, status_code=resp.status_code, method=request.method,
|
||||||
|
url_name=url.namespace + ':' + url.url_name)
|
||||||
|
|
||||||
|
return resp
|
||||||
@@ -261,6 +261,11 @@ except ImportError:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if METRICS_ENABLED:
|
||||||
|
MIDDLEWARE.insert(MIDDLEWARE.index('pretix.multidomain.middlewares.MultiDomainMiddleware') + 1,
|
||||||
|
'pretix.helpers.metrics.middleware.MetricsMiddleware')
|
||||||
|
|
||||||
|
|
||||||
PROFILING_RATE = config.getfloat('django', 'profile', fallback=0) # Percentage of requests to profile
|
PROFILING_RATE = config.getfloat('django', 'profile', fallback=0) # Percentage of requests to profile
|
||||||
if PROFILING_RATE > 0:
|
if PROFILING_RATE > 0:
|
||||||
if not os.path.exists(PROFILE_DIR):
|
if not os.path.exists(PROFILE_DIR):
|
||||||
|
|||||||
Reference in New Issue
Block a user