How to integrate New Relic (APM) to Django?
Application Performance Monitoring (APM) is a must-have tool for any of the applications which are live for external users. I prefer using NewRelic as an APM for all of my applications. Before jumping into the installation please create a new account with NewRelic.
Let us look into the basic integration of NewRelic, an Application Performance Monitoring (APM) for Django and Celery.
- Install NewRelic via in the virtual environment
pip install newrelic
- Once installed, you need to add a config file for NewRelic to pick up all the config. You can get the configuration from the below link.
Here is a basic minimalistic config file that I use for most of my application
- Create an
newrelic.inithe file inside the project directory where your
- Follow the next steps if you are using
systemdservice to deploy your application in production.
You have to configure 3 entries from the above service config file
Environment="NEW_RELIC_CONFIG_FILE=<newrelic.ini file absolute path>"Add the path where you created the
newrelic.inifile. This is helpful for the NewRelic agent to pick config.
Environment="NEW_RELIC_ENVIRONMENT=production"- Pass the name which you have added in the newrelic file,
[newrelic:production], this is used to identify your application and segregate it. For multiple environments you should copy the section and rename it, for example, we use celery application in the same code base but we want to capture the data of celery in a different section so we shall name it
[newrelic:celery_production]and pass it in the environment,
NEW_RELIC_ENVIRONMENT=celery_productionthen any data captured in celery can be easily identified.
WorkingDirectory=<your code path>Add the path to your code, this is something that you already would have done while using
ExecStart=/<virtual env path>/bin/newrelic-admin run-program /<virtual env path>/bin/gunicorn - --workers 3 --bind unix:/<project path same as gunicorn>/backend/backend.sock backend.prod_wsgi:application- Add the virtual env path and then
newrelic-admin run-programfollowed by the gunicorn settings which you generally use.
The most important part here is
newrelic-admin run-programonce your newrelic is working as expected then everything is same as what you do with systemd and gunicorn
gunicorn with systemd follow the below guide.
For Celery and Celery beat the configuration is the same with just 2 changes
Environment="NEW_RELIC_ENVIRONMENT=celery_beat_production"- Change the application name so that you can identify the data separately
ExecStart=/<virtualenv path>/bin/newrelic-admin run-program /<virtualenv path>/bin/celery -A backend beat -l info- In place of
gunicornyou would be serving the application using
Once NewRelic is configured, you can start accessing the data via NewRelic One dashboard. Here are few points which I have noticed, which can help you work better
Policiesto get better insight when your service is performing badly.
Notificationto send alerts into the slack channel so that developers can get insights during any degradation of performance of the application.
- NewRelic One plan is costly and you might not yet be there to pay for the plan, so what you can do is, use a single user account and share the credentials among different developers.
- Make sure your application name is in the
inithe config file is proper because once you add an application to NewRelic the name cannot be updated, every new name is considered as a new application.