|
50 | 50 | import fcntl |
51 | 51 | import logging |
52 | 52 | import struct |
53 | | -from collections import defaultdict |
54 | | -from typing import TYPE_CHECKING, DefaultDict, List |
55 | | - |
56 | | -from six import iteritems |
| 53 | +from typing import TYPE_CHECKING, List |
57 | 54 |
|
58 | 55 | from prometheus_client import Counter |
59 | 56 |
|
|
86 | 83 | "synapse_replication_tcp_protocol_close_reason", "", ["reason_type"] |
87 | 84 | ) |
88 | 85 |
|
| 86 | +tcp_inbound_commands_counter = Counter( |
| 87 | + "synapse_replication_tcp_protocol_inbound_commands", |
| 88 | + "Number of commands received from replication, by command and name of process connected to", |
| 89 | + ["command", "name"], |
| 90 | +) |
| 91 | + |
| 92 | +tcp_outbound_commands_counter = Counter( |
| 93 | + "synapse_replication_tcp_protocol_outbound_commands", |
| 94 | + "Number of commands sent to replication, by command and name of process connected to", |
| 95 | + ["command", "name"], |
| 96 | +) |
| 97 | + |
89 | 98 | # A list of all connected protocols. This allows us to send metrics about the |
90 | 99 | # connections. |
91 | 100 | connected_connections = [] |
@@ -151,9 +160,6 @@ def __init__(self, clock: Clock, handler: "ReplicationCommandHandler"): |
151 | 160 | # The LoopingCall for sending pings. |
152 | 161 | self._send_ping_loop = None |
153 | 162 |
|
154 | | - self.inbound_commands_counter = defaultdict(int) # type: DefaultDict[str, int] |
155 | | - self.outbound_commands_counter = defaultdict(int) # type: DefaultDict[str, int] |
156 | | - |
157 | 163 | def connectionMade(self): |
158 | 164 | logger.info("[%s] Connection established", self.id()) |
159 | 165 |
|
@@ -224,9 +230,7 @@ def lineReceived(self, line: bytes): |
224 | 230 |
|
225 | 231 | self.last_received_command = self.clock.time_msec() |
226 | 232 |
|
227 | | - self.inbound_commands_counter[cmd.NAME] = ( |
228 | | - self.inbound_commands_counter[cmd.NAME] + 1 |
229 | | - ) |
| 233 | + tcp_inbound_commands_counter.labels(cmd.NAME, self.name).inc() |
230 | 234 |
|
231 | 235 | # Now lets try and call on_<CMD_NAME> function |
232 | 236 | run_as_background_process( |
@@ -292,9 +296,8 @@ def send_command(self, cmd, do_buffer=True): |
292 | 296 | self._queue_command(cmd) |
293 | 297 | return |
294 | 298 |
|
295 | | - self.outbound_commands_counter[cmd.NAME] = ( |
296 | | - self.outbound_commands_counter[cmd.NAME] + 1 |
297 | | - ) |
| 299 | + tcp_outbound_commands_counter.labels(cmd.NAME, self.name).inc() |
| 300 | + |
298 | 301 | string = "%s %s" % (cmd.NAME, cmd.to_line()) |
299 | 302 | if "\n" in string: |
300 | 303 | raise Exception("Unexpected newline in command: %r", string) |
@@ -546,26 +549,3 @@ def transport_kernel_read_buffer_size(protocol, read=True): |
546 | 549 | for p in connected_connections |
547 | 550 | }, |
548 | 551 | ) |
549 | | - |
550 | | - |
551 | | -tcp_inbound_commands = LaterGauge( |
552 | | - "synapse_replication_tcp_protocol_inbound_commands", |
553 | | - "", |
554 | | - ["command", "name"], |
555 | | - lambda: { |
556 | | - (k, p.name): count |
557 | | - for p in connected_connections |
558 | | - for k, count in iteritems(p.inbound_commands_counter) |
559 | | - }, |
560 | | -) |
561 | | - |
562 | | -tcp_outbound_commands = LaterGauge( |
563 | | - "synapse_replication_tcp_protocol_outbound_commands", |
564 | | - "", |
565 | | - ["command", "name"], |
566 | | - lambda: { |
567 | | - (k, p.name): count |
568 | | - for p in connected_connections |
569 | | - for k, count in iteritems(p.outbound_commands_counter) |
570 | | - }, |
571 | | -) |
0 commit comments