Let's monitor a Python app with New Relic's APM. Python has a wide variety of frameworks and a wide range of publishing methods on the Web, but first I would like to introduce how to link New Relic and Python applications in the simplest possible way. This time, we will check the flow of embedding the New Relic code in a simple application made with Flask and displaying it on the monitoring screen.
CentOS 7 Python 2.7.5(default) It is assumed that you have a New Relic account. Please read LICENSE-KEY as appropriate.
Official Follow the steps to install.
An example of my environment. It's a little old, so Click here for the latest version.
# wget https://download.newrelic.com/python_agent/release/newrelic-2.8.0.7.tar.gz
# tar xvzf newrelic-2.8.0.7.tar.gz
# cd newrelic-2.8.0.7
# python setup.py install
This is easier if you just give it a try. The effort may not change much ...
pip install newrelic
or
easy_install newrelic
You can configure it by executing the following command at any location. There is no problem if you move the config file.
newrelic-admin generate-config LICENSE-KEY newrelic.ini
Modify the contents of the config. I changed the app name to "New Relic Hello!". This will be the app name on the New Relic screen. I uncommented it so that log can be output. Only the corrected points are excerpted below.
app_name = New Relic Hello!
log_file = /tmp/newrelic-python-agent.log
Check if the Python Agent can be executed with the following command.
newrelic-admin validate-config newrelic.ini
After a few seconds, you will see the name "Python Agent Test" on the APM screen. It doesn't matter how many times you run it. It can be used to isolate troubleshooting in the future process.
# easy_install Flask
An app that just displays New Relic Hello !.
# cat flask_hello.py
import newrelic.agent
newrelic.agent.initialize('/root/newrelic-2.8.0.7/newrelic.ini')
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "New Relic Hello!"
if __name__ == '__main__':
app.run(host='0.0.0.0')
#
The top two lines are the embed code for New Relic. Specifies the location of newrelic.ini.
import newrelic.agent
newrelic.agent.initialize('/root/newrelic-2.8.0.7/newrelic.ini')
Open two terminals.
Do the following: "0.0.0.0" is a special IP address that represents this host on this network. If you do not specify the port number in the code, it will be 5000 at runtime.
# python flask_hello.py
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
Do the following: You can specify the address of your own server instead of 127.0.0.1.
# curl http://127.0.0.1:5000
New Relic Hello!
You can use the watch command to see the access every second.
watch -n 1 "curl http://127.0.0.1:5000"
If it is a development environment, it may be a good idea to daemonize the execution of the application with supevisor. Or will it work with Apache or Nginx? It's a matter of consultation with time and effort.
It came up with "New Relic Hello!".
It is an application that displays only New Relic hello !, but you can see that you can also see the function name and WSGI information.
that's all
Recommended Posts