mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-05 16:48:03 +08:00
Use uuid to name the metrics shared folder (#4025)
* Use uuid to name the metrics shared folder * Use uuid to name the metrics shared folder test case
This commit is contained in:
@@ -19,6 +19,7 @@ metrics
|
|||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
import uuid
|
||||||
from typing import Set
|
from typing import Set
|
||||||
|
|
||||||
from prometheus_client import (
|
from prometheus_client import (
|
||||||
@@ -35,24 +36,20 @@ from fastdeploy.metrics import build_1_2_5_buckets
|
|||||||
from fastdeploy.metrics.work_metrics import work_process_metrics
|
from fastdeploy.metrics.work_metrics import work_process_metrics
|
||||||
|
|
||||||
|
|
||||||
def cleanup_prometheus_files(is_main):
|
def cleanup_prometheus_files(is_main: bool, instance_id: str = None):
|
||||||
"""
|
"""
|
||||||
Cleans and recreates the Prometheus multiprocess directory.
|
Cleans and recreates the Prometheus multiprocess directory.
|
||||||
|
|
||||||
Depending on whether it's the main process or a worker, this function removes the corresponding
|
|
||||||
Prometheus multiprocess directory (/tmp/prom_main or /tmp/prom_worker) and recreates it as an empty directory.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
is_main (bool): Indicates whether the current process is the main process.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: The path to the newly created Prometheus multiprocess directory.
|
|
||||||
"""
|
"""
|
||||||
PROM_DIR = "/tmp/prom_main" if is_main else "/tmp/prom_worker"
|
base_dir = "/tmp/prom_main" if is_main else "/tmp/prom_worker"
|
||||||
if os.path.exists(PROM_DIR):
|
if instance_id is None:
|
||||||
shutil.rmtree(PROM_DIR)
|
instance_id = str(uuid.uuid4())
|
||||||
os.makedirs(PROM_DIR, exist_ok=True)
|
prom_dir = f"{base_dir}_{instance_id}"
|
||||||
return PROM_DIR
|
|
||||||
|
if os.path.exists(prom_dir):
|
||||||
|
shutil.rmtree(prom_dir, ignore_errors=True)
|
||||||
|
os.makedirs(prom_dir, exist_ok=True)
|
||||||
|
|
||||||
|
return prom_dir
|
||||||
|
|
||||||
|
|
||||||
class SimpleCollector(Collector):
|
class SimpleCollector(Collector):
|
||||||
|
@@ -14,12 +14,13 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from prometheus_client import Gauge
|
from prometheus_client import Gauge
|
||||||
|
|
||||||
from fastdeploy.metrics.metrics import get_filtered_metrics
|
from fastdeploy.metrics.metrics import cleanup_prometheus_files, get_filtered_metrics
|
||||||
|
|
||||||
|
|
||||||
class TestGetFilteredMetrics(unittest.TestCase):
|
class TestGetFilteredMetrics(unittest.TestCase):
|
||||||
@@ -68,5 +69,30 @@ class TestGetFilteredMetrics(unittest.TestCase):
|
|||||||
self.assertIn("custom_metric_total", result)
|
self.assertIn("custom_metric_total", result)
|
||||||
|
|
||||||
|
|
||||||
|
class TestCleanupPrometheusFiles(unittest.TestCase):
|
||||||
|
def test_cleanup_prometheus_files(self):
|
||||||
|
prom_dir = cleanup_prometheus_files(is_main=True)
|
||||||
|
self.assertTrue(os.path.exists(prom_dir))
|
||||||
|
self.assertTrue(os.path.isdir(prom_dir))
|
||||||
|
|
||||||
|
prom_dir2 = cleanup_prometheus_files(is_main=True)
|
||||||
|
self.assertNotEqual(prom_dir, prom_dir2)
|
||||||
|
self.assertTrue(os.path.exists(prom_dir2))
|
||||||
|
self.assertTrue(os.path.isdir(prom_dir2))
|
||||||
|
|
||||||
|
prom_dir = cleanup_prometheus_files(is_main=True, instance_id="001")
|
||||||
|
self.assertTrue(os.path.exists(prom_dir))
|
||||||
|
self.assertTrue(os.path.isdir(prom_dir))
|
||||||
|
test_file = os.path.join(prom_dir, "test.txt")
|
||||||
|
with open(test_file, "w") as f:
|
||||||
|
f.write("hello")
|
||||||
|
self.assertTrue(os.path.exists(test_file))
|
||||||
|
prom_dir2 = cleanup_prometheus_files(is_main=True, instance_id="001")
|
||||||
|
self.assertEqual(prom_dir, prom_dir2)
|
||||||
|
self.assertTrue(os.path.exists(prom_dir2))
|
||||||
|
self.assertTrue(os.path.isdir(prom_dir2))
|
||||||
|
self.assertFalse(os.path.exists(test_file))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Reference in New Issue
Block a user