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




@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.6.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.6-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_watchlog_apm-0.1.6.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.6.tar.gz
Algorithm Hash digest
SHA256 b7ad7fed23e96fb4b0c61b7496a773e502328a91fc132d1546284ca683a76b69
MD5 3e76d7356cc08f6e4c2f6b04dd5ebe24
BLAKE2b-256 323baaffac828bdcaf9833af2da833a48413a452ed6e63ce727eb0c5c9eb23db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_watchlog_apm-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 5bdd35ff384ff246a9cdcd5787800b26c29db92c6041c49a4c75d759b8d41940
MD5 71f7419ad3df01867a44e3d60fba2024
BLAKE2b-256 5a780a92ccf194d1eb80f28dacdaeb4e08cf10af61f6976a6066b9702be55a9a

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