mirror of
https://github.com/pretix/pretix.git
synced 2026-05-06 15:24:02 +00:00
Add metrics for view/task time
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user