Integrating New Relic APM with UWSGI

Kunal Yadav
Level Up Coding
Published in
3 min readJun 4, 2020

--

Credits — scnsoft.com

An APM (Application Performance Management) is used to monitor and manage the performance and availability of software applications.

You can use it to figure out things like — time taken by an API, the time taken to run a database query, the time taken to run a function, etc.

To improve the performance of a system you first need to measure the performance in some way and APM can be used to do just that.

There are different APMs available in the market but here we will be discussing how to integrate New Relic APM with UWSGI for your Django/Flask Applications, so let’s get started.

Install the New Relic python library

pip install newrelic

Create a New Relic configuration file

You can use the following command to generate a New Relic configuration command, just replace YOUR-LICENSE-KEY with your account’s license key.

newrelic-admin generate-config YOUR-LICENSE-KEY newrelic.ini

Define Environments

Now, it’s time to define the different environments that you would like to monitor. Examples of this could be — production app servers, production celery servers, staging, etc.

To do so, open the newrelic.ini file and scroll to the bottom. You should see some dummy environments listed like development and test.

Here you can define your environments like this:

[newrelic:production-app]
app_name = myapp (production)
monitor_mode = true
[newrelic:production-celery]
app_name = myapp (celery)
monitor_mode = true

Here, production-app is what you will pass to your UWSGI config as the environment variable NEW_RELIC_ENVIRONMENT

app_name is the name by which you will see your application in the New Relic APM dashboard and monitor_mode indicates whether New Relic will send the data about your app to the New Relic collector.

Now that you are done with the New Relic configuration it’s time to make changes to your UWSGI config. Here I am covering the integration of two popular ways of running UWSGI.

Using systemd

You need to make two changes in your systemctl configuration.

uwsgi.service file for systemd

In the Environment option, you need to define two environment variables as done in the above config file:

  1. NEW_RELIC_CONFIG_FILE — Absolute path to the New Relic configuration file.
  2. NEW_RELIC_ENVIRONMENT — Name of the environment as specified in the configuration file.

In the ExecStart option, you just need to prepend your UWSGI command with the newrelic-admin run-program

Now, since this is a systemctl configuration, so you need to give the absolute path to the newrelic-admin (New Relic admin is available as a binary file once you install New Relic with pip).

Now reload the configuration with the following command and restart your uwsgi service:

sudo systemctl daemon-reload
sudo systemctl restart uwsgi

Using supervisor

Like systemctl here also you need to make two changes.

uwsgi.conf file for Supervisor

In the environment option, you need to define two environment variables as done in the above config file — NEW_RELIC_CONFIG_FILE and NEW_RELIC_ENVIRONMENT

In the command option, you just need to prepend your UWSGI command with the newrelic-admin run-program (specify absolute path)

Now restart supervisor with the following command:

sudo systemctl restart supervisor

You can also use the supervisorctl command.

It may take 15–20 minutes to see the data in the New Relic APM dashboard. If you don’t receive any data then do check the UWSGI logs to debug.

If you run celery with the supervisor then you can follow the same steps of the supervisor config (define environment variables and prepend New Relic Admin) and it should work perfectly.

I hope you are able to configure New Relic and didn’t spend two hours struggling like me :P
If you are facing some issues, do let me know in the comments!

Thanks a lot for reading this article. If you liked it, please give a few claps so it reaches more people who would benefit from it!

--

--

Product Engineer at Intercom, AWS Certified Professional who loves Cloud and reading books.