Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit d841a68

Browse files
committed
Add metrcis to the threadpools
1 parent 6f08f13 commit d841a68

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

synapse/app/_base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from synapse.events.third_party_rules import load_legacy_third_party_event_rules
4646
from synapse.handlers.auth import load_legacy_password_auth_providers
4747
from synapse.logging.context import PreserveLoggingContext
48+
from synapse.metrics import register_threadpool
4849
from synapse.metrics.background_process_metrics import wrap_as_background_process
4950
from synapse.metrics.jemalloc import setup_jemalloc_stats
5051
from synapse.util.caches.lrucache import setup_expire_lru_cache_entries
@@ -382,6 +383,10 @@ def run_sighup(*args, **kwargs):
382383
GAIResolver(reactor, getThreadPool=lambda: resolver_threadpool)
383384
)
384385

386+
# Register the threadpools with our metrics.
387+
register_threadpool("default", reactor.getThreadPool())
388+
register_threadpool("gai_resolver", resolver_threadpool)
389+
385390
# Instantiate the modules so they can register their web resources to the module API
386391
# before we start the listeners.
387392
module_api = hs.get_module_api()

synapse/metrics/__init__.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
)
3333

3434
from twisted.internet import reactor
35+
from twisted.python.threadpool import ThreadPool
3536

3637
import synapse
3738
from synapse.metrics._exposition import (
@@ -522,6 +523,42 @@ def collect(self):
522523
labelnames=("type", "reason"),
523524
)
524525

526+
threadpool_total_threads = Gauge(
527+
"synapse_threadpool_total_threads",
528+
"Total number of threads currently in the threadpool",
529+
["name"],
530+
)
531+
532+
threadpool_total_working_threads = Gauge(
533+
"synapse_threadpool_working_threads",
534+
"Number of threads currently working in the threadpool",
535+
["name"],
536+
)
537+
538+
threadpool_total_min_threads = Gauge(
539+
"synapse_threadpool_min_threads",
540+
"Minimum number of threads configured in the threadpool",
541+
["name"],
542+
)
543+
544+
threadpool_total_max_threads = Gauge(
545+
"synapse_threadpool_max_threads",
546+
"Maximum number of threads configured in the threadpool",
547+
["name"],
548+
)
549+
550+
551+
def register_threadpool(name: str, threadpool: ThreadPool) -> None:
552+
"""Add metrics for the threadpool."""
553+
554+
threadpool_total_min_threads.labels(name).set(threadpool.min)
555+
threadpool_total_max_threads.labels(name).set(threadpool.max)
556+
557+
threadpool_total_threads.labels(name).set_function(lambda: len(threadpool.threads))
558+
threadpool_total_working_threads.labels(name).set_function(
559+
lambda: len(threadpool.working)
560+
)
561+
525562

526563
class ReactorLastSeenMetric:
527564
def collect(self):

0 commit comments

Comments
 (0)