Skip to main content

Watchlog APM integration for Django

Project description

APM for Django – Watchlog Integration

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

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


🚀 Features

  • 🔧 Automatic tracking of all HTTP requests
  • 📊 Aggregation of metrics by route and method
  • ⚠️ Optional error tracking via error handler
  • 🧠 Smart batching to avoid redundant sends
  • 🌐 Sends metrics over HTTP to Watchlog agent
  • 🏷️ Automatically detects route pattern like users/<int:id>/
  • 💡 Safe-by-default (never crashes your app)

📦 Installation

pip install django_watchlog_apm

⚙️ Usage

In your Django settings.py, add the middleware after routing is resolved, e.g.:

MIDDLEWARE = [
    ...
    'django_watchlog_apm.middleware.WatchlogAPMMiddleware',
]

If you want to also track uncaught exceptions, add:

MIDDLEWARE = [
    ...
    'django_watchlog_apm.error_handler.capture_exception',
    'django_watchlog_apm.middleware.WatchlogAPMMiddleware',
]

🛠️ Service Identification

You can define your service name in the .env or environment variables:

WATCHLOG_APM_SERVICE=payments-service

If not defined, it falls back to:

APP_NAME=django-app

If neither is set, the default django-app is used.


📤 What gets sent?

Example payload every 10 seconds:

{
  "collected_at": "2025-05-18T12:00:00Z",
  "platformName": "django",
  "metrics": [
    {
      "type": "aggregated_request",
      "service": "payments-service",
      "path": "users/<int:id>/",
      "method": "GET",
      "request_count": 2,
      "error_count": 0,
      "avg_duration": 12.3,
      "max_duration": 20.4,
      "avg_memory": {
        "rss": 18432000,
        "heapUsed": 23789568,
        "heapTotal": 23789568
      }
    }
  ]
}

🧪 Example view to test errors

In urls.py:

from django.urls import path
from .views import hello, fail

urlpatterns = [
    path("hello/<int:id>/", hello),
    path("fail/", fail),
]

In views.py:

from django.http import JsonResponse

def hello(request, id):
    return JsonResponse({"message": f"Hello {id}"})

def fail(request):
    1 / 0  # triggers 500 error

📁 Recommended .gitignore

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

✅ Notes

  • Route patterns are extracted using request.resolver_match.route
  • Middleware must be placed after routing to access the route object
  • Metrics are batched in a local file and flushed every 10 seconds
  • Errors are captured if error handler middleware is enabled

📝 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

django_watchlog_apm-0.1.3.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

django_watchlog_apm-0.1.3-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file django_watchlog_apm-0.1.3.tar.gz.

File metadata

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

File hashes

Hashes for django_watchlog_apm-0.1.3.tar.gz
Algorithm Hash digest
SHA256 66e6664bff41aa501d943770ffb229501b8d343e85cc5ec84dfb02a3111fe550
MD5 cea699e9b88c4f1eff0810c81e500d99
BLAKE2b-256 a41b70306e5416947e621bd770f5f85c61b221d1476feceb1321bb78351926bc

See more details on using hashes here.

File details

Details for the file django_watchlog_apm-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_watchlog_apm-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2f0bfbb8d7bbba4fd28ccdc194fc718916be94ab327e2adacef5e7a9224d4f09
MD5 1a3c1072e2d3d4a84b9efafa48d91ef9
BLAKE2b-256 8040498cc421b8946055604be5d06a2d76d01fd3a4aa4fcd1863f38f9555f384

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