/
2012-05-24-djangonewrelic-quickstart-gunicornd-django-13-hack.html
72 lines (60 loc) · 2.87 KB
/
2012-05-24-djangonewrelic-quickstart-gunicornd-django-13-hack.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
title: Django/NewRelic Quickstart (gunicorn.d + Django 1.3 hack)
tags: django gunicorn newrelic
---
<p>
New Relic is an excellent web application performance reporting tool. After a great experience using it on Heroku, I went to enable it for an existing self-hosted application. While Django 1.4 has an <a href="https://docs.djangoproject.com/en/dev/releases/1.4/#improved-wsgi-support">excellent WSGI</a> configuration build-in, Django 1.3 makes it a little trickier. To complicate matters even more, I was using debian's gunicorn package, which not not make changing the Django executable easy.
</p>
<p>
New Relic has <a href="https://newrelic.com/docs/python/new-relic-for-python">excellent documentation</a> for downloading, installing and configuring their tools. Here are my crib notes:
</p>
<pre name="code" class="bash">
pip install newrelic django-newrelic-extensions
# sign up on newrelic.com, get $LICENSE_KEY under "Account Settings"
mkdir /etc/newrelic
newrelic-admin generate-config $LICENSE_KEY /etc/newrelic/newrelic.ini
newrelic-admin validate-config /etc/newrelic/newrelic.ini
</pre>
<p>
You should now be able to log in to new relic and see an application called "Python Agent Test". This means that the test worked; new relic can communicate with the hosted servers. You can run gunicorn manually from the command line with new relic as follows:
</p>
<pre name="code" class="bash">
cd /opt/my-django-project
export NEW_RELIC_CONFIG_FILE=/etc/newrelic/newrelic.ini
newrelic-admin run-program python manage.py run_gunicorn
</pre>
<p>
Notice that new relic replaces the python executable with its own command, "newrelic-admin run-program". I'm using Ubuntu, which uses the Debian gunicorn package, which stores its configuration in /etc/gunicorn.d/django. Here is my current config file:
</p>
<pre name="code" class="python">
#/etc/gunicorn.d/django
CONFIG = {
'mode': 'django',
'working_dir': '/opt/my-django-project',
'user': 'django',
'group': 'django',
# stupid hack to get new relic to load in Django 1.3
# once we move to 1.4 we should use straight WSGI
'python': '/opt/my-django-project/conf/newrelic.sh',
'environment': {
'NEW_RELIC_CONFIG_FILE': '/etc/newrelic/newrelic.ini',
},
'args': (
'--bind=0.0.0.0:80',
'--workers=4',
'--backlog=0',
'--worker-class=sync', # gevent doesn't work smoothly with new relic yet
'--preload',
),
}
</pre>
<p>
Notice the hack to inject the new relic runner script. I have over-ridden the python executable path to point to a third script, newrelic.sh.
</p>
<pre name="code" class="bash">
#!/usr/bin/env bash
/usr/local/bin/newrelic-admin run-program python $*
</pre>
<p>
This script just takes whatever arguments are passed in ($*), and passes them as arguments to newrelic-admin. That's it, a service gunicorn restart should start data flowing into new relic.
</p>