Tracing Celery Performance For Web Applications
by July 16, 2012

Filed under: Performance Monitoring

AppNeta no longer blogs on DevOps topics like this one.

Feel free to enjoy it, and check out what we can do for monitoring end user experience of the apps you use to drive your business at www.appneta.com.

Tracelytics

When AppNeta acquired Tracelytics and their awesome team – we decided to keep their great blog content too.

Are you using Celery to process python backend tasks asynchronously?  Have you wanted to get insight into their resource consumption and efficiency?  Here’s a few useful ways to get insight into Celery performance when running tasks.
A simple celery task
For a quick review, Celery lets you turn any python method into an asynchronous task.  Here’s a simple one:

from celery.task import task
@task
def add(x, y):
    return x + y

Let’s trace Celery!

We’ll start with the good stuff. In the latest release of our Python instrumentation, oboeware-1.0, we have an updated API that makes it super-easy to gather performance data from any Python code, including Celery tasks. Here’s how we’d add tracing to our example task:

from celery.task import task
import oboe
from oboeware import loader

# load monkey-patched instrumentation for supported modules
loader.load_inst_modules()

# start traces on decorated method (sampling automatically
# controlled by smart tracing as normal web requests)
oboe.config['tracing_mode'] = 'always'

@task
@oboe.trace('celery', kvs={'Controller': 'task', 'Action': 'add'})
def add(x, y):
    return x + y

The key part is the @oboe.Context.trace decorator.  Note that we’re assigning to the keys Controller and Action.  This will be used by Tracelytics to segment the data.  You can also optionally use the keys HTTP-Host and URL to indicate domain and URL to Tracelytics.  Here’s what the data we’re gathering looks like in the dashboard (I added a few sqlalchemy queries to spice up the data):

Celery Tracing 1

The code at the top is configuration tunables: set the sample rate to a fractional value to trace only a fraction of your Celery tasks.

Event-based workers

You might be using event-based workers to save a few bytes of ram.  Tracelytics also supports eventlet-based workers.  However, you’ll need to install our Tracelytics-enabled greenlet module using pip or easy install:

$ pip install --extra-index-url=http://pypi.tracelytics.com greenlet-0.3.4-tly1.0

Slightly more interesting

Of course, you’re probably interested in more complex celery tasks than that.  The good news is, all of our normal instrumentation works with your celery workers.  Here’s a screenshot of one of our internal Celery workers in action:

Other resources

Celery Tracing 2Looking for different tools?  Good news–there’s a pretty healthy Celery ecosystem.  Here’s a few useful packages and I’m sure there’s tons I’m forgetting–leave comments for anything else you find helpful with Celery!

  • If you want to keep track of worker status, you might be interested in celerymon, a monitor for celery tasks.  It keeps tabs of task execution and workers.
  • Trying to track down a memory leak?  Consider running dowser inside your celery worker.

Photo Credit: dottiemae