Monitor Heroku Apps with New Relic

Example with Flask and Gunicorn

November 01, 2021 · 8 mins read

Monitoring Heroku Apps with New Relic

For this example, I have a simple Flask application called datacrunch-consulting that is deployed to Heroku via GitHub integration. Starting with GitHub, you can create a repository. Then, use GitHub Desktop to clone/push code back to GitHub without using the command line. Linking the repository with Heroku allows the application to deploy every time a push is made to your repository, great for continuous integration / continus deployment.

See GitHub repository here: or use your own Heroku app.

We’ll need to make some adjustments to your Heroku Procfile:

newrelic-admin run-program gunicorn -b "$PORT" -w 3 --chdir datacrunch-consulting webserver:flaskapp

We’ll go over how to setup the following in New Relic One:

  1. Application Performance Monitoring (APM) for golden signals
  2. Browser for Real User Monitoring (RUM)
  3. Synthetics ping check for application uptime
  4. Logs for distributed tracing
  5. Alerts to notifiy when threshold conditions are met

New Relic APM Setup

  1. Log into Heroku and verify your account by adding a payment method
  2. Go to Heroku Add-ons and select New Relic APM
  3. Add-on plan: Wayne - Free
  4. App to provision to datacrunch-consulting
  5. Click Submit Order Form Heroku Screenshot 1
  6. Wait for the add-on to install. Note: it’ll create an account for you, even you already have one. We’ll need to configure the environment variables to send APM data to the correct account.
  7. In your Heroku dashboard, click on the the app with the New Relic APM add-on
  8. Click on Settings > Config Vars > Reveal Config Vars
  9. You should see at least three (3) configuration variables. If not, add them manually:
    • NEW_RELIC_LOG Heroku Screenshot 2
  10. Edit the NEW_RELIC_APP_NAME to give your application a name. This is what shows up in New Relic.
    You also can edit this in New Relic One > APM > Settings > Application > Application Settings > Application alias
  11. Edit NEW_RELIC_LICENSE_KEY to have the license key for the New Relic account you want data ingested into
  12. Restart your application by toggling off/on your dynos in Heroku > Resources
  13. Go to your deployed Heroku app and log into New Relic One and check APM for data ingested. Heroku Screenshot 3

New Relic Browser Setup

With the APM agent installed on Heroku, you can get Real User Monitoring to track browser sessions, which browser is being used, which landing page the user is getting errors (if any), and geographical location of your users.

  1. In New Relic One, click Add more data on the top-right and select ` Browser Metrics`
  2. Select Enable via New Relic APM and select your application from the selection box. Heroku Screenshot 4
  3. Once enabled, go to your Heroku app to get data ingested. Note: Some adblockers prevent JavaScript from running in the background. Heroku Screenshot 5

New Relic Synthetics Setup

Heroku apps on the free tier shuts down the app after 30 minutes of inactivity. When someone accesses the Heroku app after inactivity, it will cold start and may take a minute to load. Having a simple synthetics ping test every 15 minutes will keep the Heroku app up to avoid “cold starts”.

  1. Go to New Relic One > Synthetics
  2. Add monitor by giving it a name
  3. For Text validation (optional), add a string Welcome! (or whatever string that appears on your endpoint)
  4. Advanced options: enable Verify SSL to check for expired certificates
  5. Check 1 location, every 15 minutes Heroku Screenshot 6
  6. Wait for your Synthetics ping check to get back some data to see uptime info Heroku Screenshot 7

New Relic Logs Setup (Command Line)

General Instructions here

  1. Download and install the Heorku CLI:

  2. Check if Heroku is installed:
    heroku --version

  3. Update Heroku CLI as needed:
    heroku update

  4. Log in:
    heroku login Heroku Screenshot 13

  5. Check Environment variables, in case you can’t get to Heroku Dashboard > Settings:
    PowerShell: heroku run env --app datacrunch-consulting | findstr -i NEW_RELIC Heroku Screenshot 12

  6. If you need to set environment variables from the command line:
    heroku config:set NEW_RELIC_LOG='stdout' --app datacrunch-consulting
    heroku config:set NEW_RELIC_APP_NAME='flaskapp.heroku' --app datacrunch-consulting
    heroku config:set NEW_RELIC_LICENSE_KEY='a1b2c3d4e5f6g7h8i9j0NRAL' --app datacrunch-consulting
  7. Add Drain Logs
    heroku drains:add syslog+tls:// --app datacrunch-consulting

  8. Copy drain token:
    heroku drains --app datacrunch-consulting --json Heroku Screenshot 11

  9. Add drain token in New Relic Heroku Screenshot 8

  10. Add tokens and restart Heroku Dynos: Heroku Dashboard > More > Restart all Dynos or:
    heroku dyno:restart --app datacrunch-consulting

  11. Go to New Relic One > Logs > Patterns to see logs, and any aggregated log messages based on patterns: Heroku Screenshot 9

  12. You can also Query your data with NRQL to set up alerts based on log messages Heroku Screenshot 10

New Relic Alerts Setup

  1. In New Relic One, click on Alerts & AI > Notification Channels
  2. On the top-right, click New notification channel and add a notification channel of your choice Heroku Screenshot 14
  3. Next, go to Alerts & AI > Policies > New alert policy
  4. Git your Alert Policy a Name Create alert policy Heroku Screenshot 15
  5. Click on Create a condition
  6. Alerts for APM, Browser, and Synthetics are straight-forward and have selections to choose from. We’ll work with NRQL alerts then click Next, define thresholds
  7. The NRQL query we’ll use for alerting will be based on any log messages from Heroku that contain “Shutting down” at least once in 5 minutes. This will send an alert when the app shuts down on Heroku for whatever reason.
    SELECT count(*) from Log where plugin.type='syslog-heroku' and message like '%Shutting down%'

    Heroku Screenshot 16