Python Flask Gunicorn
Installation
Following are the steps to install the New Relic agent and connect it with CubeAPM. If New Relic agent is already installed, you can jump to step 5.
-
Install dependencies:
pip install newrelic
-
(Optional) From
lib/pythonx.xx/site-packages/newrelic
, copynewrelic.ini
into the root directory of your application. -
Configure the agent.
- newrelic.ini
- Environment Variables
[newrelic]
app_name = <app_name>
license_key = ABC4567890ABC4567890ABC4567890ABC4567890NEW_RELIC_APP_NAME=<app_name>
NEW_RELIC_LICENSE_KEY=ABC4567890ABC4567890ABC4567890ABC4567890 -
Add the highlighted lines below to your project's
app.py
file:- newrelic.ini
- Environment Variables
app.pyfrom flask import Flask
import newrelic.agent
app = Flask(__name__)
newrelic.agent.initialize("newrelic.ini")app.pyfrom flask import Flask
import newrelic.agent
app = Flask(__name__)
newrelic.agent.initialize() -
Tell the agent to connect with CubeAPM instead of New Relic:
- newrelic.ini
- Environment Variables
[newrelic]
app_name = <app_name>
license_key = ABC4567890ABC4567890ABC4567890ABC4567890
host = <domain_of_cubeapm_server>NEW_RELIC_APP_NAME=<app_name>
NEW_RELIC_LICENSE_KEY=ABC4567890ABC4567890ABC4567890ABC4567890
NEW_RELIC_HOST=<domain_of_cubeapm_server>tipSee Using CubeAPM with New Relic agents for details on how to set up
<domain_of_cubeapm_server>
.
Sample App
A working example is available at https://github.com/cubeapm/sample_app_python_flask_gunicorn/tree/newrelic
Troubleshooting
The following can be used for troubleshooting:
- newrelic.ini
- Environment Variables
[newrelic]
# Print New Relic agent logs on screen
log_file = stdout
# Set New Relic agent log level to debug if needed to see detailed logs
log_level = debug
# Print New Relic agent logs on screen
NEW_RELIC_LOG=stdout
# Set New Relic agent log level to debug if needed to see detailed logs
NEW_RELIC_LOG_LEVEL=debug