Skip to Content
FeaturesModulesMonitorMonitor (django_monitor)

Monitor — django_monitor

django_monitor captures server-side errors, slow queries, background-task failures and browser events, writes each as a JSON line through the standard Django-CFG logging pipeline, and (when Telegram is configured) batches error events into Telegram alerts.

Zero-config. There is no MonitorConfig — nothing to switch on. Capture hooks connect automatically at startup. Telegram alerts turn on by themselves the moment a TelegramConfig is present on your DjangoConfig; with no Telegram configured, capture still records events, it just doesn’t send alerts.


How It Fits Together

Every event is one flat JSON line (the fields at the top level) on the django_cfg.monitor logger: stdout/stderr, and also djangocfg/monitor.log in logging.dir, else /var/log/<app>, else the per-user state dir (10 MB x 5, size-rotated and pruned; see Django Logging). An error that repeats is logged once a minute, and one django_cfg.monitor.repeat line carries the count. Error-level events additionally join a Telegram batch when Telegram is set up.


What Gets Captured

SourceEvent typeAlerts?
got_request_exception signalUNHANDLED_EXCEPTIONyes
logging.Handler (ERROR+)LOG_ERRORyes
execute_wrapper (slow DB query)SLOW_QUERYover threshold
RQ exception_handlerRQ_FAILUREyes
capture_exception()SERVER_ERRORyes
capture_message()LOG_ERRORyes (by level)
Browser events (/cfg/monitor/ingest/)FRONTEND_ERROR, …error-level only

Alerts are batched: the first occurrence of a new fingerprint is sent immediately, and repeats within the window are folded into a single 60-second digest so a crash loop can’t flood your chat.

Each alert line also ends with a <code>fingerprint</code> token when one is available — a stable 16-hex-char SHA-256 join key you can use to correlate repeat occurrences of the same error across alerts, and against monitor.log entries (ev.fingerprint is stored there too). It’s computed per source: from the exception signature for UNHANDLED_EXCEPTION, RQ_FAILURE and LOG_ERROR, or from the query SQL with literal values stripped for SLOW_QUERY — so the same query with different parameters shares one fingerprint. The fingerprint is omitted (not faked) when none is available, which only happens for browser-originated events.


Quick Start

There is nothing to configure for capture. To receive alerts, add a TelegramConfig:

# djangoconfig.py from django_cfg import DjangoConfig, TelegramConfig class MyConfig(DjangoConfig): telegram: TelegramConfig = TelegramConfig( bot_token="${TELEGRAM_BOT_TOKEN}", chat_id="${TELEGRAM_CHAT_ID}", )

That’s the whole setup — errors now flow to monitor.log and to Telegram.

Manual capture (optional)

from django_cfg.modules.django_monitor import capture_exception, capture_message try: process_payment(order) except Exception as e: capture_exception(e, url="/api/orders/", http_method="POST") capture_message("payment gateway slow", level="warning", extra={"latency_ms": 3200})

Both calls are fire-and-forget — they never raise.

Check status

python manage.py monitor_status python manage.py monitor_tail --frontend # last browser events

Reports whether alerts are on (i.e. whether Telegram is configured) and summarizes today’s events from monitor.log.


Browser events

The frontend counterpart is the @djangocfg/devtools package. It captures JS errors, console errors and failed requests in the browser and posts them in batches to POST /cfg/monitor/ingest/, where they are enriched and written to the log like any server event (error-level ones also alert). The endpoint is bounded: 64 KB per request, 60 requests and 300 events a minute per client, 3000 events a minute per project. Behind a proxy set MONITOR_CLIENT_IP_HEADER (CF-Connecting-IP, or X-Forwarded-For, read from the right) so a client is not the proxy. It ships an in-app debug panel for inspecting the same feed live.

Which user hit the error

When the visitor is signed in, the browser sends its access token (Authorization: Bearer ...) and the event line gets "user_id": "<primary key>" next to session_id. Signed out, or with a bad or expired token, the field is absent and the event is still accepted (202): the endpoint never answers 401 or 403 and never looks at a session cookie. Only the primary key is logged (an integer or a uuid, whatever your user model uses), never an e-mail or a name; a user_id in the request body is ignored. A fingerprint identifies the error, session_id one browser tab, user_id the person.

  • A repeat of the same fingerprint inside the 60 s window is counted, not logged, so the line carries the user of the first occurrence; the django_cfg.monitor.repeat line has no user.
  • Rate limits stay per client address, and a signed-in user is counted too (60 requests and 300 events a minute), so one account cannot hide behind many addresses.
  • Privacy: a primary key is a personal identifier. If your site publishes a privacy page, say that browser errors are reported with the user’s id when signed in. Pick the log retention (logging.retain_days) with that in mind.
  • For ids you need: the token must reach the browser the way @djangocfg/api stores it (it does, after sign-in), Authorization must be in the CORS allowed headers for the API origin (it is in the default cors_allow_headers; if you replaced that list, add it back).

Browser events are the one path where an alert may show no fingerprint token — server-side capture always stamps one, but frontend ingest doesn’t compute one upstream.


What’s Next


TAGS: django_monitor, error-tracking, telegram, logging, zero-config

Last updated on