Monitor Heroku Apps with New Relic (2024)

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: https://github.com/pnvnd/flaskapp or use your own Heroku app.

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

newrelic-admin run-program gunicorn -b "0.0.0.0:$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 FormMonitor Heroku Apps with New Relic (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_APP_NAME
    • NEW_RELIC_LICENSE_KEY
    • NEW_RELIC_LOGMonitor Heroku Apps with New Relic (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.Monitor Heroku Apps with New Relic (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.Monitor Heroku Apps with New Relic (4)
  3. Once enabled, go to your Heroku app to get data ingested. Note: Some adblockers prevent JavaScript from running in the background.Monitor Heroku Apps with New Relic (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 minutesMonitor Heroku Apps with New Relic (6)
  6. Wait for your Synthetics ping check to get back some data to see uptime infoMonitor Heroku Apps with New Relic (7)

New Relic Logs Setup (Command Line)

General Instructions here

  1. Download and install the Heorku CLI:
    https://devcenter.heroku.com/articles/heroku-cli

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

  3. Update Heroku CLI as needed:
    heroku update

  4. Log in:
    heroku loginMonitor Heroku Apps with New Relic (8)

  5. Check Environment variables, in case you can’t get to Heroku Dashboard > Settings:
    PowerShell: heroku run env --app datacrunch-consulting | findstr -i NEW_RELICMonitor Heroku Apps with New Relic (9)

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

  8. Copy drain token:
    heroku drains --app datacrunch-consulting --jsonMonitor Heroku Apps with New Relic (10)

  9. Add drain token in New RelicMonitor Heroku Apps with New Relic (11)

  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:Monitor Heroku Apps with New Relic (12)

  12. You can also Query your data with NRQL to set up alerts based on log messagesMonitor Heroku Apps with New Relic (13)

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 choiceMonitor Heroku Apps with New Relic (14)
  3. Next, go to Alerts & AI > Policies > New alert policy
  4. Git your Alert Policy a Name Create alert policyMonitor Heroku Apps with New Relic (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%'

    Monitor Heroku Apps with New Relic (16)

← Previous Post Next Post →

Monitor Heroku Apps with New Relic (2024)
Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 6241

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.