Add metrics for request/task counting

This commit is contained in:
Raphael Michel
2017-03-25 21:17:12 +01:00
parent c932892dbd
commit e0e3a72268
5 changed files with 57 additions and 3 deletions

View File

@@ -146,5 +146,7 @@ def metric_values():
"""
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")
http_view_requests = Counter("http_view_requests", "Total number of HTTP requests made.",
["status_code", "method", "url_name"])
celery_task_runs = Counter("celery_task_runs", "Total calls to a celery task",
["task_name", "status"])

View File

@@ -15,12 +15,12 @@ import time
from django.conf import settings
from django.db import transaction
from pretix.base.metrics import celery_task_runs
from pretix.celery_app import app
class ProfiledTask(app.Task):
def __call__(self, *args, **kwargs):
if settings.PROFILING_RATE > 0 and random.random() < settings.PROFILING_RATE / 100:
profiler = cProfile.Profile()
profiler.enable()
@@ -35,6 +35,23 @@ class ProfiledTask(app.Task):
else:
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):
"""