Exemplo n.º 1
0
    def test_basic(self):
        gauge = InFlightGauge("test1",
                              "",
                              labels=["test_label"],
                              sub_metrics=["foo", "bar"])

        def handle1(metrics):
            metrics.foo += 2
            metrics.bar = max(metrics.bar, 5)

        def handle2(metrics):
            metrics.foo += 3
            metrics.bar = max(metrics.bar, 7)

        gauge.register(("key1", ), handle1)

        self.assert_dict(
            {
                "test1_total": {
                    ("key1", ): 1
                },
                "test1_foo": {
                    ("key1", ): 2
                },
                "test1_bar": {
                    ("key1", ): 5
                },
            },
            self.get_metrics_from_gauge(gauge),
        )

        gauge.unregister(("key1", ), handle1)

        self.assert_dict(
            {
                "test1_total": {
                    ("key1", ): 0
                },
                "test1_foo": {
                    ("key1", ): 0
                },
                "test1_bar": {
                    ("key1", ): 0
                },
            },
            self.get_metrics_from_gauge(gauge),
        )

        gauge.register(("key1", ), handle1)
        gauge.register(("key2", ), handle2)

        self.assert_dict(
            {
                "test1_total": {
                    ("key1", ): 1,
                    ("key2", ): 1
                },
                "test1_foo": {
                    ("key1", ): 2,
                    ("key2", ): 3
                },
                "test1_bar": {
                    ("key1", ): 5,
                    ("key2", ): 7
                },
            },
            self.get_metrics_from_gauge(gauge),
        )

        gauge.unregister(("key2", ), handle2)
        gauge.register(("key1", ), handle2)

        self.assert_dict(
            {
                "test1_total": {
                    ("key1", ): 2,
                    ("key2", ): 0
                },
                "test1_foo": {
                    ("key1", ): 5,
                    ("key2", ): 0
                },
                "test1_bar": {
                    ("key1", ): 7,
                    ("key2", ): 0
                },
            },
            self.get_metrics_from_gauge(gauge),
        )
Exemplo n.º 2
0
block_db_txn_count = Counter("synapse_util_metrics_block_db_txn_count", "",
                             ["block_name"])

# seconds spent waiting for db txns, excluding scheduling time, in this block
block_db_txn_duration = Counter(
    "synapse_util_metrics_block_db_txn_duration_seconds", "", ["block_name"])

# seconds spent waiting for a db connection, in this block
block_db_sched_duration = Counter(
    "synapse_util_metrics_block_db_sched_duration_seconds", "", ["block_name"])

# Tracks the number of blocks currently active
in_flight = InFlightGauge(
    "synapse_util_metrics_block_in_flight",
    "",
    labels=["block_name"],
    sub_metrics=["real_time_max", "real_time_sum"],
)

T = TypeVar("T", bound=Callable[..., Any])


def measure_func(name: Optional[str] = None) -> Callable[[T], T]:
    """
    Used to decorate an async function with a `Measure` context manager.

    Usage:

    @measure_func()
    async def foo(...):
        ...
Exemplo n.º 3
0
    def test_basic(self):
        gauge = InFlightGauge(
            "test1", "",
            labels=["test_label"],
            sub_metrics=["foo", "bar"],
        )

        def handle1(metrics):
            metrics.foo += 2
            metrics.bar = max(metrics.bar, 5)

        def handle2(metrics):
            metrics.foo += 3
            metrics.bar = max(metrics.bar, 7)

        gauge.register(("key1",), handle1)

        self.assert_dict({
            "test1_total": {("key1",): 1},
            "test1_foo": {("key1",): 2},
            "test1_bar": {("key1",): 5},
        }, self.get_metrics_from_gauge(gauge))

        gauge.unregister(("key1",), handle1)

        self.assert_dict({
            "test1_total": {("key1",): 0},
            "test1_foo": {("key1",): 0},
            "test1_bar": {("key1",): 0},
        }, self.get_metrics_from_gauge(gauge))

        gauge.register(("key1",), handle1)
        gauge.register(("key2",), handle2)

        self.assert_dict({
            "test1_total": {("key1",): 1, ("key2",): 1},
            "test1_foo": {("key1",): 2, ("key2",): 3},
            "test1_bar": {("key1",): 5, ("key2",): 7},
        }, self.get_metrics_from_gauge(gauge))

        gauge.unregister(("key2",), handle2)
        gauge.register(("key1",), handle2)

        self.assert_dict({
            "test1_total": {("key1",): 2, ("key2",): 0},
            "test1_foo": {("key1",): 5, ("key2",): 0},
            "test1_bar": {("key1",): 7, ("key2",): 0},
        }, self.get_metrics_from_gauge(gauge))