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

Commit 418635e

Browse files
authored
Add a prometheus metric for active cache lookups. (#5750)
* Add a prometheus metric for active cache lookups. * changelog
1 parent 3641784 commit 418635e

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

changelog.d/5750.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a prometheus metric for pending cache lookups.

synapse/util/caches/__init__.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# Copyright 2015, 2016 OpenMarket Ltd
3+
# Copyright 2019 The Matrix.org Foundation C.I.C.
34
#
45
# Licensed under the Apache License, Version 2.0 (the "License");
56
# you may not use this file except in compliance with the License.
@@ -51,7 +52,19 @@ def get_cache_factor_for(cache_name):
5152
response_cache_total = Gauge("synapse_util_caches_response_cache:total", "", ["name"])
5253

5354

54-
def register_cache(cache_type, cache_name, cache):
55+
def register_cache(cache_type, cache_name, cache, collect_callback=None):
56+
"""Register a cache object for metric collection.
57+
58+
Args:
59+
cache_type (str):
60+
cache_name (str): name of the cache
61+
cache (object): cache itself
62+
collect_callback (callable|None): if not None, a function which is called during
63+
metric collection to update additional metrics.
64+
65+
Returns:
66+
CacheMetric: an object which provides inc_{hits,misses,evictions} methods
67+
"""
5568

5669
# Check if the metric is already registered. Unregister it, if so.
5770
# This usually happens during tests, as at runtime these caches are
@@ -90,6 +103,8 @@ def collect(self):
90103
cache_hits.labels(cache_name).set(self.hits)
91104
cache_evicted.labels(cache_name).set(self.evicted_size)
92105
cache_total.labels(cache_name).set(self.hits + self.misses)
106+
if collect_callback:
107+
collect_callback()
93108
except Exception as e:
94109
logger.warn("Error calculating metrics for %s: %s", cache_name, e)
95110
raise

synapse/util/caches/descriptors.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import six
2323
from six import itervalues, string_types
2424

25+
from prometheus_client import Gauge
26+
2527
from twisted.internet import defer
2628

2729
from synapse.logging.context import make_deferred_yieldable, preserve_fn
@@ -37,6 +39,12 @@
3739
logger = logging.getLogger(__name__)
3840

3941

42+
cache_pending_metric = Gauge(
43+
"synapse_util_caches_cache_pending",
44+
"Number of lookups currently pending for this cache",
45+
["name"],
46+
)
47+
4048
_CacheSentinel = object()
4149

4250

@@ -82,11 +90,19 @@ def __init__(self, name, max_entries=1000, keylen=1, tree=False, iterable=False)
8290
self.name = name
8391
self.keylen = keylen
8492
self.thread = None
85-
self.metrics = register_cache("cache", name, self.cache)
93+
self.metrics = register_cache(
94+
"cache",
95+
name,
96+
self.cache,
97+
collect_callback=self._metrics_collection_callback,
98+
)
8699

87100
def _on_evicted(self, evicted_count):
88101
self.metrics.inc_evictions(evicted_count)
89102

103+
def _metrics_collection_callback(self):
104+
cache_pending_metric.labels(self.name).set(len(self._pending_deferred_cache))
105+
90106
def check_thread(self):
91107
expected_thread = self.thread
92108
if expected_thread is None:

0 commit comments

Comments
 (0)