Monitor Python apps with New Relic's APM (Flask)

Introduction

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.

environment

CentOS 7 Python 2.7.5(default) It is assumed that you have a New Relic account. Please read LICENSE-KEY as appropriate.

New Relic APM setup

Install Python Agent

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

Config (newrelic.ini) generation

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 config

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

Checking the operation of Python Agent

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.

01.jpg

Flask setup

Installing Flask (Reference)

# easy_install Flask

App creation

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')

Operation check

Open two terminals.

Terminal 1

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)

Terminal 2

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.

New Relic screen

It came up with "New Relic Hello!".

02.jpg

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.

03.jpg

that's all

Recommended Posts

Monitor Python apps with New Relic's APM (Flask)
Monitor Python web apps with Prometheus
Programming with Python Flask
Web application with Python + Flask ② ③
Web application with Python + Flask ④
SNS Python basics made with Flask
[Python] Use Basic/Digest authentication with Flask
Monitor Python application performance with Dynatrace ♪
Application development with Docker + Python + Flask
Getting Started with Flask with Azure Web Apps
POST variously with Python and receive with Flask
Page cache in Python + Flask with Flask-Caching
Develop Windows apps with Python 3 + Tkinter (Preparation)
Easy web app with Python + Flask + Heroku
[Python] Quickly create an API with Flask
Flask can't be RESTful with azure API Apps
Vienna with Python + Flask web app on Jenkins
Develop Windows apps with Python 3 + Tkinter (exe file)
[Improved version] Script to monitor CPU with Python
Launch a web server with Python and Flask
Create a new page in confluence with Python
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
Flow to complete Slack authentication with Flask (Python)
I made a Mattermost bot with Python (+ Flask)
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Integrate with Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Create an animated GIF local server with Python + Flask
Parse and visualize JSON (Web application ⑤ with Python + Flask)
How to upload with Heroku, Flask, Python, Git (Part 1)
A server that echoes data POSTed with flask / python
How to upload with Heroku, Flask, Python, Git (Part 2)