Send buffered records to the collection endpoint. Returns count sent.
()
| 245 | |
| 246 | |
| 247 | def flush() -> int: |
| 248 | """Send buffered records to the collection endpoint. Returns count sent.""" |
| 249 | buf = _buffer_path() |
| 250 | if not buf.exists(): |
| 251 | return 0 |
| 252 | try: |
| 253 | records_text = buf.read_text().strip() |
| 254 | if not records_text: |
| 255 | return 0 |
| 256 | lines = records_text.splitlines() |
| 257 | success = _send_batch(lines) |
| 258 | if success: |
| 259 | # Write to sent log (audit trail for successfully sent records) |
| 260 | sent = _sent_log_path() |
| 261 | with open(sent, "a", encoding="utf-8") as f: |
| 262 | for line in lines: |
| 263 | f.write(line + "\n") |
| 264 | # Clear buffer |
| 265 | buf.unlink(missing_ok=True) |
| 266 | return len(lines) |
| 267 | except Exception as e: |
| 268 | logger.debug("Telemetry flush failed: %s", e) |
| 269 | return 0 |
| 270 | |
| 271 | |
| 272 | def _send_batch(lines: list[str]) -> bool: |
no test coverage detected