Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement metrics for redis cache (#79)
- Loading branch information
Showing
7 changed files
with
141 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ env: | |
- DJANGO_VERSION=1.8 | ||
services: | ||
- memcached | ||
- redis | ||
- mysql | ||
- postgresql | ||
matrix: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from django_redis import cache, exceptions | ||
|
||
from django_prometheus.cache.metrics import ( | ||
django_cache_get_fail_total, | ||
django_cache_get_total, | ||
django_cache_hits_total, | ||
django_cache_misses_total, | ||
) | ||
|
||
|
||
class RedisCache(cache.RedisCache): | ||
"""Inherit redis to add metrics about hit/miss/interruption ratio""" | ||
|
||
@cache.omit_exception | ||
def get(self, key, default=None, version=None, client=None): | ||
try: | ||
django_cache_get_total.labels(backend='redis').inc() | ||
cached = self.client.get( | ||
key, | ||
default=None, | ||
version=version, | ||
client=client | ||
) | ||
except exceptions.ConnectionInterrupted as e: | ||
django_cache_get_fail_total.labels(backend='redis').inc() | ||
if cache.DJANGO_REDIS_IGNORE_EXCEPTIONS or self._ignore_exceptions: | ||
if cache.DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS: | ||
cache.logger.error(str(e)) | ||
return default | ||
raise | ||
else: | ||
if cached is not None: | ||
django_cache_hits_total.labels(backend='redis').inc() | ||
else: | ||
django_cache_misses_total.labels(backend='redis').inc() | ||
return cached or default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
from prometheus_client import Counter | ||
|
||
django_cache_get_total = Counter('django_cache_get_total', | ||
'Total get requests on cache', ['backend']) | ||
django_cache_hits_total = Counter('django_cache_get_hits_total', | ||
'Total hits on cache', ['backend']) | ||
django_cache_misses_total = Counter('django_cache_get_misses_total', | ||
'Total misses on cache', ['backend']) | ||
django_cache_get_total = Counter( | ||
'django_cache_get_total', 'Total get requests on cache', ['backend'] | ||
) | ||
django_cache_hits_total = Counter( | ||
'django_cache_get_hits_total', 'Total hits on cache', ['backend'] | ||
) | ||
django_cache_misses_total = Counter( | ||
'django_cache_get_misses_total', 'Total misses on cache', ['backend'] | ||
) | ||
django_cache_get_fail_total = Counter( | ||
'django_cache_get_fail_total', | ||
'Total get request failures by cache', ['backend'] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
Django>=1.8 | ||
django-redis>=4.8 | ||
pep8>=1.6.2 | ||
prometheus-client>=0.0.21 | ||
pip-prometheus>=1.1.0 | ||
|