|
| 1 | +"""Async Splunk HEC client for sending telemetry events.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import platform |
| 5 | +import time |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import aiohttp |
| 9 | + |
| 10 | +from configuration import configuration |
| 11 | +from version import __version__ |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +def _get_hostname() -> str: |
| 17 | + """Get the hostname for Splunk event metadata.""" |
| 18 | + return platform.node() or "unknown" |
| 19 | + |
| 20 | + |
| 21 | +def _read_token_from_file(token_path: str) -> str | None: |
| 22 | + """Read HEC token from file path.""" |
| 23 | + try: |
| 24 | + with open(token_path, encoding="utf-8") as f: |
| 25 | + return f.read().strip() |
| 26 | + except OSError as e: |
| 27 | + logger.warning("Failed to read Splunk HEC token from %s: %s", token_path, e) |
| 28 | + return None |
| 29 | + |
| 30 | + |
| 31 | +async def send_splunk_event(event: dict[str, Any], sourcetype: str) -> None: |
| 32 | + """Send an event to Splunk HEC. |
| 33 | +
|
| 34 | + This function sends events asynchronously and handles failures gracefully |
| 35 | + by logging warnings instead of raising exceptions. This ensures that |
| 36 | + Splunk connectivity issues don't affect the main application flow. |
| 37 | +
|
| 38 | + Args: |
| 39 | + event: The event payload to send. |
| 40 | + sourcetype: The Splunk sourcetype (e.g., "infer_with_llm", "infer_error"). |
| 41 | + """ |
| 42 | + splunk_config = configuration.splunk |
| 43 | + if splunk_config is None or not splunk_config.enabled: |
| 44 | + logger.debug("Splunk integration disabled, skipping event") |
| 45 | + return |
| 46 | + |
| 47 | + if not splunk_config.url or not splunk_config.token_path or not splunk_config.index: |
| 48 | + logger.warning("Splunk configuration incomplete, skipping event") |
| 49 | + return |
| 50 | + |
| 51 | + # Read token on each request to support rotation without restart |
| 52 | + token = _read_token_from_file(str(splunk_config.token_path)) |
| 53 | + if not token: |
| 54 | + return |
| 55 | + |
| 56 | + payload = { |
| 57 | + "time": int(time.time()), |
| 58 | + "host": _get_hostname(), |
| 59 | + "source": f"{splunk_config.source} (v{__version__})", |
| 60 | + "sourcetype": sourcetype, |
| 61 | + "index": splunk_config.index, |
| 62 | + "event": event, |
| 63 | + } |
| 64 | + |
| 65 | + headers = { |
| 66 | + "Authorization": f"Splunk {token}", |
| 67 | + "Content-Type": "application/json", |
| 68 | + } |
| 69 | + |
| 70 | + timeout = aiohttp.ClientTimeout(total=splunk_config.timeout) |
| 71 | + connector = aiohttp.TCPConnector(ssl=splunk_config.verify_ssl) |
| 72 | + |
| 73 | + try: |
| 74 | + async with aiohttp.ClientSession( |
| 75 | + timeout=timeout, connector=connector |
| 76 | + ) as session: |
| 77 | + async with session.post( |
| 78 | + splunk_config.url, json=payload, headers=headers |
| 79 | + ) as response: |
| 80 | + if response.status >= 400: |
| 81 | + body = await response.text() |
| 82 | + logger.warning( |
| 83 | + "Splunk HEC request failed with status %d: %s", |
| 84 | + response.status, |
| 85 | + body[:200], |
| 86 | + ) |
| 87 | + except aiohttp.ClientError as e: |
| 88 | + logger.warning("Splunk HEC request failed: %s", e) |
| 89 | + except TimeoutError: |
| 90 | + logger.warning("Splunk HEC request timed out after %ds", splunk_config.timeout) |
0 commit comments