Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support all cache backends of Django >=3.2 #329

Merged
merged 2 commits into from
Jul 14, 2022
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
24 changes: 21 additions & 3 deletions django_prometheus/cache/backends/memcached.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django import VERSION as DJANGO_VERSION
from django.core.cache.backends import memcached

from django_prometheus.cache.metrics import (
Expand All @@ -7,9 +8,7 @@
)


class MemcachedCache(memcached.PyLibMCCache):
"""Inherit memcached to add metrics about hit/miss ratio"""

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)
Expand All @@ -18,3 +17,22 @@ def get(self, key, default=None, version=None):
else:
django_cache_misses_total.labels(backend="memcached").inc()
return cached or default


class MemcachedCache(MemcachedPrometheusCacheMixin, memcached.MemcachedCache):
"""Inherit memcached to add metrics about hit/miss ratio"""

pass


if DJANGO_VERSION >= (3, 2):

class PyLibMCCache(MemcachedPrometheusCacheMixin, memcached.PyLibMCCache):
"""Inherit memcached to add metrics about hit/miss ratio"""

pass

class PyMemcacheCache(MemcachedPrometheusCacheMixin, memcached.PyMemcacheCache):
"""Inherit memcached to add metrics about hit/miss ratio"""

pass
14 changes: 13 additions & 1 deletion django_prometheus/tests/end2end/testapp/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import tempfile

from django import VERSION as DJANGO_VERSION
from testapp.helpers import get_middleware

# SECURITY WARNING: keep the secret key used in production secret!
Expand Down Expand Up @@ -99,7 +100,7 @@
"BACKEND": "django_prometheus.cache.backends.memcached.MemcachedCache",
"LOCATION": "localhost:11211",
},
"memcached": {
"memcached.MemcachedCache": {
"BACKEND": "django_prometheus.cache.backends.memcached.MemcachedCache",
"LOCATION": "localhost:11211",
},
Expand Down Expand Up @@ -127,6 +128,17 @@
},
}


if DJANGO_VERSION >= (3, 2):
CACHES["memcached.PyLibMCCache"] = {
"BACKEND": "django_prometheus.cache.backends.memcached.PyLibMCCache",
"LOCATION": "localhost:11211",
}
CACHES["memcached.PyMemcacheCache"] = {
"BACKEND": "django_prometheus.cache.backends.memcached.PyMemcacheCache",
"LOCATION": "localhost:11211",
}

# Internationalization
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
Expand Down
46 changes: 36 additions & 10 deletions django_prometheus/tests/end2end/testapp/test_caches.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django import VERSION as DJANGO_VERSION
from django.core.cache import caches
from django.test import TestCase
from redis import RedisError
Expand All @@ -9,22 +10,34 @@ class TestCachesMetrics(PrometheusTestCaseMixin, TestCase):
"""Test django_prometheus.caches metrics."""

def testCounters(self):
supported_caches = ["memcached", "filebased", "locmem", "redis"]
supported_caches = [
"memcached.MemcachedCache",
"filebased",
"locmem",
"redis",
]

if DJANGO_VERSION >= (3, 2):
supported_caches.extend(
[
"memcached.PyLibMCCache",
"memcached.PyMemcacheCache",
]
)

# Note: those tests require a memcached server running
for supported_cache in supported_caches:
tested_cache = caches[supported_cache]
backend = supported_cache.split(".")[0]

total_before = (
self.getMetric("django_cache_get_total", backend=supported_cache) or 0
self.getMetric("django_cache_get_total", backend=backend) or 0
)
hit_before = (
self.getMetric("django_cache_get_hits_total", backend=supported_cache)
or 0
self.getMetric("django_cache_get_hits_total", backend=backend) or 0
)
miss_before = (
self.getMetric("django_cache_get_misses_total", backend=supported_cache)
or 0
self.getMetric("django_cache_get_misses_total", backend=backend) or 0
)

tested_cache.set("foo1", "bar")
Expand All @@ -37,15 +50,15 @@ def testCounters(self):
assert result == "default"

self.assertMetricEquals(
total_before + 4, "django_cache_get_total", backend=supported_cache
total_before + 4, "django_cache_get_total", backend=backend
)
self.assertMetricEquals(
hit_before + 2, "django_cache_get_hits_total", backend=supported_cache
hit_before + 2, "django_cache_get_hits_total", backend=backend
)
self.assertMetricEquals(
miss_before + 2,
"django_cache_get_misses_total",
backend=supported_cache,
backend=backend,
)

def test_redis_cache_fail(self):
Expand Down Expand Up @@ -101,7 +114,20 @@ def test_redis_cache_fail(self):
)

def test_cache_version_support(self):
supported_caches = ["memcached", "filebased", "locmem", "redis"]
supported_caches = [
"memcached.MemcachedCache",
"filebased",
"locmem",
"redis",
]

if DJANGO_VERSION >= (3, 2):
supported_caches.extend(
[
"memcached.PyLibMCCache",
"memcached.PyMemcacheCache",
]
)

# Note: those tests require a memcached server running
for supported_cache in supported_caches:
Expand Down
21 changes: 9 additions & 12 deletions django_prometheus/tests/test_testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,15 @@ def testGetMetricVector(self):
vector = self.test_case.getMetricVector(
"some_labelled_gauge", registry=self.registry
)
assert (
sorted(
[
({"labelred": "pink", "labelblue": "indigo"}, 1),
({"labelred": "pink", "labelblue": "royal"}, 2),
({"labelred": "carmin", "labelblue": "indigo"}, 3),
({"labelred": "carmin", "labelblue": "royal"}, 4),
],
key=itemgetter(1),
)
== sorted(vector, key=itemgetter(1))
)
assert sorted(
[
({"labelred": "pink", "labelblue": "indigo"}, 1),
({"labelred": "pink", "labelblue": "royal"}, 2),
({"labelred": "carmin", "labelblue": "indigo"}, 3),
({"labelred": "carmin", "labelblue": "royal"}, 4),
],
key=itemgetter(1),
) == sorted(vector, key=itemgetter(1))

def testAssertMetricEquals(self):
"""Tests assertMetricEquals."""
Expand Down
4 changes: 2 additions & 2 deletions django_prometheus/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ def TimeSince(t):
def PowersOf(logbase, count, lower=0, include_zero=True):
"""Returns a list of count powers of logbase (from logbase**lower)."""
if not include_zero:
return [logbase ** i for i in range(lower, count + lower)]
return [logbase**i for i in range(lower, count + lower)]
else:
return [0] + [logbase ** i for i in range(lower, count + lower)]
return [0] + [logbase**i for i in range(lower, count + lower)]
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ psycopg2
pytest==6.2.5
pytest-django
pylibmc
pymemcache
python-memcached
18 changes: 12 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,25 @@ def get_version():
keywords="django monitoring prometheus",
url="http://github.com/korfuri/django-prometheus",
project_urls={
"Changelog": "https://github.com/korfuri/django-prometheus/blob/master/CHANGELOG.md",
"Documentation": "https://github.com/korfuri/django-prometheus/blob/master/README.md",
"Source": "https://github.com/korfuri/django-prometheus",
"Tracker": "https://github.com/korfuri/django-prometheus/issues",
"Changelog": "https://github.com/korfuri/django-prometheus/blob/master/CHANGELOG.md",
"Documentation": "https://github.com/korfuri/django-prometheus/blob/master/README.md",
"Source": "https://github.com/korfuri/django-prometheus",
"Tracker": "https://github.com/korfuri/django-prometheus/issues",
},
packages=find_packages(exclude=["tests",]),
packages=find_packages(
exclude=[
"tests",
]
),
test_suite="django_prometheus.tests",
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
tests_require=["pytest", "pytest-django"],
setup_requires=["pytest-runner"],
options={"bdist_wheel": {"universal": "1"}},
install_requires=["prometheus-client>=0.7",],
install_requires=[
"prometheus-client>=0.7",
],
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
Expand Down
2 changes: 1 addition & 1 deletion update_version_from_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get_git_version_info():


def prerelease_version():
""" return what the prerelease version should be.
"""return what the prerelease version should be.
https://packaging.python.org/tutorials/distributing-packages/#pre-release-versioning
0.0.2.dev22
"""
Expand Down