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

Commit 8c75667

Browse files
authored
Add prometheus metrics for the number of active pushers (#7103)
1 parent 443162e commit 8c75667

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

changelog.d/7103.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add prometheus metrics for the number of active pushers.

synapse/metrics/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import platform
2121
import threading
2222
import time
23-
from typing import Dict, Union
23+
from typing import Callable, Dict, Iterable, Optional, Tuple, Union
2424

2525
import six
2626

@@ -59,10 +59,12 @@ def collect():
5959
@attr.s(hash=True)
6060
class LaterGauge(object):
6161

62-
name = attr.ib()
63-
desc = attr.ib()
64-
labels = attr.ib(hash=False)
65-
caller = attr.ib()
62+
name = attr.ib(type=str)
63+
desc = attr.ib(type=str)
64+
labels = attr.ib(hash=False, type=Optional[Iterable[str]])
65+
# callback: should either return a value (if there are no labels for this metric),
66+
# or dict mapping from a label tuple to a value
67+
caller = attr.ib(type=Callable[[], Union[Dict[Tuple[str, ...], float], float]])
6668

6769
def collect(self):
6870

synapse/metrics/background_process_metrics.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import threading
1818
from asyncio import iscoroutine
1919
from functools import wraps
20+
from typing import Dict, Set
2021

2122
import six
2223

@@ -80,13 +81,13 @@
8081
# map from description to a counter, so that we can name our logcontexts
8182
# incrementally. (It actually duplicates _background_process_start_count, but
8283
# it's much simpler to do so than to try to combine them.)
83-
_background_process_counts = {} # type: dict[str, int]
84+
_background_process_counts = {} # type: Dict[str, int]
8485

8586
# map from description to the currently running background processes.
8687
#
8788
# it's kept as a dict of sets rather than a big set so that we can keep track
8889
# of process descriptions that no longer have any active processes.
89-
_background_processes = {} # type: dict[str, set[_BackgroundProcess]]
90+
_background_processes = {} # type: Dict[str, Set[_BackgroundProcess]]
9091

9192
# A lock that covers the above dicts
9293
_bg_metrics_lock = threading.Lock()

synapse/push/pusherpool.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@
1515
# limitations under the License.
1616

1717
import logging
18+
from collections import defaultdict
19+
from typing import Dict, Tuple, Union
1820

1921
from twisted.internet import defer
2022

23+
from synapse.metrics import LaterGauge
2124
from synapse.metrics.background_process_metrics import run_as_background_process
2225
from synapse.push import PusherConfigException
26+
from synapse.push.emailpusher import EmailPusher
27+
from synapse.push.httppusher import HttpPusher
2328
from synapse.push.pusher import PusherFactory
2429
from synapse.util.async_helpers import concurrently_execute
2530

@@ -47,7 +52,24 @@ def __init__(self, _hs):
4752
self._should_start_pushers = _hs.config.start_pushers
4853
self.store = self.hs.get_datastore()
4954
self.clock = self.hs.get_clock()
50-
self.pushers = {}
55+
56+
# map from user id to app_id:pushkey to pusher
57+
self.pushers = {} # type: Dict[str, Dict[str, Union[HttpPusher, EmailPusher]]]
58+
59+
def count_pushers():
60+
results = defaultdict(int) # type: Dict[Tuple[str, str], int]
61+
for pushers in self.pushers.values():
62+
for pusher in pushers.values():
63+
k = (type(pusher).__name__, pusher.app_id)
64+
results[k] += 1
65+
return results
66+
67+
LaterGauge(
68+
name="synapse_pushers",
69+
desc="the number of active pushers",
70+
labels=["kind", "app_id"],
71+
caller=count_pushers,
72+
)
5173

5274
def start(self):
5375
"""Starts the pushers off in a background process.

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ commands = mypy \
191191
synapse/handlers/sync.py \
192192
synapse/handlers/ui_auth \
193193
synapse/logging/ \
194+
synapse/metrics \
194195
synapse/module_api \
196+
synapse/push/pusherpool.py \
195197
synapse/replication \
196198
synapse/rest \
197199
synapse/spam_checker_api \

0 commit comments

Comments
 (0)