I just finished setting up New Relic application monitoring on Recommendify. It was a little bit of a painful process and it wasn’t properly described in their docs, so I’m going to note the steps I went through here. Hopefully it will help somebody else in the same situation.
Install the New Relic agent
First, install the
newrelic package using
pip install newrelic
Generate the New Relic configuration
Then copy the command to generate the configuration file from your New Relic dashboard. It will look something like this:
newrelic-admin generate-config LICENSE-KEY newrelic.ini
LICENSE-KEY will be the specific license key for your account.
This command will create a file called
newrelic.ini. Copy it somewhere safe on your server and make sure it has permissions to be read by the user Apache runs under.
The next step took some time to figure out. New Relic provides a wrapper script for Python applications, but it doesn’t work for setups using embedded interpreters, such as Apache with
mod_wsgi. As this is how Recommendify is configured, I had to find another way.
I edited the
wsgi.py file from my Django project to wrap the WSGI application object.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
if socket.gethostname() == "server-hostname":
application = newrelic.agent.WSGIApplicationWrapper(application)
This script imports the
socket module and used the
socket.gethostname() function to get the hostname of the current machine. I did this so that New Relic would only log data in production and not during development.
At the bottom of the script, I check if the application is running on the production environment. If it is, I import
newrelic.agent, initialize it by passing it the path to the
newrelic.ini created during the previous step, then wrap the WSGI
application object using the
You will need to use your own Django settings module, hostname string, and path to
newrelic.ini, of course.
Once you deploy the modified
wsgi.py, you should start seeing monitoring information in New Relic after about five minutes. Here’s what it looks like for me.
Download Mastering Decorators
Enjoyed this article? Join the newsletter and get Mastering Decorators - a gentle 22-page introduction to one of the trickiest parts of Python.