52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
#THIS IS WHAT IS USED IN PROD, CURRENT MAIN INIT IS FOR TESTING ONLY
|
|
import os, json, gzip, logging, requests
|
|
import azure.functions as func
|
|
|
|
AGENT_API_URL = os.getenv("AGENT_API_URL") # e.g. https://agent-api-app.azurewebsites.net
|
|
|
|
def _to_events(data: bytes):
|
|
# gunzip if needed
|
|
if len(data) >= 2 and data[0] == 0x1F and data[1] == 0x8B:
|
|
data = gzip.decompress(data)
|
|
text = data.decode("utf-8", errors="replace").lstrip("\ufeff").strip()
|
|
if not text:
|
|
return []
|
|
# try JSON (object or array)
|
|
try:
|
|
parsed = json.loads(text)
|
|
if isinstance(parsed, dict):
|
|
return [parsed]
|
|
if isinstance(parsed, list):
|
|
return [x for x in parsed if isinstance(x, dict)]
|
|
except Exception:
|
|
pass
|
|
# try JSONL
|
|
out = []
|
|
for ln in text.splitlines():
|
|
s = ln.strip()
|
|
if not s:
|
|
continue
|
|
try:
|
|
out.append(json.loads(s))
|
|
except Exception:
|
|
pass
|
|
return out
|
|
|
|
def main(inputblob: func.InputStream):
|
|
if not AGENT_API_URL:
|
|
logging.warning("AGENT_API_URL not set; skipping.")
|
|
return
|
|
|
|
events = _to_events(inputblob.read())
|
|
if not events:
|
|
logging.info("No JSON events parsed from blob %s", inputblob.name)
|
|
return
|
|
|
|
try:
|
|
url = AGENT_API_URL.rstrip("/") + "/ingest"
|
|
r = requests.post(url, json={"events": events}, timeout=30)
|
|
logging.info("Agent ingest -> %s %s", r.status_code, r.text[:300])
|
|
r.raise_for_status()
|
|
except Exception as e:
|
|
logging.exception("Ingest failed: %s", e)
|