Add metrics for view/task time

This commit is contained in:
Raphael Michel
2017-03-25 21:38:42 +01:00
parent f2378168c1
commit 4514bd7e53
7 changed files with 25 additions and 10 deletions

View File

@@ -15,25 +15,31 @@ import time
from django.conf import settings
from django.db import transaction
from pretix.base.metrics import celery_task_runs
from pretix.base.metrics import celery_task_runs, celery_task_times
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()
starttime = time.time()
t0 = time.perf_counter()
ret = super().__call__(*args, **kwargs)
tottime = time.perf_counter() - t0
profiler.disable()
tottime = time.time() - starttime
profiler.dump_stats(os.path.join(settings.PROFILE_DIR, '{time:.0f}_{tottime:.3f}_celery_{t}.pstat'.format(
t=self.name, tottime=tottime, time=time.time()
)))
return ret
else:
return super().__call__(*args, **kwargs)
t0 = time.perf_counter()
ret = super().__call__(*args, **kwargs)
tottime = time.perf_counter() - t0
if settings.METRICS_ENABLED:
celery_task_times.observe(tottime, task_name=self.name)
return ret
def on_failure(self, exc, task_id, args, kwargs, einfo):
if settings.METRICS_ENABLED:

View File

@@ -1,7 +1,6 @@
from collections import Counter, namedtuple
from datetime import timedelta
from decimal import Decimal
from typing import List, Optional
from celery.exceptions import MaxRetriesExceededError