29 May 2015, 17:23

Integrating New Relic with Python, Flask, and uWSGI Emperor

I recently needed to integrate one of my Python Flask apps with New Relic to get some monitoring insight. The app is being served via uWSGI running in Emperor mode. The docs available from New Relic weren’t particularly clear to me so it took me a little while longer than it should have, but I got it ironed out.

I’m going to assume that if you’re trying to do this, you’ve already got uWSGI running in Emperor mode and that your vassals are configured.

First, ensure that the New Relic module is installed and that your newrelic.ini is created correctly (New Relic’s docs are fine for this) and that it resides in a location and has permissions where your app can read it. I like to leave mine in the root of my application.

Second, ensure that your vassal is configured correctly for New Relic to work. There are two required settings for New Relic; enable-threads = true and single-interpreter = true. I didn’t have any luck with using the eval or env configuration options presented by New Relic, so those aren’t present here. My dev vassal looks like this:

[uwsgi]                                                                                                                                                                                                   
plugins = python                                                                                                                                                                                          
chdir = /path/to/myapplication                                                                                                                        
socket = /path/to/myapplication/tmp/uwsgi.sock                                                                                                                             
uid = www-data                                                                                                                                                                                            
gid = www-data                                                                                                                                                                                            
enable-threads = true                                                                                                                                                                                     
single-interpreter = true                                                                                                                                                                                 
module = myapplication:app                                                                                                                                                                                  
chmod-socket = 666                                                                                                                                                                                        
logto = /var/log/uwsgi/dev.myapplication.log                                                                                                                                                                
catch-exceptions = true                                                                                                                                                                                   
py-reload = 2

At this point, you should be able to restart uWSGI and effectively see no change in behavior.

Because of the previously mentioned issues using env and eval to get this all running, my New Relic setup is actually part of the app itself, which I actually prefer. The magic bits to get this to work inside of your app are:

import newrelic.agent                                                                                                                                                                                 
newrelic.agent.initialize('./newrelic.ini') 

I have these immediately after the rest of the imports in my app. Note that this obviously assumes that you have the newrelic.ini in the same place that I do – right next to the application.

Now if you restart uWSGI, make a few requests to your app, and wait a few minutes, you should start getting metrics reported to New Relic.

comments powered by Disqus