예제 #1
0
    def test_cache_metric(self):
        """
        Caches produce metrics reflecting their state when scraped.
        """
        CACHE_NAME = "cache_metrics_test_fgjkbdfg"
        cache = DeferredCache(CACHE_NAME, max_entries=777)

        items = {
            x.split(b"{")[0].decode("ascii"): x.split(b" ")[1].decode("ascii")
            for x in filter(
                lambda x: b"cache_metrics_test_fgjkbdfg" in x,
                generate_latest(REGISTRY).split(b"\n"),
            )
        }

        self.assertEqual(items["synapse_util_caches_cache_size"], "0.0")
        self.assertEqual(items["synapse_util_caches_cache_max_size"], "777.0")

        cache.prefill("1", "hi")

        items = {
            x.split(b"{")[0].decode("ascii"): x.split(b" ")[1].decode("ascii")
            for x in filter(
                lambda x: b"cache_metrics_test_fgjkbdfg" in x,
                generate_latest(REGISTRY).split(b"\n"),
            )
        }

        self.assertEqual(items["synapse_util_caches_cache_size"], "1.0")
        self.assertEqual(items["synapse_util_caches_cache_max_size"], "777.0")
    def test_exposed_to_prometheus(self):
        """
        Forward extremity counts are exposed via Prometheus.
        """
        room_creator = self.hs.get_room_creation_handler()

        user = UserID("alice", "test")
        requester = Requester(user, None, False, None, None)

        # Real events, forward extremities
        events = [(3, 2), (6, 2), (4, 6)]

        for event_count, extrems in events:
            info = self.get_success(room_creator.create_room(requester, {}))
            room_id = info["room_id"]

            last_event = None

            # Make a real event chain
            for i in range(event_count):
                ev = self.create_and_send_event(room_id, user, False, last_event)
                last_event = [ev]

            # Sprinkle in some extremities
            for i in range(extrems):
                ev = self.create_and_send_event(room_id, user, False, last_event)

        # Let it run for a while, then pull out the statistics from the
        # Prometheus client registry
        self.reactor.advance(60 * 60 * 1000)
        self.pump(1)

        items = set(
            filter(
                lambda x: b"synapse_forward_extremities_" in x,
                generate_latest(REGISTRY).split(b"\n"),
            )
        )

        expected = set(
            [
                b'synapse_forward_extremities_bucket{le="1.0"} 0.0',
                b'synapse_forward_extremities_bucket{le="2.0"} 2.0',
                b'synapse_forward_extremities_bucket{le="3.0"} 2.0',
                b'synapse_forward_extremities_bucket{le="5.0"} 2.0',
                b'synapse_forward_extremities_bucket{le="7.0"} 3.0',
                b'synapse_forward_extremities_bucket{le="10.0"} 3.0',
                b'synapse_forward_extremities_bucket{le="15.0"} 3.0',
                b'synapse_forward_extremities_bucket{le="20.0"} 3.0',
                b'synapse_forward_extremities_bucket{le="50.0"} 3.0',
                b'synapse_forward_extremities_bucket{le="100.0"} 3.0',
                b'synapse_forward_extremities_bucket{le="200.0"} 3.0',
                b'synapse_forward_extremities_bucket{le="500.0"} 3.0',
                b'synapse_forward_extremities_bucket{le="+Inf"} 3.0',
                b"synapse_forward_extremities_count 3.0",
                b"synapse_forward_extremities_sum 10.0",
            ]
        )

        self.assertEqual(items, expected)
예제 #3
0
 def test_get_build(self):
     """
     The synapse_build_info metric reports the OS version, Python version,
     and Synapse version.
     """
     items = list(
         filter(
             lambda x: b"synapse_build_info{" in x,
             generate_latest(REGISTRY).split(b"\n"),
         ))
     self.assertEqual(len(items), 1)
     self.assertTrue(b"osversion=" in items[0])
     self.assertTrue(b"pythonversion=" in items[0])
     self.assertTrue(b"version=" in items[0])
예제 #4
0
    def test_exposed_to_prometheus(self):
        """
        Forward extremity counts are exposed via Prometheus.
        """
        room_creator = self.hs.get_room_creation_handler()

        user = UserID("alice", "test")
        requester = create_requester(user)

        # Real events, forward extremities
        events = [(3, 2), (6, 2), (4, 6)]

        for event_count, extrems in events:
            info, _ = self.get_success(room_creator.create_room(requester, {}))
            room_id = info["room_id"]

            last_event = None

            # Make a real event chain
            for _ in range(event_count):
                ev = self.create_and_send_event(room_id, user, False,
                                                last_event)
                last_event = [ev]

            # Sprinkle in some extremities
            for _ in range(extrems):
                ev = self.create_and_send_event(room_id, user, False,
                                                last_event)

        # Let it run for a while, then pull out the statistics from the
        # Prometheus client registry
        self.reactor.advance(60 * 60 * 1000)
        self.pump(1)

        items = list(
            filter(
                lambda x: b"synapse_forward_extremities_" in x,
                generate_latest(REGISTRY, emit_help=False).split(b"\n"),
            ))

        expected = [
            b'synapse_forward_extremities_bucket{le="1.0"} 0.0',
            b'synapse_forward_extremities_bucket{le="2.0"} 2.0',
            b'synapse_forward_extremities_bucket{le="3.0"} 2.0',
            b'synapse_forward_extremities_bucket{le="5.0"} 2.0',
            b'synapse_forward_extremities_bucket{le="7.0"} 3.0',
            b'synapse_forward_extremities_bucket{le="10.0"} 3.0',
            b'synapse_forward_extremities_bucket{le="15.0"} 3.0',
            b'synapse_forward_extremities_bucket{le="20.0"} 3.0',
            b'synapse_forward_extremities_bucket{le="50.0"} 3.0',
            b'synapse_forward_extremities_bucket{le="100.0"} 3.0',
            b'synapse_forward_extremities_bucket{le="200.0"} 3.0',
            b'synapse_forward_extremities_bucket{le="500.0"} 3.0',
            # per https://docs.google.com/document/d/1KwV0mAXwwbvvifBvDKH_LU1YjyXE_wxCkHNoCGq1GX0/edit#heading=h.wghdjzzh72j9,
            # "inf" is valid: "this includes variants such as inf"
            b'synapse_forward_extremities_bucket{le="inf"} 3.0',
            b"# TYPE synapse_forward_extremities_gcount gauge",
            b"synapse_forward_extremities_gcount 3.0",
            b"# TYPE synapse_forward_extremities_gsum gauge",
            b"synapse_forward_extremities_gsum 10.0",
        ]
        self.assertEqual(items, expected)