Fix corrupted JSONL metrics file due to concurrent writes (#19011)

This commit is contained in:
Talor Abramovich
2026-02-22 14:33:27 +02:00
committed by GitHub
parent 1f7a813051
commit c6a99e43b9

View File

@@ -87,6 +87,7 @@ class FileRequestMetricsExporter(RequestMetricsExporter):
# File handler state management
self._current_file_handler = None
self._current_file_lock = asyncio.Lock()
self._current_hour_suffix = None
def _ensure_file_handler(self, hour_suffix: str):
@@ -135,20 +136,21 @@ class FileRequestMetricsExporter(RequestMetricsExporter):
current_time = datetime.now()
hour_suffix = current_time.strftime("%Y%m%d_%H")
# Ensure correct file handler is open for current hour
self._ensure_file_handler(hour_suffix)
async with self._current_file_lock:
# Ensure correct file handler is open for current hour
self._ensure_file_handler(hour_suffix)
if self._current_file_handler is None:
return
if self._current_file_handler is None:
return
metrics_data = self._format_output_data(obj, out_dict)
metrics_data = self._format_output_data(obj, out_dict)
def write_file():
json.dump(metrics_data, self._current_file_handler)
self._current_file_handler.write("\n")
self._current_file_handler.flush()
def write_file():
json.dump(metrics_data, self._current_file_handler)
self._current_file_handler.write("\n")
self._current_file_handler.flush()
await asyncio.to_thread(write_file)
await asyncio.to_thread(write_file)
except Exception as e:
logger.exception(f"Failed to write perf metrics to file: {e}")