| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import os |
| 5 | import tempfile |
| 6 | |
| 7 | from prometheus_client import REGISTRY, CollectorRegistry, multiprocess |
| 8 | |
| 9 | from vllm.logger import init_logger |
| 10 | |
| 11 | logger = init_logger(__name__) |
| 12 | |
| 13 | # Global temporary directory for prometheus multiprocessing |
| 14 | _prometheus_multiproc_dir: tempfile.TemporaryDirectory | None = None |
| 15 | |
| 16 | |
| 17 | def setup_multiprocess_prometheus(): |
| 18 | """Set up prometheus multiprocessing directory if not already configured.""" |
| 19 | global _prometheus_multiproc_dir |
| 20 | |
| 21 | if "PROMETHEUS_MULTIPROC_DIR" not in os.environ: |
| 22 | # Make TemporaryDirectory for prometheus multiprocessing |
| 23 | # Note: global TemporaryDirectory will be automatically |
| 24 | # cleaned up upon exit. |
| 25 | _prometheus_multiproc_dir = tempfile.TemporaryDirectory() |
| 26 | os.environ["PROMETHEUS_MULTIPROC_DIR"] = _prometheus_multiproc_dir.name |
| 27 | logger.debug( |
| 28 | "Created PROMETHEUS_MULTIPROC_DIR at %s", _prometheus_multiproc_dir.name |
| 29 | ) |
| 30 | else: |
| 31 | logger.warning( |
| 32 | "Found PROMETHEUS_MULTIPROC_DIR was set by user. " |
| 33 | "This directory must be wiped between vLLM runs or " |
| 34 | "you will find inaccurate metrics. Unset the variable " |
| 35 | "and vLLM will properly handle cleanup." |
| 36 | ) |
| 37 | |
| 38 | |
| 39 | def get_prometheus_registry() -> CollectorRegistry: |
| 40 | """Get the appropriate prometheus registry based on multiprocessing |
| 41 | configuration. |
| 42 | |
| 43 | Returns: |
| 44 | Registry: A prometheus registry |
| 45 | |
| 46 | """ |
| 47 | if os.getenv("PROMETHEUS_MULTIPROC_DIR") is not None: |
| 48 | logger.debug("Using multiprocess registry for prometheus metrics") |
| 49 | registry = CollectorRegistry() |
| 50 | multiprocess.MultiProcessCollector(registry) |
| 51 | return registry |
| 52 | |
| 53 | return REGISTRY |
| 54 | |
| 55 | |
| 56 | def unregister_vllm_metrics(): |
| 57 | """Unregister any existing vLLM collectors from the prometheus registry. |
| 58 | |
| 59 | This is useful for testing and CI/CD where metrics may be registered |
| 60 | multiple times across test runs. |
| 61 | |
| 62 | Also, in case of multiprocess, we need to unregister the metrics from the |
| 63 | global registry. |
| 64 | """ |
| 65 | registry = REGISTRY |
| 66 | # Unregister any existing vLLM collectors |
| 67 | for collector in list(registry._collector_to_names): |
| 68 | if hasattr(collector, "_name") and collector._name.startswith("vllm:"): |
| 69 | registry.unregister(collector) |
| 70 | |
| 71 | |
| 72 | def shutdown_prometheus(): |
| 73 | """Shutdown prometheus metrics.""" |
| 74 | path = _prometheus_multiproc_dir |
| 75 | if path is None: |
| 76 | return |
| 77 | try: |
| 78 | pid = os.getpid() |
| 79 | multiprocess.mark_process_dead(pid, path) |
| 80 | logger.debug("Marked Prometheus metrics for process %d as dead", pid) |
| 81 | except Exception as e: |
| 82 | logger.error("Error during metrics cleanup: %s", str(e)) |