Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug Django app running on Heroku using a remote pdb connection?

To debug a bug I'm seeing on Heroku but not on my local machine, I'm trying to do step-through debugging.

The typical import pdb; pdb.set_trace() approach doesn't work with Heroku since you don't have access to a console connected to your app, but apparently you can use rpdb, a "remote" version of pdb.

So I've installed rpdb, added import rpdb; rpdb.set_trace() at the appropriate spot. When I make a request that hits the rpdb line, the app hangs as expected and I see the following in my heroku log:

pdb is running on 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc:4444

Ok, so how to connect to the pdb that is running? I've tried heroku run nc 3d0c9fdd-c18a-4cc2-8466-da6671a72cbc 4444 to try to connect to the named host from within heroku's system, but that just immediately exits with status 1 and no error message.

So my specific question is: how do I now connect to this remote pdb?

The general related question is: is this even the right way for this sort of interactive debugging of an app running on Heroku? Is there a better way?

NOTE RE CELERY: Note, I've now also tried a similar approach with Celery, to no avail. The default host celery's rdb (remote pdb wrapper) uses is localhost, which you can't get to when it's Heroku. I've tried using the CELERY_RDB_HOST environment variable to the domain of the website that is being hosted on Heroku, but that gives a "Cannot assign requested address" error. So it's the same basic issue -- how to connect to the remote pdb instance that's running on Heroku?

like image 326
Ghopper21 Avatar asked Apr 04 '13 03:04

Ghopper21


1 Answers

In answer to your second question, I do it differently depending on the type of error (browser-side, backend, or view). For backend and view testing (unittests), will something like this work for you?

$ heroku run --app=your-app "python manage.py shell --settings=settings.production"

Then debug-away within ipython:

>>> %run -d script_to_run_unittests.py

Even if you aren't running a django app you could just run the debugger as a command line option to ipython so that any python errors will drop you to the debugger:

$ heroku run --app=your-app "ipython --pdb"

Front-end testing is a whole different ballgame where you should look into tools like selenium. I think there's also a "salad" test suite module that makes front end tests easier to write. Writing a test that breaks is the first step in debugging (or so I'm told ;).

If the bug looks simple, you can always do the old "print and run" with something like

import logging
logger = logging.getLogger(__file__)
logger.warn('here be bugs')`

and review your log files with getsentry.com or an equivalent monitoring tool or just:

heroku logs --tail
like image 115
hobs Avatar answered Oct 14 '22 06:10

hobs