From 7c1a92274c720f125e8c5bb96b85f05734a58943 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Mon, 8 Aug 2016 11:12:21 +0100 Subject: [PATCH] Make psutil optional --- synapse/metrics/__init__.py | 13 +++++++++++-- synapse/metrics/metric.py | 5 ++--- synapse/python_dependencies.py | 4 +++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/synapse/metrics/__init__.py b/synapse/metrics/__init__.py index cce3dba47..76d5998d7 100644 --- a/synapse/metrics/__init__.py +++ b/synapse/metrics/__init__.py @@ -68,9 +68,18 @@ class Metrics(object): def register_memory_metrics(hs): - metric = MemoryUsageMetric(hs) + try: + import psutil + process = psutil.Process() + process.memory_info().rss + except (ImportError, AttributeError): + logger.warn( + "psutil is not installed or incorrect version." + " Disabling memory metrics." + ) + return + metric = MemoryUsageMetric(hs, psutil) all_metrics.append(metric) - return metric def get_metrics_for(pkg_name): diff --git a/synapse/metrics/metric.py b/synapse/metrics/metric.py index 7becbe049..e81af2989 100644 --- a/synapse/metrics/metric.py +++ b/synapse/metrics/metric.py @@ -16,8 +16,6 @@ from itertools import chain -import psutil - # TODO(paul): I can't believe Python doesn't have one of these def map_concat(func, items): @@ -167,9 +165,10 @@ class MemoryUsageMetric(object): UPDATE_HZ = 2 # number of times to get memory per second WINDOW_SIZE_SEC = 30 # the size of the window in seconds - def __init__(self, hs): + def __init__(self, hs, psutil): clock = hs.get_clock() self.memory_snapshots = [] + self.process = psutil.Process() clock.looping_call(self._update_curr_values, 1000 / self.UPDATE_HZ) diff --git a/synapse/python_dependencies.py b/synapse/python_dependencies.py index 799d35da5..86e3d8915 100644 --- a/synapse/python_dependencies.py +++ b/synapse/python_dependencies.py @@ -36,7 +36,6 @@ REQUIREMENTS = { "blist": ["blist"], "pysaml2>=3.0.0,<4.0.0": ["saml2>=3.0.0,<4.0.0"], "pymacaroons-pynacl": ["pymacaroons"], - "psutil>=2.0.0": ["psutil>=2.0.0"], } CONDITIONAL_REQUIREMENTS = { "web_client": { @@ -52,6 +51,9 @@ CONDITIONAL_REQUIREMENTS = { "ldap": { "ldap3>=1.0": ["ldap3>=1.0"], }, + "psutil": { + "psutil>=2.0.0": ["psutil>=2.0.0"], + }, }