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.2.0.tar.gz (6.2 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: flask_watchlog_apm-0.2.0.tar.gz
  • Upload date:
  • Size: 6.2 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.2.0.tar.gz
Algorithm Hash digest
SHA256 abf5edd460a604ebeeec6cb75ed34665c22b5df4d8eb53d15af378cd2ef06fd2
MD5 80c8f7c637126c62b44585d96b2e1d4e
BLAKE2b-256 1f18c5c17155e653f474b10ec6c120fc22c78cf52272a0303565f2aca1848932

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