Files
LLM-Zabbix-HABR/alert-receiver/app/forwarder.py
T

46 lines
1.3 KiB
Python

from __future__ import annotations
import logging
import httpx
from app.config import settings
from app.models import ProcessorForwardEnvelope
logger = logging.getLogger(__name__)
async def forward_to_processor(envelope: ProcessorForwardEnvelope) -> bool:
if not settings.forward_to_processor:
logger.info(
"Forwarding disabled, event accepted locally only: correlation_id=%s",
envelope.event.correlation_id,
)
return False
headers = {}
if settings.alert_processor_token:
headers["X-Internal-Token"] = settings.alert_processor_token
try:
async with httpx.AsyncClient(timeout=settings.forward_timeout_seconds) as client:
response = await client.post(
settings.alert_processor_url,
json=envelope.model_dump(mode="json"),
headers=headers,
)
response.raise_for_status()
logger.info(
"Forwarded event to alert-processor: correlation_id=%s status=%s",
envelope.event.correlation_id,
response.status_code,
)
return True
except Exception as exc:
logger.exception(
"Failed to forward event to alert-processor: correlation_id=%s error=%s",
envelope.event.correlation_id,
exc,
)
return False