Skip to content

Commit 1265bb9

Browse files
committed
update version
1 parent 91b3d9e commit 1265bb9

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

mcpgateway/version.py

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,46 +145,88 @@ def _database_version() -> tuple[str, bool]:
145145

146146
def _system_metrics() -> Dict[str, Any]:
147147
"""
148-
Gather system and process metrics using psutil if available.
148+
Gather system-wide and per-process metrics using psutil, falling back gracefully
149+
if psutil is not installed or certain APIs are unavailable.
149150
150151
Returns:
151-
Dict[str, Any]: Metrics including CPU, memory, disk, and process details.
152+
Dict[str, Any]: A dictionary containing:
153+
- boot_time (str): ISO-formatted system boot time.
154+
- cpu_percent (float): Total CPU utilization percentage.
155+
- cpu_count (int): Number of logical CPU cores.
156+
- cpu_freq_mhz (int | None): Current CPU frequency in MHz, or None if unavailable.
157+
- load_avg (tuple[float | None, float | None, float | None]):
158+
System load average over 1, 5, and 15 minutes, or (None, None, None)
159+
on platforms without getloadavg.
160+
- mem_total_mb (int): Total physical memory in megabytes.
161+
- mem_used_mb (int): Used physical memory in megabytes.
162+
- swap_total_mb (int): Total swap memory in megabytes.
163+
- swap_used_mb (int): Used swap memory in megabytes.
164+
- disk_total_gb (float): Total size of the root disk partition in gigabytes.
165+
- disk_used_gb (float): Used space of the root disk partition in gigabytes.
166+
- process (Dict[str, Any]): A nested dict with per-process metrics:
167+
* pid (int): Current process ID.
168+
* threads (int): Number of active threads.
169+
* rss_mb (float): Resident Set Size memory usage in megabytes.
170+
* vms_mb (float): Virtual Memory Size usage in megabytes.
171+
* open_fds (int | None): Number of open file descriptors, or None if unsupported.
172+
* proc_cpu_percent (float): CPU utilization percentage for this process.
173+
{}: Empty dict if psutil is not installed.
152174
"""
153175
if not psutil:
154176
return {}
177+
178+
# System memory and swap
155179
vm = psutil.virtual_memory()
156180
swap = psutil.swap_memory()
181+
182+
# Load average (Unix); on Windows returns (None, None, None)
157183
try:
158184
load = tuple(round(x, 2) for x in os.getloadavg())
159185
except (AttributeError, OSError):
160186
load = (None, None, None)
187+
188+
# CPU metrics
161189
freq = psutil.cpu_freq()
190+
cpu_pct = psutil.cpu_percent(interval=0.3)
191+
cpu_count = psutil.cpu_count(logical=True)
192+
193+
# Process metrics
162194
proc = psutil.Process()
163195
try:
164196
open_fds = proc.num_fds()
165197
except Exception:
166198
open_fds = None
167-
root = Path(os.getenv("SystemDrive", "C:\\")) if os.name == "nt" else Path("/")
168-
disk = psutil.disk_usage(root)
199+
proc_cpu_pct = proc.cpu_percent(interval=0.1)
200+
rss_mb = round(proc.memory_info().rss / 1_048_576, 2)
201+
vms_mb = round(proc.memory_info().vms / 1_048_576, 2)
202+
threads = proc.num_threads()
203+
pid = proc.pid
204+
205+
# Disk usage for root partition (ensure str on Windows)
206+
root = os.getenv("SystemDrive", "C:\\") if os.name == "nt" else "/"
207+
disk = psutil.disk_usage(str(root))
208+
disk_total_gb = round(disk.total / 1_073_741_824, 2)
209+
disk_used_gb = round(disk.used / 1_073_741_824, 2)
210+
169211
return {
170212
"boot_time": datetime.fromtimestamp(psutil.boot_time()).isoformat(),
171-
"cpu_percent": psutil.cpu_percent(interval=0.3),
172-
"cpu_count": psutil.cpu_count(logical=True),
213+
"cpu_percent": cpu_pct,
214+
"cpu_count": cpu_count,
173215
"cpu_freq_mhz": round(freq.current) if freq else None,
174216
"load_avg": load,
175217
"mem_total_mb": round(vm.total / 1_048_576),
176218
"mem_used_mb": round(vm.used / 1_048_576),
177219
"swap_total_mb": round(swap.total / 1_048_576),
178220
"swap_used_mb": round(swap.used / 1_048_576),
179-
"disk_total_gb": round(disk.total / 1_073_741_824, 2),
180-
"disk_used_gb": round(disk.used / 1_073_741_824, 2),
221+
"disk_total_gb": disk_total_gb,
222+
"disk_used_gb": disk_used_gb,
181223
"process": {
182-
"pid": proc.pid,
183-
"threads": proc.num_threads(),
184-
"rss_mb": round(proc.memory_info().rss / 1_048_576, 2),
185-
"vms_mb": round(proc.memory_info().vms / 1_048_576, 2),
224+
"pid": pid,
225+
"threads": threads,
226+
"rss_mb": rss_mb,
227+
"vms_mb": vms_mb,
186228
"open_fds": open_fds,
187-
"proc_cpu_percent": proc.cpu_percent(interval=0.1),
229+
"proc_cpu_percent": proc_cpu_pct,
188230
},
189231
}
190232

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ dependencies = [
5454
"pydantic-settings>=2.9.1",
5555
"pyjwt>=2.10.1",
5656
"sqlalchemy>=2.0.41",
57+
"psutil>=7.0.0",
5758
"sse-starlette>=2.3.5",
5859
"starlette>=0.46.2",
5960
"uvicorn>=0.34.2",

0 commit comments

Comments
 (0)