From ccc1a3d54d08a90152100f3d57b1b912482281a6 Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Thu, 27 Oct 2016 18:07:34 +0100 Subject: [PATCH 1/3] Allow creation of a 'subspace' within a Metrics object, returning another one --- synapse/metrics/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/synapse/metrics/__init__.py b/synapse/metrics/__init__.py index a6b868775d..f6c084169b 100644 --- a/synapse/metrics/__init__.py +++ b/synapse/metrics/__init__.py @@ -41,6 +41,9 @@ class Metrics(object): def __init__(self, name): self.name_prefix = name + def make_subspace(self, name): + return Metrics("%s_%s" % (self.name_prefix, name)) + def register_collector(self, func): all_collectors.append(func) From aac13b1f9a940f5661d5989e3045b4fc086f1e20 Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Thu, 27 Oct 2016 18:08:15 +0100 Subject: [PATCH 2/3] Pass the Metrics group into the process collector instead of having it find its own one; this avoids it needing to import from synapse.metrics --- synapse/app/homeserver.py | 2 +- synapse/metrics/process_collector.py | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py index f27150d411..6766befc4e 100755 --- a/synapse/app/homeserver.py +++ b/synapse/app/homeserver.py @@ -338,7 +338,7 @@ def setup(config_options): hs.get_replication_layer().start_get_pdu_cache() register_memory_metrics(hs) - register_process_collector() + register_process_collector(get_metrics_for("process")) reactor.callWhenRunning(start) diff --git a/synapse/metrics/process_collector.py b/synapse/metrics/process_collector.py index 1c851d9234..0e95582368 100644 --- a/synapse/metrics/process_collector.py +++ b/synapse/metrics/process_collector.py @@ -20,8 +20,6 @@ import os import stat from resource import getrusage, RUSAGE_SELF -from synapse.metrics import get_metrics_for - TICKS_PER_SEC = 100 BYTES_PER_PAGE = 4096 @@ -111,10 +109,10 @@ def _process_fds(): return counts -def register_process_collector(): +def register_process_collector(process_metrics): # Legacy synapse-invented metric names - resource_metrics = get_metrics_for("process.resource") + resource_metrics = process_metrics.make_subspace("resource") resource_metrics.register_collector(update_resource_metrics) @@ -125,12 +123,10 @@ def register_process_collector(): # kilobytes resource_metrics.register_callback("maxrss", lambda: rusage.ru_maxrss * 1024) - get_metrics_for("process").register_callback("fds", _process_fds, labels=["type"]) + process_metrics.register_callback("fds", _process_fds, labels=["type"]) # New prometheus-standard metric names - process_metrics = get_metrics_for("process") - if HAVE_PROC_SELF_STAT: process_metrics.register_callback( "cpu_user_seconds_total", From 1cc22da6002bc12b11e2e9455143c9f16ed0f5c4 Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Thu, 27 Oct 2016 18:09:34 +0100 Subject: [PATCH 3/3] Set up the process collector during metrics __init__; that way all split-process workers have it --- synapse/app/homeserver.py | 2 -- synapse/metrics/__init__.py | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py index 6766befc4e..54f35900f8 100755 --- a/synapse/app/homeserver.py +++ b/synapse/app/homeserver.py @@ -52,7 +52,6 @@ from synapse.config.homeserver import HomeServerConfig from synapse.crypto import context_factory from synapse.util.logcontext import LoggingContext from synapse.metrics import register_memory_metrics, get_metrics_for -from synapse.metrics.process_collector import register_process_collector from synapse.metrics.resource import MetricsResource, METRICS_PREFIX from synapse.replication.resource import ReplicationResource, REPLICATION_PREFIX from synapse.federation.transport.server import TransportLayerServer @@ -338,7 +337,6 @@ def setup(config_options): hs.get_replication_layer().start_get_pdu_cache() register_memory_metrics(hs) - register_process_collector(get_metrics_for("process")) reactor.callWhenRunning(start) diff --git a/synapse/metrics/__init__.py b/synapse/metrics/__init__.py index f6c084169b..7041da25ce 100644 --- a/synapse/metrics/__init__.py +++ b/synapse/metrics/__init__.py @@ -24,6 +24,7 @@ from .metric import ( CounterMetric, CallbackMetric, DistributionMetric, CacheMetric, MemoryUsageMetric, ) +from .process_collector import register_process_collector logger = logging.getLogger(__name__) @@ -121,6 +122,8 @@ reactor_metrics.register_callback( "gc_counts", lambda: {(i,): v for i, v in enumerate(gc.get_count())}, labels=["gen"] ) +register_process_collector(get_metrics_for("process")) + def runUntilCurrentTimer(func):