summary refs log tree commit diff
diff options
context:
space:
mode:
authorPaul "LeoNerd" Evans <paul@matrix.org>2016-10-19 14:10:03 +0100
committerPaul "LeoNerd" Evans <paul@matrix.org>2016-10-19 15:05:21 +0100
commit9b0316c75a50de6ce85a952f09247221b25b65a9 (patch)
treeadd3dd537fb12168e3cd9ba9ec9423eb90498df6
parentExport CPU usage metrics also under prometheus-standard metric name (diff)
downloadsynapse-9b0316c75a50de6ce85a952f09247221b25b65a9.tar.xz
Use /proc/self/stat to generate the new process_cpu_*_seconds_total metrics
-rw-r--r--synapse/metrics/__init__.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/synapse/metrics/__init__.py b/synapse/metrics/__init__.py
index e4dd4c61e2..434e7535cc 100644
--- a/synapse/metrics/__init__.py
+++ b/synapse/metrics/__init__.py
@@ -112,13 +112,21 @@ def render_all():
 # Now register some standard process-wide state metrics, to give indications of
 # process resource usage
 
-rusage = None
+TICKS_PER_SEC = 100
 
+rusage = None
+stats = None
 
 def update_resource_metrics():
     global rusage
     rusage = getrusage(RUSAGE_SELF)
 
+    global stats
+    with open("/proc/self/stat") as s:
+        line = s.read()
+        # line is PID (command) more stats go here ...
+        stats = line.split(") ", 1)[1].split(" ")
+
 ## Legacy synapse-invented metric names
 
 resource_metrics = get_metrics_for("process.resource")
@@ -171,13 +179,13 @@ get_metrics_for("process").register_callback("fds", _process_fds, labels=["type"
 process_metrics = get_metrics_for("process");
 
 process_metrics.register_callback(
-    "cpu_user_seconds_total", lambda: rusage.ru_utime
+    "cpu_user_seconds_total", lambda: float(stats[11]) / TICKS_PER_SEC
 )
 process_metrics.register_callback(
-    "cpu_system_seconds_total", lambda: rusage.ru_stime
+    "cpu_system_seconds_total", lambda: float(stats[12]) / TICKS_PER_SEC
 )
 process_metrics.register_callback(
-    "cpu_seconds_total", lambda: rusage.ru_utime + rusage.ru_stime
+    "cpu_seconds_total", lambda: (float(stats[11]) + float(stats[12])) / TICKS_PER_SEC
 )
 
 reactor_metrics = get_metrics_for("reactor")