Skip to main content

Lightweight APM for Flask that sends metrics to Watchlog Agent

Project description

APM for Flask – Watchlog Integration

🎯 Lightweight and production-safe Application Performance Monitoring (APM) middleware for Flask apps, made for Watchlog.

Tracks route execution time, status codes, memory usage, and errors — and sends them periodically to your Watchlog agent in aggregated form.


🚀 Features

  • 🔧 Automatic tracking of all Flask routes
  • 📊 Aggregation of metrics by path and method
  • ⚠️ Optional error tracking
  • 🌐 Sends metrics to Watchlog agent over HTTP
  • 🧠 Captures memory usage (rss, vms)
  • 💡 Safe-by-default (never crashes your app)
  • 🏷️ Auto-detects route like /users/<int:id>

📦 Installation

pip install flask_watchlog_apm

⚙️ Usage

Here’s a complete example:

from flask import Flask
from flask_watchlog_apm import patch_flask_app, apm_error_handler, start

app = Flask(__name__)

# Auto-instrument all routes
patch_flask_app(app, service="my-flask-service")

# Enable automatic error tracking
apm_error_handler(app, service="my-flask-service")

# Start background sender
start(agent_url="http://localhost:3774/apm", interval=10)




@app.route("/user/<int:user_id>")
def get_user(user_id):
    return f"User {user_id}"

@app.route("/fail")
def fail():
    raise Exception("error")

if __name__ == "__main__":
    app.run(debug=True)

🛠️ Service Name

You can define your service name like this:

patch_flask_app(app, service="my-service")
apm_error_handler(app, service="my-service")

Or set it in environment variables:

export WATCHLOG_APM_SERVICE=users-api

Fallbacks to APP_NAME or defaults to flask-app.


📤 What gets sent

Example payload every 10 seconds:

{
  "collected_at": "2025-05-18T12:00:00Z",
  "platformName": "flask",
  "metrics": [
    {
      "type": "aggregated_request",
      "service": "users-api",
      "path": "/user/<int:user_id>",
      "method": "GET",
      "request_count": 3,
      "error_count": 1,
      "avg_duration": 9.2,
      "max_duration": 13.4,
      "avg_memory": {
        "rss": 13434880,
        "heapUsed": 13930496,
        "heapTotal": 13930496
      }
    }
  ]
}

📁 Recommended .gitignore

/storage/logs/apm-buffer.json
/storage/framework/cache/watchlog-apm.lock

✅ Notes

  • Uses request.url_rule.rule to extract route pattern like /user/<int:id>
  • All routes are auto-wrapped using patch_flask_app
  • Error tracking is optional but recommended
  • Metrics are batched in a local file and flushed every 10 seconds

📝 License

MIT © Mohammadreza
Built for Watchlog.io

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_watchlog_apm-0.1.5.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_watchlog_apm-0.1.5-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file flask_watchlog_apm-0.1.5.tar.gz.

File metadata

  • Download URL: flask_watchlog_apm-0.1.5.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for flask_watchlog_apm-0.1.5.tar.gz
Algorithm Hash digest
SHA256 0fbbba80c78716d7d5864bf4a39b280e254e208d253c0604508a6901fb7a8eab
MD5 2fe7ef2a2910b5e0fa67c6bb94c5f58a
BLAKE2b-256 f47d01825701cef382e2044cda3d89c64fb6cbe3572bfde63acb94169064a5d6

See more details on using hashes here.

File details

Details for the file flask_watchlog_apm-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_watchlog_apm-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d5b06e355d7273d7e700f975e11cd03f1ad98defde430617cf063f25e1835e8d
MD5 6e99514d9e2766838340ed3e2d3dd1b0
BLAKE2b-256 da700864b13387d982365433b9f292232b7c62e39a48858ce3351f13442ea0b9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page