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

Time external cache response time #9904

Merged
merged 5 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/9904.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Time response time for external cache requests.
36 changes: 26 additions & 10 deletions synapse/replication/tcp/external_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import logging
from typing import TYPE_CHECKING, Any, Optional

from prometheus_client import Counter
from prometheus_client import Counter, Histogram

from synapse.logging.context import make_deferred_yieldable
from synapse.util import json_decoder, json_encoder
Expand All @@ -35,6 +35,20 @@
labelnames=["cache_name", "hit"],
)

response_timer = Histogram(
"synapse_external_cache_response_time_seconds",
"Time taken to get a response from Redis for a cache get/set request",
labelnames=["method"],
buckets=(
0.001,
0.002,
0.005,
0.01,
0.02,
0.05,
),
)


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -72,23 +86,25 @@ async def set(self, cache_name: str, key: str, value: Any, expiry_ms: int) -> No

logger.debug("Caching %s %s: %r", cache_name, key, encoded_value)

return await make_deferred_yieldable(
self._redis_connection.set(
self._get_redis_key(cache_name, key),
encoded_value,
pexpire=expiry_ms,
with response_timer.labels("set").time():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to add the cache_name as a label?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, the times shouldn't depend on what we're caching (and I'm a bit cautious about the fact it adds a bunch of extra metrics). Broadly these should be super quick, and if they're not then something is wrong/congested.

return await make_deferred_yieldable(
self._redis_connection.set(
self._get_redis_key(cache_name, key),
encoded_value,
pexpire=expiry_ms,
)
)
)

async def get(self, cache_name: str, key: str) -> Optional[Any]:
"""Look up a key/value in the named cache."""

if self._redis_connection is None:
return None

result = await make_deferred_yieldable(
self._redis_connection.get(self._get_redis_key(cache_name, key))
)
with response_timer.labels("get").time():
result = await make_deferred_yieldable(
self._redis_connection.get(self._get_redis_key(cache_name, key))
)

logger.debug("Got cache result %s %s: %r", cache_name, key, result)

Expand Down