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