Koed00 / django-q

A multiprocessing distributed task queue for Django
https://django-q.readthedocs.org
MIT License
1.8k stars 274 forks source link

Newrelic integration #338

Open JereMalinen opened 5 years ago

JereMalinen commented 5 years ago

I need to monitor tasks with Newrelic, here is some documentation: https://docs.newrelic.com/docs/agents/python-agent/supported-features/python-background-tasks

Documented way seems like I would need to remember to decorate all my tasks separately, which isn't too convenient. So I tried to leverage pre_execute signal https://github.com/Koed00/django-q/blob/master/django_q/cluster.py#L375 but could not effectively change the func from there.

Example code:

from django_q.signals import pre_execute

class RunTaskWithNewrelic:
    def __init__(self, f, task_name):
        self.f = f
        self.task_name = task_name

    def __call__(self, *args, **kwargs):
        print('newrelic task ' * 100)
        with newrelic.agent.BackgroundTask('qcluster', name=self.task_name):
            r = self.f(*args, **kwargs)
        return r

def newrelic_execute_callback(sender, func, task, **kwargs):
    print('newrelic_execute_callback ' * 100)
    func = RunTaskWithNewrelic(func, task['name'])

pre_execute.connect(newrelic_execute_callback)

Are there any smart solutions? =)

JereMalinen commented 5 years ago

Added solution: https://github.com/Koed00/django-q/pull/348