release: v1.10.0 — модуль команд (teams), team_id на алертах
- Alembic 006: teams, team_label_rules, irm_alerts.team_id - Вебхук: сопоставление команды по правилам лейблов (priority) - API/UI Команды; алерты: JOIN team, фильтр team_id - Тесты test_team_match, test_teams_api; обновлён test_root_ui Made-with: Cursor
This commit is contained in:
@ -10,6 +10,7 @@ from starlette.responses import Response
|
||||
from onguard24.domain.entities import Alert, Severity
|
||||
from onguard24.grafana_sources import sources_by_slug, webhook_authorized
|
||||
from onguard24.ingress.grafana_payload import extract_alert_row_from_grafana_body
|
||||
from onguard24.ingress.team_match import resolve_team_id_for_labels
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter(tags=["ingress"])
|
||||
@ -136,13 +137,14 @@ async def _grafana_webhook_impl(
|
||||
)
|
||||
raw_id = row["id"] if row else None
|
||||
if raw_id is not None:
|
||||
team_id = await resolve_team_id_for_labels(conn, labels_row)
|
||||
await conn.execute(
|
||||
"""
|
||||
INSERT INTO irm_alerts (
|
||||
ingress_event_id, status, title, severity, source,
|
||||
grafana_org_slug, service_name, labels, fingerprint
|
||||
grafana_org_slug, service_name, labels, fingerprint, team_id
|
||||
)
|
||||
VALUES ($1, 'firing', $2, $3, 'grafana', $4, $5, $6::jsonb, $7)
|
||||
VALUES ($1, 'firing', $2, $3, 'grafana', $4, $5, $6::jsonb, $7, $8)
|
||||
""",
|
||||
raw_id,
|
||||
title_row or "—",
|
||||
@ -151,6 +153,7 @@ async def _grafana_webhook_impl(
|
||||
service_name,
|
||||
json.dumps(labels_row),
|
||||
fp_row,
|
||||
team_id,
|
||||
)
|
||||
bus = getattr(request.app.state, "event_bus", None)
|
||||
if bus and raw_id is not None:
|
||||
|
||||
51
onguard24/ingress/team_match.py
Normal file
51
onguard24/ingress/team_match.py
Normal file
@ -0,0 +1,51 @@
|
||||
"""Сопоставление входящего алерта с командой по правилам лейблов (как Team в Grafana IRM)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Sequence
|
||||
from uuid import UUID
|
||||
|
||||
import asyncpg
|
||||
|
||||
|
||||
def match_team_for_labels(
|
||||
labels: dict[str, Any],
|
||||
rules: Sequence[asyncpg.Record | tuple[UUID, str, str]],
|
||||
) -> UUID | None:
|
||||
"""
|
||||
rules — упорядочены по приоритету (выше priority — раньше проверка).
|
||||
Первое совпадение label_key == label_value возвращает team_id.
|
||||
"""
|
||||
if not labels or not rules:
|
||||
return None
|
||||
flat: dict[str, str] = {
|
||||
str(k): "" if v is None else str(v) for k, v in labels.items()
|
||||
}
|
||||
for row in rules:
|
||||
if isinstance(row, tuple):
|
||||
tid, key, val = row[0], row[1], row[2]
|
||||
else:
|
||||
tid = row["team_id"]
|
||||
key = row["label_key"]
|
||||
val = row["label_value"]
|
||||
if flat.get(str(key)) == str(val):
|
||||
return tid if isinstance(tid, UUID) else UUID(str(tid))
|
||||
return None
|
||||
|
||||
|
||||
async def fetch_team_rules(conn: asyncpg.Connection) -> list[asyncpg.Record]:
|
||||
return await conn.fetch(
|
||||
"""
|
||||
SELECT team_id, label_key, label_value
|
||||
FROM team_label_rules
|
||||
ORDER BY priority DESC, id ASC
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
async def resolve_team_id_for_labels(
|
||||
conn: asyncpg.Connection,
|
||||
labels: dict[str, Any],
|
||||
) -> UUID | None:
|
||||
rules = await fetch_team_rules(conn)
|
||||
return match_team_for_labels(labels, list(rules))
|
||||
Reference in New Issue
Block a user