This commit is contained in:
HgO 2022-08-08 01:59:09 +02:00
parent 3705f6c219
commit 1555359e03

View file

@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
import logging
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Dict, List, Optional from typing import Dict, List, Optional
@ -20,6 +21,9 @@ DEFAULT_DURATION = timedelta(hours=3)
MAX_DURATION = timedelta(days=3652) MAX_DURATION = timedelta(days=3652)
logger = logging.getLogger(__name__)
class AlertmanagerClient: class AlertmanagerClient:
def __init__(self, url: str, cache: Cache) -> None: def __init__(self, url: str, cache: Cache) -> None:
self.api_url = f"{url}/api/v2" self.api_url = f"{url}/api/v2"
@ -40,6 +44,7 @@ class AlertmanagerClient:
) from e ) from e
async def get_alert(self, fingerprint: str) -> Dict: async def get_alert(self, fingerprint: str) -> Dict:
logger.debug(f"Fetching details for alert with fingerprint {fingerprint}")
alerts = await self.get_alerts() alerts = await self.get_alerts()
return self._find_alert(fingerprint, alerts) return self._find_alert(fingerprint, alerts)
@ -54,6 +59,7 @@ class AlertmanagerClient:
) from e ) from e
async def get_silence(self, silence_id: str) -> Dict: async def get_silence(self, silence_id: str) -> Dict:
logger.debug(f"Fetching details for silence with ID {silence_id}")
silences = await self.get_silences() silences = await self.get_silences()
return self._find_silence(silence_id, silences) return self._find_silence(silence_id, silences)
@ -80,6 +86,9 @@ class AlertmanagerClient:
user: Optional[str] = None, user: Optional[str] = None,
duration_seconds: Optional[int] = None, duration_seconds: Optional[int] = None,
) -> str: ) -> str:
logger.debug(
f"Reading silence for alert with fingerprint {fingerprint} from cache"
)
try: try:
silence_id: Optional[str] silence_id: Optional[str]
expire_time: Optional[int] expire_time: Optional[int]
@ -92,6 +101,8 @@ class AlertmanagerClient:
f"Cannot find silence for alert with fingerprint {fingerprint} in cache." f"Cannot find silence for alert with fingerprint {fingerprint} in cache."
) )
logger.debug(f"Updating silence with ID {silence_id}")
if duration_seconds is None: if duration_seconds is None:
if expire_time is not None: if expire_time is not None:
raise SilenceExtendError( raise SilenceExtendError(