未加星标

Rollbar monitoring of Celery in a Django app

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

ByMatt Layman on February 7, 2017


Rollbar monitoring of Celery in a Django app

The lastRollbar post that I made covered how to integrate Rollbar into your Ember application. As a reminder, Rollbar is a service that let’s you record your errors wherever they happen. In that original post, I didn’t cover how to include Rollbar in a Django application because the Rollbar team does a great job of that in their own documentation . But what happens when you want to do asynchronous task management with Celery in your Django app? I encountered some gotchas in making that work so I’m going to describe what I learned here.

College Conductor uses Celery to make sure that slow tasks like sending email do not happen in the web request. I still want the safety net of error reporting in my async tasks, but I discovered that my Celery workers were not sending errors to Rollbar. This initally surprised me because I was using the Django middleware in the Rollbar documentation.

When I looked closely at the problem, I realized that it made perfect sense that Celery was not running Rollbar code. The Celery worker reads the Django settings to initialize its tasks, but it does not execute the Django middleware. Thus, the Rollbar configuration is skipped. After searching, I found Rollbar’s example code for configuring a Celery worker and that is when I encountered my problem. The sample code and the Django middleware configure the rollbar.BASE_DATA_HOOK attribute, but they use different python functions for the hook. Both the Celery worker and the Django application need access to the Celery app instance so I had to resolve how to have two different BASE_DATA_HOOK functions.

The best solution I could devise was to do Celery specific configuration if a certain environment variable was set.

In my Django settings file, I have the necessary ROLLBAR dictionary:

ROLLBAR = { 'access_token': os.environ['ROLLBAR_ACCESS_TOKEN'], 'environment': os.environ['ROLLBAR_ENVIRONMENT'], 'root': BASE_DIR, 'enabled': bool(os.environ.get('ROLLBAR_ENABLED', False)), }

These settings, in combination with the Django middleware, make the Django application report errors to Rollbar. (Note: my Django app follows the twelve factor app design so all of the environment specific stuff like acccess tokens are injected as environment variables. It’s a very powerful pattern.)

Over in my celery.py module, the code looks like:

import os from celery import Celery from celery.signals import task_failure os.environ.setdefault( 'DJANGO_SETTINGS_MODULE', 'conductor.settings.development') app = Celery('conductor') app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks() if bool(os.environ.get('CELERY_WORKER_RUNNING', False)): from django.conf import settings import rollbar rollbar.init(**settings.ROLLBAR) def celery_base_data_hook(request, data): data['framework'] = 'celery' rollbar.BASE_DATA_HOOK = celery_base_data_hook @task_failure.connect def handle_task_failure(**kw): rollbar.report_exc_info(extra_data=kw)

The linchpin of this setup is the CELERY_WORKER_RUNNING environment variable. All the code within the if statement will look very similar to Rollbar’s Celery example. In my configuration management tool, I add the CELERY_WORKER_RUNNING variable for the Celery worker and exclude it for the Django app. By doing this, the system is able to get the proper BASE_DATA_HOOK depending on the execution context.

Without too much extra work, I managed to make my project send Django and Celery errors to Rollbar in a shared module. I hope this setup can help anyone else struggling to make the two contexts play nicely together.

If you want to chat about this with me, I'm @mblayman on Twitter.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: DjangoRYDUTIPythonTwitter
分页:12
转载请注明
本文标题:Rollbar monitoring of Celery in a Django app
本站链接:http://www.codesec.net/view/531312.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(43)