|
20 | 20 | from typing import Any, Callable, Dict, List, Optional, Sized, TypeVar
|
21 | 21 |
|
22 | 22 | import attr
|
| 23 | +from prometheus_client import REGISTRY |
23 | 24 | from prometheus_client.core import Gauge
|
24 | 25 |
|
25 | 26 | from synapse.config.cache import add_resizable_cache
|
| 27 | +from synapse.util.metrics import DynamicCollectorRegistry |
26 | 28 |
|
27 | 29 | logger = logging.getLogger(__name__)
|
28 | 30 |
|
29 | 31 |
|
30 | 32 | # Whether to track estimated memory usage of the LruCaches.
|
31 | 33 | TRACK_MEMORY_USAGE = False
|
32 | 34 |
|
| 35 | +# We track cache metrics in a special registry that lets us update the metrics |
| 36 | +# just before they are returned from the scrape endpoint. |
| 37 | +CACHE_METRIC_REGISTRY = DynamicCollectorRegistry() |
33 | 38 |
|
34 | 39 | caches_by_name: Dict[str, Sized] = {}
|
35 |
| -collectors_by_name: Dict[str, "CacheMetric"] = {} |
36 | 40 |
|
37 |
| -cache_size = Gauge("synapse_util_caches_cache_size", "", ["name"]) |
38 |
| -cache_hits = Gauge("synapse_util_caches_cache_hits", "", ["name"]) |
39 |
| -cache_evicted = Gauge("synapse_util_caches_cache_evicted_size", "", ["name", "reason"]) |
40 |
| -cache_total = Gauge("synapse_util_caches_cache", "", ["name"]) |
41 |
| -cache_max_size = Gauge("synapse_util_caches_cache_max_size", "", ["name"]) |
| 41 | +cache_size = Gauge( |
| 42 | + "synapse_util_caches_cache_size", "", ["name"], registry=CACHE_METRIC_REGISTRY |
| 43 | +) |
| 44 | +cache_hits = Gauge( |
| 45 | + "synapse_util_caches_cache_hits", "", ["name"], registry=CACHE_METRIC_REGISTRY |
| 46 | +) |
| 47 | +cache_evicted = Gauge( |
| 48 | + "synapse_util_caches_cache_evicted_size", |
| 49 | + "", |
| 50 | + ["name", "reason"], |
| 51 | + registry=CACHE_METRIC_REGISTRY, |
| 52 | +) |
| 53 | +cache_total = Gauge( |
| 54 | + "synapse_util_caches_cache", "", ["name"], registry=CACHE_METRIC_REGISTRY |
| 55 | +) |
| 56 | +cache_max_size = Gauge( |
| 57 | + "synapse_util_caches_cache_max_size", "", ["name"], registry=CACHE_METRIC_REGISTRY |
| 58 | +) |
42 | 59 | cache_memory_usage = Gauge(
|
43 | 60 | "synapse_util_caches_cache_size_bytes",
|
44 | 61 | "Estimated memory usage of the caches",
|
45 | 62 | ["name"],
|
| 63 | + registry=CACHE_METRIC_REGISTRY, |
46 | 64 | )
|
47 | 65 |
|
48 |
| -response_cache_size = Gauge("synapse_util_caches_response_cache_size", "", ["name"]) |
49 |
| -response_cache_hits = Gauge("synapse_util_caches_response_cache_hits", "", ["name"]) |
| 66 | +response_cache_size = Gauge( |
| 67 | + "synapse_util_caches_response_cache_size", |
| 68 | + "", |
| 69 | + ["name"], |
| 70 | + registry=CACHE_METRIC_REGISTRY, |
| 71 | +) |
| 72 | +response_cache_hits = Gauge( |
| 73 | + "synapse_util_caches_response_cache_hits", |
| 74 | + "", |
| 75 | + ["name"], |
| 76 | + registry=CACHE_METRIC_REGISTRY, |
| 77 | +) |
50 | 78 | response_cache_evicted = Gauge(
|
51 |
| - "synapse_util_caches_response_cache_evicted_size", "", ["name", "reason"] |
| 79 | + "synapse_util_caches_response_cache_evicted_size", |
| 80 | + "", |
| 81 | + ["name", "reason"], |
| 82 | + registry=CACHE_METRIC_REGISTRY, |
52 | 83 | )
|
53 |
| -response_cache_total = Gauge("synapse_util_caches_response_cache", "", ["name"]) |
| 84 | +response_cache_total = Gauge( |
| 85 | + "synapse_util_caches_response_cache", "", ["name"], registry=CACHE_METRIC_REGISTRY |
| 86 | +) |
| 87 | + |
| 88 | + |
| 89 | +# Register our custom cache metrics registry with the global registry |
| 90 | +REGISTRY.register(CACHE_METRIC_REGISTRY) |
54 | 91 |
|
55 | 92 |
|
56 | 93 | class EvictionReason(Enum):
|
@@ -168,9 +205,8 @@ def register_cache(
|
168 | 205 | add_resizable_cache(cache_name, resize_callback)
|
169 | 206 |
|
170 | 207 | metric = CacheMetric(cache, cache_type, cache_name, collect_callback)
|
171 |
| - metric_name = "cache_%s_%s" % (cache_type, cache_name) |
172 | 208 | caches_by_name[cache_name] = cache
|
173 |
| - collectors_by_name[metric_name] = metric |
| 209 | + CACHE_METRIC_REGISTRY.register_hook(metric.collect) |
174 | 210 | return metric
|
175 | 211 |
|
176 | 212 |
|
|
0 commit comments