Implement Monlet MVP stack and UI updates
This commit is contained in:
45
server/monlet_server/services/notifiers/alertmanager.py
Normal file
45
server/monlet_server/services/notifiers/alertmanager.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import httpx
|
||||
|
||||
from ...redaction import redact
|
||||
from .base import DeliveryResult
|
||||
from .http import classify_response
|
||||
|
||||
|
||||
class AlertmanagerNotifier:
|
||||
name = "alertmanager"
|
||||
|
||||
def __init__(self, client: httpx.AsyncClient, url: str) -> None:
|
||||
self._client = client
|
||||
self._url = url.rstrip("/")
|
||||
|
||||
async def deliver(self, event_type: str, payload: dict) -> DeliveryResult:
|
||||
labels = {
|
||||
"alertname": "monlet_incident",
|
||||
"agent_id": str(payload.get("agent_id", "")),
|
||||
"check_id": str(payload.get("check_id", "")),
|
||||
"incident_key": str(payload.get("incident_key", "")),
|
||||
}
|
||||
annotations = {
|
||||
"severity": str(payload.get("severity", "unknown")),
|
||||
"status": str(payload.get("status", "")),
|
||||
"summary": redact(payload.get("summary") or "") or "",
|
||||
}
|
||||
if payload.get("output"):
|
||||
annotations["output"] = (redact(payload["output"]) or "")[:1024]
|
||||
alert = {
|
||||
"labels": labels,
|
||||
"annotations": annotations,
|
||||
"startsAt": payload.get("opened_at") or payload.get("observed_at"),
|
||||
}
|
||||
if event_type == "resolved" and payload.get("resolved_at"):
|
||||
alert["endsAt"] = payload["resolved_at"]
|
||||
try:
|
||||
resp = await self._client.post(f"{self._url}/api/v2/alerts", json=[alert])
|
||||
except httpx.HTTPError as exc:
|
||||
return DeliveryResult(ok=False, permanent=False, error=type(exc).__name__)
|
||||
ok, perm = classify_response(resp.status_code)
|
||||
if ok:
|
||||
return DeliveryResult(ok=True)
|
||||
return DeliveryResult(ok=False, permanent=perm, error=f"http_{resp.status_code}")
|
||||
Reference in New Issue
Block a user