Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions synapse/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,18 @@ def register_cache(self, *args, **kwargs):


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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move the rest of the function inside the try block rather than returning here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to keep try...except as narrow as possible, to ensure we don't accidentally consume legitimate errors.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough

metric = MemoryUsageMetric(hs, psutil)
all_metrics.append(metric)
return metric


def get_metrics_for(pkg_name):
Expand Down
5 changes: 2 additions & 3 deletions synapse/metrics/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion synapse/python_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"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": {
Expand All @@ -52,6 +51,9 @@
"ldap": {
"ldap3>=1.0": ["ldap3>=1.0"],
},
"psutil": {
"psutil>=2.0.0": ["psutil>=2.0.0"],
},
}


Expand Down