|
| 1 | +"""Buffer the CLI's local log records and POST them in batches to |
| 2 | +/python-cli-runs/<run_id>/logs so the dashboard's view of a CLI run |
| 3 | +mirrors what the user sees in their terminal. |
| 4 | +
|
| 5 | +Behavior: |
| 6 | +- daemon thread, 5s flush |
| 7 | +- swallow all network errors (debug log only) |
| 8 | +- skip empty buffers |
| 9 | +- drain on shutdown |
| 10 | +- at-most-once semantics (failed batches dropped, not retried) |
| 11 | +
|
| 12 | +A thread-local recursion guard prevents the uploader's own request-error |
| 13 | +log lines (emitted by `cli_client.py`'s `socketdev` logger) from being |
| 14 | +re-enqueued during a flush. |
| 15 | +""" |
| 16 | + |
| 17 | +import json |
| 18 | +import logging |
| 19 | +import threading |
| 20 | +from datetime import datetime, timezone |
| 21 | +from typing import Optional |
| 22 | + |
| 23 | +from .cli_client import CliClient |
| 24 | + |
| 25 | +log = logging.getLogger(__name__) |
| 26 | + |
| 27 | +_FLUSH_GUARD = threading.local() |
| 28 | + |
| 29 | +_MAX_BATCH_BYTES = 256 * 1024 - 1024 # depscan body cap is 256KB; reserve headroom for envelope/headers |
| 30 | + |
| 31 | +_LEVEL_MAP = { |
| 32 | + logging.DEBUG: "DEBUG", |
| 33 | + logging.INFO: "INFO", |
| 34 | + logging.WARNING: "WARN", |
| 35 | + logging.ERROR: "ERROR", |
| 36 | + logging.CRITICAL: "ERROR", |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +def _now_str() -> str: |
| 41 | + return datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] |
| 42 | + |
| 43 | + |
| 44 | +class BatchedLogUploader: |
| 45 | + def __init__( |
| 46 | + self, |
| 47 | + client: CliClient, |
| 48 | + run_id: str, |
| 49 | + flush_interval: float = 5.0, |
| 50 | + ): |
| 51 | + self._client = client |
| 52 | + self._run_id = run_id |
| 53 | + self._flush_interval = flush_interval |
| 54 | + self._buf: list = [] |
| 55 | + self._lock = threading.Lock() |
| 56 | + self._stop = threading.Event() |
| 57 | + self._thread: Optional[threading.Thread] = None |
| 58 | + |
| 59 | + def add(self, entry: dict) -> None: |
| 60 | + with self._lock: |
| 61 | + self._buf.append(entry) |
| 62 | + |
| 63 | + def start(self) -> None: |
| 64 | + if self._thread is not None: |
| 65 | + return |
| 66 | + self._thread = threading.Thread( |
| 67 | + target=self._run, |
| 68 | + name=f"socket-log-uploader-{self._run_id[:8]}", |
| 69 | + daemon=True, |
| 70 | + ) |
| 71 | + self._thread.start() |
| 72 | + |
| 73 | + def stop(self, timeout: float = 2.0) -> None: |
| 74 | + if self._thread is None: |
| 75 | + self._flush() |
| 76 | + return |
| 77 | + self._stop.set() |
| 78 | + self._thread.join(timeout=timeout) |
| 79 | + self._thread = None |
| 80 | + self._flush() |
| 81 | + |
| 82 | + def _run(self) -> None: |
| 83 | + while not self._stop.is_set(): |
| 84 | + self._flush() |
| 85 | + self._stop.wait(self._flush_interval) |
| 86 | + |
| 87 | + def _flush(self) -> None: |
| 88 | + with self._lock: |
| 89 | + if not self._buf: |
| 90 | + return |
| 91 | + entries = self._buf |
| 92 | + self._buf = [] |
| 93 | + |
| 94 | + _FLUSH_GUARD.active = True |
| 95 | + try: |
| 96 | + for chunk in _chunk_by_size(entries): |
| 97 | + try: |
| 98 | + self._client.request( |
| 99 | + path=f"python-cli-runs/{self._run_id}/logs", |
| 100 | + method="POST", |
| 101 | + payload=json.dumps({"logs": chunk}), |
| 102 | + ) |
| 103 | + except Exception as e: |
| 104 | + log.debug(f"log upload failed (swallowed, {len(chunk)} entries dropped): {e}") |
| 105 | + finally: |
| 106 | + _FLUSH_GUARD.active = False |
| 107 | + |
| 108 | + |
| 109 | +def _chunk_by_size(entries: list) -> list: |
| 110 | + """Split entries into chunks that each serialize to <= _MAX_BATCH_BYTES. |
| 111 | + Single entries that exceed the cap are dropped with a debug log.""" |
| 112 | + chunks: list = [] |
| 113 | + current: list = [] |
| 114 | + envelope = len('{"logs":[]}') |
| 115 | + current_size = envelope |
| 116 | + for entry in entries: |
| 117 | + entry_size = len(json.dumps(entry)) + 1 # +1 for inter-entry comma |
| 118 | + if entry_size + envelope > _MAX_BATCH_BYTES: |
| 119 | + log.debug(f"log entry too large ({entry_size}B), dropped") |
| 120 | + continue |
| 121 | + if current and current_size + entry_size > _MAX_BATCH_BYTES: |
| 122 | + chunks.append(current) |
| 123 | + current = [entry] |
| 124 | + current_size = envelope + entry_size |
| 125 | + else: |
| 126 | + current.append(entry) |
| 127 | + current_size += entry_size |
| 128 | + if current: |
| 129 | + chunks.append(current) |
| 130 | + return chunks |
| 131 | + |
| 132 | + |
| 133 | +class UploadingLogHandler(logging.Handler): |
| 134 | + def __init__(self, uploader: BatchedLogUploader, context: str = "socket-python-cli"): |
| 135 | + super().__init__() |
| 136 | + self._uploader = uploader |
| 137 | + self._context = context |
| 138 | + |
| 139 | + def emit(self, record: logging.LogRecord) -> None: |
| 140 | + if getattr(_FLUSH_GUARD, "active", False): |
| 141 | + return |
| 142 | + try: |
| 143 | + self._uploader.add({ |
| 144 | + "timestamp": _now_str(), |
| 145 | + "level": _LEVEL_MAP.get(record.levelno, "INFO"), |
| 146 | + "message": self.format(record), |
| 147 | + "context": self._context, |
| 148 | + }) |
| 149 | + except Exception: |
| 150 | + self.handleError(record) |
0 commit comments