monitoring.alerts_storage module
MongoDB-backed alerts storage with TTL and simple counters.
Design goals: - Fail-open: never throw from public APIs - Lazy init: connect to Mongo only on first use - Config via env only (avoid importing global config to prevent cycles) - TTL-based retention (default 30 days) to keep the collection bounded
Environment variables: - ALERTS_DB_ENABLED: ”true/1/yes“ to enable writes (fallback to METRICS_DB_ENABLED) - MONGODB_URL: required when enabled - DATABASE_NAME: DB name (default: code_keeper_bot) - ALERTS_COLLECTION: Collection name (default: alerts_log) - ALERTS_TTL_DAYS: TTL for documents (default: 30)
Public API: - record_alert(alert_id, name, severity, summary, source) -> None - count_alerts_since(since_dt) -> tuple[int, int] - count_alerts_last_hours(hours=24) -> tuple[int, int]
- monitoring.alerts_storage.compute_error_signature(error_data)[מקור]
מחשב חתימה ייחודית לשגיאה.
החתימה מבוססת על: - סוג השגיאה - שם הקובץ והשורה (אם יש) - 3 השורות הראשונות של ה-stack trace
- monitoring.alerts_storage.is_new_error(signature)[מקור]
בודק אם השגיאה חדשה (לא נראתה ב-30 יום האחרונים).
- monitoring.alerts_storage.enrich_alert_with_signature(alert_data)[מקור]
מעשיר את נתוני ההתראה עם חתימה ומידע על חדשות.
- monitoring.alerts_storage.record_alert(*, alert_id, name, severity, summary='', source='', silenced=False, details=None)[מקור]
Insert (or upsert via unique key) a single alert record.
When alert_id is provided, use it for de-duplication via a unique key.
Otherwise use a stable hash based on name/severity/summary/minute.
- monitoring.alerts_storage.fetch_alert_type_catalog(*, min_total_count=1, limit=5000)[מקור]
Return catalog of all observed alert types (fail-open).
- Each row includes:
alert_type, total_count, first_seen_dt, last_seen_dt, sample_name, sample_title
- monitoring.alerts_storage.fetch_alerts_by_type(*, alert_type, limit=100, include_details=True)[מקור]
Fetch recent alerts of a specific type with Sentry details.
Returns a list of dicts, for example:
{ "alert_id": str, "ts_dt": datetime, "name": str, "summary": str, "sentry_issue_id": Optional[str], "sentry_permalink": Optional[str], "sentry_short_id": Optional[str], }
- monitoring.alerts_storage.count_alerts_since(since_dt)[מקור]
Return (total, critical) counts since the given datetime (UTC recommended).
- monitoring.alerts_storage.list_recent_alert_ids(limit=10)[מקור]
Return recent alert identifiers from the DB (fail-open).
Preference order: document
alert_idwhen present, otherwise the stable unique_keyused for de-duplication. Results are ordered byts_dtdescending and truncated tolimit.
- monitoring.alerts_storage.fetch_alerts(*, start_dt, end_dt, severity=None, alert_type=None, endpoint=None, search=None, page=1, per_page=50)[מקור]
Return paginated alert documents filtered by the provided criteria.
- monitoring.alerts_storage.aggregate_alert_type_stats(*, start_dt, end_dt, min_count=1, limit=500)[מקור]
Aggregate active alert_types with counts and last_seen (exclude drills).
- Return type:
- פרמטרים:
- Returns a list of dicts:
{ ”alert_type“: str, ”count“: int, ”last_seen_dt“: datetime, ”sample_title“: str, ”sample_name“: str }
Fail-open: returns [] on any error / when storage is unavailable.
- monitoring.alerts_storage.aggregate_alert_summary(*, start_dt, end_dt)[מקור]
Aggregate alert counts by severity and deployment flag.