|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -import functools |
16 | 15 | import itertools
|
17 | 16 | import logging
|
18 | 17 | import os
|
19 | 18 | import platform
|
20 | 19 | import threading
|
21 |
| -import time |
22 | 20 | from typing import (
|
23 |
| - Any, |
24 | 21 | Callable,
|
25 | 22 | Dict,
|
26 | 23 | Generic,
|
|
33 | 30 | Type,
|
34 | 31 | TypeVar,
|
35 | 32 | Union,
|
36 |
| - cast, |
37 | 33 | )
|
38 | 34 |
|
39 | 35 | import attr
|
|
44 | 40 | GaugeMetricFamily,
|
45 | 41 | )
|
46 | 42 |
|
47 |
| -from twisted.internet import reactor |
48 |
| -from twisted.internet.base import ReactorBase |
49 | 43 | from twisted.python.threadpool import ThreadPool
|
50 | 44 |
|
51 |
| -import synapse |
| 45 | +import synapse.metrics._reactor_metrics |
52 | 46 | from synapse.metrics._exposition import (
|
53 | 47 | MetricsResource,
|
54 | 48 | generate_latest,
|
@@ -368,21 +362,6 @@ def collect(self) -> Iterable[Metric]:
|
368 | 362 | REGISTRY.register(CPUMetrics())
|
369 | 363 |
|
370 | 364 |
|
371 |
| -# |
372 |
| -# Twisted reactor metrics |
373 |
| -# |
374 |
| - |
375 |
| -tick_time = Histogram( |
376 |
| - "python_twisted_reactor_tick_time", |
377 |
| - "Tick time of the Twisted reactor (sec)", |
378 |
| - buckets=[0.001, 0.002, 0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.5, 1, 2, 5], |
379 |
| -) |
380 |
| -pending_calls_metric = Histogram( |
381 |
| - "python_twisted_reactor_pending_calls", |
382 |
| - "Pending calls", |
383 |
| - buckets=[1, 2, 5, 10, 25, 50, 100, 250, 500, 1000], |
384 |
| -) |
385 |
| - |
386 | 365 | #
|
387 | 366 | # Federation Metrics
|
388 | 367 | #
|
@@ -434,8 +413,6 @@ def collect(self) -> Iterable[Metric]:
|
434 | 413 | " ".join([platform.system(), platform.release()]),
|
435 | 414 | ).set(1)
|
436 | 415 |
|
437 |
| -last_ticked = time.time() |
438 |
| - |
439 | 416 | # 3PID send info
|
440 | 417 | threepid_send_requests = Histogram(
|
441 | 418 | "synapse_threepid_send_requests_with_tries",
|
@@ -483,75 +460,6 @@ def register_threadpool(name: str, threadpool: ThreadPool) -> None:
|
483 | 460 | )
|
484 | 461 |
|
485 | 462 |
|
486 |
| -class ReactorLastSeenMetric: |
487 |
| - def collect(self) -> Iterable[Metric]: |
488 |
| - cm = GaugeMetricFamily( |
489 |
| - "python_twisted_reactor_last_seen", |
490 |
| - "Seconds since the Twisted reactor was last seen", |
491 |
| - ) |
492 |
| - cm.add_metric([], time.time() - last_ticked) |
493 |
| - yield cm |
494 |
| - |
495 |
| - |
496 |
| -REGISTRY.register(ReactorLastSeenMetric()) |
497 |
| - |
498 |
| -F = TypeVar("F", bound=Callable[..., Any]) |
499 |
| - |
500 |
| - |
501 |
| -def runUntilCurrentTimer(reactor: ReactorBase, func: F) -> F: |
502 |
| - @functools.wraps(func) |
503 |
| - def f(*args: Any, **kwargs: Any) -> Any: |
504 |
| - now = reactor.seconds() |
505 |
| - num_pending = 0 |
506 |
| - |
507 |
| - # _newTimedCalls is one long list of *all* pending calls. Below loop |
508 |
| - # is based off of impl of reactor.runUntilCurrent |
509 |
| - for delayed_call in reactor._newTimedCalls: |
510 |
| - if delayed_call.time > now: |
511 |
| - break |
512 |
| - |
513 |
| - if delayed_call.delayed_time > 0: |
514 |
| - continue |
515 |
| - |
516 |
| - num_pending += 1 |
517 |
| - |
518 |
| - num_pending += len(reactor.threadCallQueue) |
519 |
| - start = time.time() |
520 |
| - ret = func(*args, **kwargs) |
521 |
| - end = time.time() |
522 |
| - |
523 |
| - # record the amount of wallclock time spent running pending calls. |
524 |
| - # This is a proxy for the actual amount of time between reactor polls, |
525 |
| - # since about 25% of time is actually spent running things triggered by |
526 |
| - # I/O events, but that is harder to capture without rewriting half the |
527 |
| - # reactor. |
528 |
| - tick_time.observe(end - start) |
529 |
| - pending_calls_metric.observe(num_pending) |
530 |
| - |
531 |
| - # Update the time we last ticked, for the metric to test whether |
532 |
| - # Synapse's reactor has frozen |
533 |
| - global last_ticked |
534 |
| - last_ticked = end |
535 |
| - |
536 |
| - return ret |
537 |
| - |
538 |
| - return cast(F, f) |
539 |
| - |
540 |
| - |
541 |
| -try: |
542 |
| - # Ensure the reactor has all the attributes we expect |
543 |
| - reactor.seconds # type: ignore |
544 |
| - reactor.runUntilCurrent # type: ignore |
545 |
| - reactor._newTimedCalls # type: ignore |
546 |
| - reactor.threadCallQueue # type: ignore |
547 |
| - |
548 |
| - # runUntilCurrent is called when we have pending calls. It is called once |
549 |
| - # per iteratation after fd polling. |
550 |
| - reactor.runUntilCurrent = runUntilCurrentTimer(reactor, reactor.runUntilCurrent) # type: ignore |
551 |
| -except AttributeError: |
552 |
| - pass |
553 |
| - |
554 |
| - |
555 | 463 | __all__ = [
|
556 | 464 | "MetricsResource",
|
557 | 465 | "generate_latest",
|
|
0 commit comments