Skip to content

Commit bd21c20

Browse files
geobeaukorfuri
authored andcommitted
Added possibility to export metrics of several caches backends (#69)
* Added metrics for filebased cache * Added more backends and more testings * Fix 2.7 compatibility * fix typos * Removed unwanted comments
1 parent 9e77122 commit bd21c20

File tree

12 files changed

+138
-1
lines changed

12 files changed

+138
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,8 @@ tramp
9191
# cask packages
9292
.cask/
9393

94+
# venv
95+
venv/
96+
9497
### Prometheus ###
9598
examples/prometheus/data

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ env:
77
- DJANGO_VERSION=1.11
88
- DJANGO_VERSION=1.8
99
services:
10+
- memcached
1011
- mysql
1112
- postgresql
1213
matrix:
1314
exclude:
1415
- python: "2.7"
1516
env: DJANGO_VERSION=2.0
16-
17+
1718
install:
1819
- pip install -r requirements.txt
1920
- pip install -q Django==$DJANGO_VERSION

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ DATABASES = {
6868
}
6969
```
7070

71+
### Monitoring your caches
72+
73+
Filebased and memcached caches can be monitored. Just replace
74+
the cache backend to use the one provided by django_prometheus
75+
`django.core.cache.backends` with `django_prometheus.cache.backends`.
76+
77+
```python
78+
CACHES = {
79+
'default': {
80+
'BACKEND': 'django_prometheus.cache.backends.filebased.FileBasedCache',
81+
'LOCATION': '/var/tmp/django_cache',
82+
}
83+
}
84+
```
85+
7186
### Monitoring your models
7287

7388
You may want to monitor the creation/deletion/update rate for your

django_prometheus/cache/__init__.py

Whitespace-only changes.

django_prometheus/cache/backends/__init__.py

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django_memcached_consul import memcached
2+
from django_prometheus.cache.metrics import (
3+
django_cache_get_total, django_cache_hits_total, django_cache_misses_total)
4+
5+
6+
class MemcachedCache(memcached.MemcachedCache):
7+
"""Inherit django_memcached_consul to add metrics about hit/miss ratio"""
8+
9+
def get(self, key, default=None, version=None):
10+
django_cache_get_total.labels(backend='django_memcached_consul').inc()
11+
cached = super(MemcachedCache, self).get(
12+
key, default=None, version=None)
13+
if cached is not None:
14+
django_cache_hits_total.labels(
15+
backend='django_memcached_consul').inc()
16+
else:
17+
django_cache_misses_total.labels(
18+
backend='django_memcached_consul').inc()
19+
return cached
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django.core.cache.backends import filebased
2+
from django_prometheus.cache.metrics import (
3+
django_cache_get_total, django_cache_hits_total, django_cache_misses_total)
4+
5+
6+
class FileBasedCache(filebased.FileBasedCache):
7+
"""Inherit filebased cache to add metrics about hit/miss ratio"""
8+
9+
def get(self, key, default=None, version=None):
10+
django_cache_get_total.labels(backend='filebased').inc()
11+
cached = super(FileBasedCache, self).get(
12+
key, default=None, version=None)
13+
if cached is not None:
14+
django_cache_hits_total.labels(backend='filebased').inc()
15+
else:
16+
django_cache_misses_total.labels(backend='filebased').inc()
17+
return cached
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.core.cache.backends import memcached
2+
from django_prometheus.cache.metrics import (
3+
django_cache_get_total, django_cache_hits_total, django_cache_misses_total)
4+
5+
6+
class MemcachedCache(memcached.MemcachedCache):
7+
"""Inherit memcached to add metrics about hit/miss ratio"""
8+
9+
def get(self, key, default=None, version=None):
10+
django_cache_get_total.labels(backend='memcached').inc()
11+
cached = super(MemcachedCache, self).get(
12+
key, default=None, version=None)
13+
if cached is not None:
14+
django_cache_hits_total.labels(
15+
backend='memcached').inc()
16+
else:
17+
django_cache_misses_total.labels(
18+
backend='memcached').inc()
19+
return cached

django_prometheus/cache/metrics.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from prometheus_client import Counter
2+
3+
django_cache_get_total = Counter('django_cache_get_total',
4+
'Total get requests on cache', ['backend'])
5+
django_cache_hits_total = Counter('django_cache_get_hits_total',
6+
'Total hits on cache', ['backend'])
7+
django_cache_misses_total = Counter('django_cache_get_misses_total',
8+
'Total misses on cache', ['backend'])

django_prometheus/tests/end2end/testapp/settings.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,22 @@ def GetMiddlewareClasses():
135135
},
136136
}
137137

138+
# Caches
139+
140+
CACHES = {
141+
'default': {
142+
'BACKEND': 'django_prometheus.cache.backends.memcached.MemcachedCache',
143+
'LOCATION': 'localhost:11211',
144+
},
145+
'memcached': {
146+
'BACKEND': 'django_prometheus.cache.backends.memcached.MemcachedCache',
147+
'LOCATION': 'localhost:11211',
148+
},
149+
'filebased': {
150+
'BACKEND': 'django_prometheus.cache.backends.filebased.FileBasedCache',
151+
'LOCATION': '/var/tmp/django_cache',
152+
}
153+
}
138154

139155
# Internationalization
140156
# https://docs.djangoproject.com/en/1.8/topics/i18n/

0 commit comments

Comments
 (0)