Python Flask uWSGI
Prerequisites
Python 3.6+
Installation
-
Install dependencies:
pip install "elastic-apm[flask]" -
Add the highlighted lines below to your project's
app.pyfile:import requests
import socket
from flask import Flask
from elasticapm.contrib.flask import ElasticAPM
app = Flask(__name__)
apm = ElasticAPM(app) -
Configure the agent.
- Environment Variables
- Code
ELASTIC_APM_SERVICE_NAME=<app_name>
# send traces to CubeAPM
ELASTIC_APM_SERVER_URL=http://<ip_address_of_cubeapm_server>:3130
# optional settings
ELASTIC_APM_ENVIRONMENT=myenv
ELASTIC_APM_SERVICE_VERSION=1.2.3
ELASTIC_APM_GLOBAL_LABELS=mykey1=myvalue1,mykey2=myvalue2import requests
import socket
from flask import Flask
from elasticapm.contrib.flask import ElasticAPM
app = Flask(__name__)
app.config['ELASTIC_APM'] = {
'SERVICE_NAME': '<app_name>',
# send traces to CubeAPM
'SERVER_URL': 'http://<ip_address_of_cubeapm_server>:3130',
# optional settings
'ENVIRONMENT': 'myenv',
'SERVICE_VERSION': '1.2.3',
'GLOBAL_LABELS': 'mykey1=myvalue1,mykey2=myvalue2',
}
apm = ElasticAPM(app)
Sample Application
A working example is available at https://github.com/cubeapm/sample_app_python_flask_uwsgi/tree/elastic.
Troubleshooting
The following can be used for debugging:
# Print Elastic agent logs on screen
ELASTIC_APM_LOG_FILE=stdout
# Set Elastic agent log level to debug if needed to see detailed logs.
# Refer to Sample Application for additional requirements.
ELASTIC_APM_LOG_LEVEL=debug