Skip to content

Commit

Permalink
Moved celery task time execution log to DEBUG, sending as statsd metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
jimleroyer committed Nov 9, 2023
1 parent 9b6fa23 commit 1904442
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions app/celery/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ def worker_process_shutdown(sender, signal, pid, exitcode, **kwargs):


def make_task(app):

from app import statsd_client

class NotifyTask(Task):
abstract = True
start = None

def on_success(self, retval, task_id, args, kwargs):
elapsed_time = time.time() - self.start
app.logger.debug("{task_name} took {time}".format(task_name=self.name, time="{0:.4f}".format(elapsed_time)))
task_name: str = self.name
now: float = time.time()
statsd_client.timing_with_dates(f"celery-task.{task_name}.total-time", now, self.start)

elapsed_time = now - self.start
app.logger.debug("{task_name} took {time}".format(task_name=task_name, time="{0:.4f}".format(elapsed_time)))

def on_failure(self, exc, task_id, args, kwargs, einfo):
# ensure task will log exceptions to correct handlers
Expand All @@ -28,7 +35,7 @@ def on_failure(self, exc, task_id, args, kwargs, einfo):
def __call__(self, *args, **kwargs):
# ensure task has flask context to access config, logger, etc
with app.app_context():
self.start = time.time()
self.start: float = time.time()
return super().__call__(*args, **kwargs)

return NotifyTask
Expand Down
2 changes: 1 addition & 1 deletion app/celery/provider_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def deliver_sms(self, notification_id):
@statsd(namespace="tasks")
def deliver_email(self, notification_id):
try:
current_app.logger.info("Start sending email for notification id: {}".format(notification_id))
current_app.logger.debug("Start sending email for notification id: {}".format(notification_id))
notification = notifications_dao.get_notification_by_id(notification_id)
if not notification:
raise NoResultFound()
Expand Down

0 comments on commit 1904442

Please sign in to comment.