Skip to content

Commit

Permalink
Catch psutil errors (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw authored Jan 3, 2024
1 parent 5581be4 commit 43cb7b6
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions python/langsmith/env.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Environment information."""
import functools
import logging
import os
import platform
import subprocess
Expand All @@ -14,6 +15,7 @@
_PSUTIL_AVAILABLE = True
except ImportError:
_PSUTIL_AVAILABLE = False
logger = logging.getLogger(__name__)


def get_runtime_and_metrics() -> dict:
Expand All @@ -23,31 +25,39 @@ def get_runtime_and_metrics() -> dict:

def get_system_metrics() -> Dict[str, Union[float, dict]]:
"""Get CPU and other performance metrics."""
global _PSUTIL_AVAILABLE
if not _PSUTIL_AVAILABLE:
return {}
process = psutil.Process(os.getpid())
metrics: Dict[str, Union[float, dict]] = {}

with process.oneshot():
mem_info = process.memory_info()
metrics["thread_count"] = float(process.num_threads())
metrics["mem"] = {
"rss": float(mem_info.rss),
}
ctx_switches = process.num_ctx_switches()
cpu_times = process.cpu_times()
metrics["cpu"] = {
"time": {
"sys": cpu_times.system,
"user": cpu_times.user,
},
"ctx_switches": {
"voluntary": float(ctx_switches.voluntary),
"involuntary": float(ctx_switches.involuntary),
},
"percent": process.cpu_percent(),
}
return metrics
try:
process = psutil.Process(os.getpid())
metrics: Dict[str, Union[float, dict]] = {}

with process.oneshot():
mem_info = process.memory_info()
metrics["thread_count"] = float(process.num_threads())
metrics["mem"] = {
"rss": float(mem_info.rss),
}
ctx_switches = process.num_ctx_switches()
cpu_times = process.cpu_times()
metrics["cpu"] = {
"time": {
"sys": cpu_times.system,
"user": cpu_times.user,
},
"ctx_switches": {
"voluntary": float(ctx_switches.voluntary),
"involuntary": float(ctx_switches.involuntary),
},
"percent": process.cpu_percent(),
}
return metrics
except Exception as e:
# If psutil is installed but not compatible with the build,
# we'll just cease further attempts to use it.
_PSUTIL_AVAILABLE = False
logger.debug("Failed to get system metrics: %s", e)
return {}


@functools.lru_cache(maxsize=1)
Expand Down

0 comments on commit 43cb7b6

Please sign in to comment.