Files
awoooi/apps/api/src/services/alertmanager_llm_guard.py
Your Name 9db87f177e
Some checks failed
CD Pipeline / tests (push) Successful in 1m37s
Code Review / ai-code-review (push) Successful in 28s
CD Pipeline / post-deploy-checks (push) Has been cancelled
CD Pipeline / build-and-deploy (push) Has been cancelled
fix(aiops): suppress repeated llm alert loops
2026-05-01 13:02:07 +08:00

46 lines
1.3 KiB
Python

"""Alertmanager LLM storm guards.
Service-layer Redis helpers used by webhook routers to avoid spawning duplicate
LLM analysis tasks for the same Alertmanager fingerprint.
"""
from src.core.logging import get_logger
from src.core.redis_client import get_redis
logger = get_logger("awoooi.alertmanager_llm_guard")
ALERTMANAGER_LLM_INFLIGHT_LOCK_TTL_SECONDS = 600
def alertmanager_llm_inflight_key(fingerprint: str) -> str:
"""Return the Redis lock key for one Alertmanager fingerprint entering AI analysis."""
return f"alertmanager:llm_inflight:{fingerprint}"
async def try_acquire_alertmanager_llm_lock(
fingerprint: str,
alert_id: str,
*,
ttl_seconds: int = ALERTMANAGER_LLM_INFLIGHT_LOCK_TTL_SECONDS,
) -> bool:
"""Prevent same-second duplicate Alertmanager deliveries from spawning LLM calls."""
try:
redis = get_redis()
acquired = await redis.set(
alertmanager_llm_inflight_key(fingerprint),
alert_id,
ex=ttl_seconds,
nx=True,
)
return bool(acquired)
except Exception as exc:
logger.warning(
"alertmanager_llm_inflight_lock_failed_fail_open",
fingerprint=fingerprint,
alert_id=alert_id,
error=str(exc),
)
return True