This commit is contained in:
Iliyan Angelov
2025-09-19 11:58:53 +03:00
parent 306b20e24a
commit 6b247e5b9f
11423 changed files with 1500615 additions and 778 deletions

View File

@@ -0,0 +1,20 @@
from django_memcached_consul import memcached
from django_prometheus.cache.metrics import (
django_cache_get_total,
django_cache_hits_total,
django_cache_misses_total,
)
class MemcachedCache(memcached.MemcachedCache):
"""Inherit django_memcached_consul to add metrics about hit/miss ratio"""
def get(self, key, default=None, version=None):
django_cache_get_total.labels(backend="django_memcached_consul").inc()
cached = super().get(key, default=None, version=version)
if cached is not None:
django_cache_hits_total.labels(backend="django_memcached_consul").inc()
else:
django_cache_misses_total.labels(backend="django_memcached_consul").inc()
return cached or default

View File

@@ -0,0 +1,20 @@
from django.core.cache.backends import filebased
from django_prometheus.cache.metrics import (
django_cache_get_total,
django_cache_hits_total,
django_cache_misses_total,
)
class FileBasedCache(filebased.FileBasedCache):
"""Inherit filebased cache to add metrics about hit/miss ratio"""
def get(self, key, default=None, version=None):
django_cache_get_total.labels(backend="filebased").inc()
cached = super().get(key, default=None, version=version)
if cached is not None:
django_cache_hits_total.labels(backend="filebased").inc()
else:
django_cache_misses_total.labels(backend="filebased").inc()
return cached or default

View File

@@ -0,0 +1,20 @@
from django.core.cache.backends import locmem
from django_prometheus.cache.metrics import (
django_cache_get_total,
django_cache_hits_total,
django_cache_misses_total,
)
class LocMemCache(locmem.LocMemCache):
"""Inherit filebased cache to add metrics about hit/miss ratio"""
def get(self, key, default=None, version=None):
django_cache_get_total.labels(backend="locmem").inc()
cached = super().get(key, default=None, version=version)
if cached is not None:
django_cache_hits_total.labels(backend="locmem").inc()
else:
django_cache_misses_total.labels(backend="locmem").inc()
return cached or default

View File

@@ -0,0 +1,27 @@
from django.core.cache.backends import memcached
from django_prometheus.cache.metrics import (
django_cache_get_total,
django_cache_hits_total,
django_cache_misses_total,
)
class MemcachedPrometheusCacheMixin:
def get(self, key, default=None, version=None):
django_cache_get_total.labels(backend="memcached").inc()
cached = super().get(key, default=None, version=version)
if cached is not None:
django_cache_hits_total.labels(backend="memcached").inc()
return cached
django_cache_misses_total.labels(backend="memcached").inc()
return default
class PyLibMCCache(MemcachedPrometheusCacheMixin, memcached.PyLibMCCache):
"""Inherit memcached to add metrics about hit/miss ratio"""
class PyMemcacheCache(MemcachedPrometheusCacheMixin, memcached.PyMemcacheCache):
"""Inherit memcached to add metrics about hit/miss ratio"""

View File

@@ -0,0 +1,47 @@
from django.core.cache.backends.redis import RedisCache as DjangoRedisCache
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 self._ignore_exceptions:
if self._log_ignored_exceptions:
self.logger.error(str(e))
return default
raise
else:
if cached is not None:
django_cache_hits_total.labels(backend="redis").inc()
return cached
django_cache_misses_total.labels(backend="redis").inc()
return default
class NativeRedisCache(DjangoRedisCache):
def get(self, key, default=None, version=None):
django_cache_get_total.labels(backend="native_redis").inc()
try:
result = super().get(key, default=None, version=version)
except Exception:
django_cache_get_fail_total.labels(backend="native_redis").inc()
raise
if result is not None:
django_cache_hits_total.labels(backend="native_redis").inc()
return result
django_cache_misses_total.labels(backend="native_redis").inc()
return default

View File

@@ -0,0 +1,28 @@
from prometheus_client import Counter
from django_prometheus.conf import NAMESPACE
django_cache_get_total = Counter(
"django_cache_get_total",
"Total get requests on cache",
["backend"],
namespace=NAMESPACE,
)
django_cache_hits_total = Counter(
"django_cache_get_hits_total",
"Total hits on cache",
["backend"],
namespace=NAMESPACE,
)
django_cache_misses_total = Counter(
"django_cache_get_misses_total",
"Total misses on cache",
["backend"],
namespace=NAMESPACE,
)
django_cache_get_fail_total = Counter(
"django_cache_get_fail_total",
"Total get request failures by cache",
["backend"],
namespace=NAMESPACE,
)