Instrumenting Python App with New Relic Agent

This tutorial demonstrates how to integrate the New Relic monitoring agent in a Python application running on Apcera. To complete this tutorial you will need to do the following:

Steps to integrate New Relic agent:

  1. Open a terminal window and cd to the example-python-wsgi sample app directory:

     cd sample-apps/example-python-wsgi/
  2. Open example-python-wsgi/requirements.txt and append newrelic to the list of requirements, for example:

  3. Using the newrelic-admin utility create a New Relic configuration file for your application, passing it your license key, for example:

     newrelic-admin generate-config <LICENSE-KEY> newrelic.ini

    This will create a file name named newrelic.ini in the app directory. See Creating the configuration file for more information.

  4. Modify in the sample app folder to import the newrelic.agent package and to initialize the agent, as shown below:

     # Add newrelic.agent to imports:
     import os, newrelic.agent
     from flask import Flask
     app = Flask(__name__)
     # Initialize agent:

    Save your changes to

  5. Use APC to create a new Apcera application from the Python app. The --allow-egress option is necessary to allow the New Relic monitoring agent to communicate with the New Relic servers:

     apc app create newrelic-agent --allow-egress

    See Binding to an outside network for more information about accessing network services outside of Apcera.

  6. Once the application has been created, start it with the following command:

     apc app start newrelic-agent

Once the application has started successfully, open it at the indicated URL (for example, "App should be accessible at ""). Reload the web page repeatedly in the browser to generate some data and locate your application on the New Relic dashboard. After a few minutes data should begin appearing in the dashboard. If not, see Troubleshooting Tips.

New Relic Dashboard

Troubleshooting Tips

If no data appears in the New Relic dashboard, you can enable logging on the New Relic agent and view the log output. You can configure the agent to log to a file or to standard error (stderr) or standard out (stdout). A benefit of logging to one of the standard streams is that those log statements can easily be viewed using APC.

Steps to enable and view New Relic agent logs:

  1. Modify newrelic.ini to enable logging to a file or standard out.
  2. Re-deploy the application with the configuration change:

     apc app deploy newrelic-agent

If you configured the agent to log items to stderr (or stdout), you can tail the application's log file using the following APC command:

apc app logs newrelic-agent

[stderr] 2015-08-18 17:34:43 [6] [DEBUG] GET /
[stdout] 2015-08-18 17:34:43,148 (6/NR-Activate-Session/Test App) newrelic.core.data_collector INFO - Successfully registered New Relic Python agent where app_name='Test App', pid=6, redirect_host=u'' and agent_run_id=u'33510150964740125', in 0.55 seconds.
[stdout] 2015-08-18 17:34:43,149 (6/NR-Activate-Session/Test App) newrelic.core.data_collector INFO - Reporting to:

If you instead configured the agent to log items to a file you can SSH into the instance to view the file's contents. To do this, you first need to enable SSH access on your application:

apc app update newrelic-agent --allow-ssh

Restart the job when prompted by APC, and use the following commands to SSH into the instance and view the generated log file (/tmp/newrelic-python-agent.log, by default).

apc app connect newrelic-agent
root@ip-169-254-0-7:/# cat /tmp/newrelic-python-agent.log
2015-08-18 20:01:27,266 (10/MainThread) newrelic.core.agent INFO - New Relic Python Agent (
2015-08-18 20:01:27,842 (10/NR-Activate-Session/Test App) newrelic.core.data_collector INFO - Successfully registered New Relic Python agent where app_name='Test App', pid=10, redirect_host=u'' and agent_run_id=u'33510150965268109', in 0.54 seconds.
2015-08-18 20:01:27,842 (10/NR-Activate-Session/Test App) newrelic.core.data_collector INFO - Reporting to: