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

Uploaded Source

File details

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

File metadata

  • Download URL: flask_watchlog_apm-0.1.9.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.1.9.tar.gz
Algorithm Hash digest
SHA256 82afc544b556de3844356c136472ea5fb8bf3cc7d5358b988827a771bd258bce
MD5 70db79bfbb386eaf9ab5cc94baf815e8
BLAKE2b-256 5d444b56c6082fbf2efda4ea33db1fbc77dea01e544d28811af0b58f9f7e3950

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