How to setup monitoring for your Django app using New Relic

Introduction

In this short guide, you will learn how to set app monitoring for your Django app using New Relic.

Prerequisites

To complete this tutorial, you will need:

  • Django Starter Project
  • New Relic Account (Free Forever) - Sign up here

Setup the python agent.

Install the New Relic Python agent. See more detailed instructions here.

# using pip
$ pip install newrelic

# add it to your requirements.txt file and specify the package version
...
new_relic==n.n.n.n

Configuration

Next, you will add the newrelic.ini config file. The config file allows you to specify custom behaviour for the Python agent.

You can generate a config file using the command below.

# this will generate a new config file.
# you can retrieve YOUR_LICENSE_KEY from the dashboard when you sign up.

newrelic-admin generate-config YOUR_LICENSE_KEY newrelic.ini

If you control how your application is started you will have to modify your startup command to include the newrelic_admin command before your command options. Some practical examples are below.

# generic 
NEW_RELIC_CONFIG_FILE=newrelic.ini newrelic-admin run-program YOUR_COMMAND_OPTIONS

# example - For deployments to Heroku modify the procfile.
web: newrelic-admin run-program bin/start-pgbouncer-stunnel gunicorn config.asgi:application -k uvicorn.workers.UvicornWorker

# docker file example 
CMD NEW_RELIC_CONFIG_FILE=newrelic.ini newrelic-admin run-program gunicorn -b :$PORT main:app
 

Under the hood, the agent intercepts incoming /outgoing events to and from your ASGI / WSGI callable.

Restart your application

Once you restart your application, wait for 5 minutes, you should be able to see some action on the dashboard.

New Relic monitoring dashboard

Conclusion

That took less than ten minutes, but it could save you lots of debugging time later on. The dashboard has a host of other tools and options that we will be exploring in a more detailed post later.

Copyright © 2022 www.advantch.com