Elastic APM Python Agent

6.25.0 · active · verified Sat Apr 11

The official Python module for Elastic APM. It provides full out-of-the-box support for many popular frameworks, including Django and Flask, and can be adapted for most WSGI-compatible web applications or used in any standalone Python application. It monitors applications in real-time, collecting performance metrics, tracing transactions, and capturing errors. Currently at version 6.25.0, it sees regular updates focusing on framework compatibility and performance.

Warnings

Install

Imports

Quickstart

This Flask example demonstrates basic setup with environment variable configuration, capturing a custom message, and an exception. It also shows how to ignore specific routes from tracing using `TRANSACTIONS_IGNORE_PATTERNS`. Ensure an Elastic APM Server is running and accessible at the configured `SERVER_URL`.

import os
from flask import Flask
from elasticapm.contrib.flask import ElasticAPM

app = Flask(__name__)

app.config['ELASTIC_APM'] = {
    'SERVICE_NAME': os.environ.get('ELASTIC_APM_SERVICE_NAME', 'my-flask-app'),
    'SERVER_URL': os.environ.get('ELASTIC_APM_SERVER_URL', 'http://localhost:8200'),
    'ENVIRONMENT': os.environ.get('ELASTIC_APM_ENVIRONMENT', 'development'),
    'CAPTURE_HEADERS': True,
    'TRANSACTIONS_IGNORE_PATTERNS': ['^/healthcheck']
}

apm = ElasticAPM(app)

@app.route('/')
def hello_world():
    apm.client.capture_message('Hello World request received!')
    try:
        1 / 0
    except ZeroDivisionError:
        apm.client.capture_exception()
    return 'Hello, World!'

@app.route('/healthcheck')
def healthcheck():
    return 'OK'

if __name__ == '__main__':
    # Set environment variables or ensure APM server is running at localhost:8200
    # For example: ELASTIC_APM_SERVICE_NAME=my-service ELASTIC_APM_SERVER_URL=http://localhost:8200 python your_app.py
    app.run(debug=True)

view raw JSON →