diff --git a/django_prometheus/cache/backends/redis.py b/django_prometheus/cache/backends/redis.py index bc982dac..e14c7321 100644 --- a/django_prometheus/cache/backends/redis.py +++ b/django_prometheus/cache/backends/redis.py @@ -1,4 +1,4 @@ -from django import VERSION as DJANGO_VERSION +from django.core.cache.backends.redis import RedisCache as DjangoRedisCache from django_redis import cache, exceptions from django_prometheus.cache.metrics import ( @@ -33,20 +33,17 @@ def get(self, key, default=None, version=None, client=None): return default -if DJANGO_VERSION >= (4, 0): - from django.core.cache.backends.redis import RedisCache as DjangoRedisCache - - 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 - else: - django_cache_misses_total.labels(backend="native_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 + else: + django_cache_misses_total.labels(backend="native_redis").inc() + return default diff --git a/django_prometheus/db/backends/common.py b/django_prometheus/db/backends/common.py deleted file mode 100644 index ee66715d..00000000 --- a/django_prometheus/db/backends/common.py +++ /dev/null @@ -1,9 +0,0 @@ -from django import VERSION - - -def get_postgres_cursor_class(): - if VERSION < (4, 2): - from psycopg2.extensions import cursor as cursor_cls - else: - from django.db.backends.postgresql.base import Cursor as cursor_cls - return cursor_cls diff --git a/django_prometheus/db/backends/postgis/base.py b/django_prometheus/db/backends/postgis/base.py index fb322d7b..95d5a9c0 100644 --- a/django_prometheus/db/backends/postgis/base.py +++ b/django_prometheus/db/backends/postgis/base.py @@ -1,6 +1,6 @@ from django.contrib.gis.db.backends.postgis import base +from django.db.backends.postgresql.base import Cursor -from django_prometheus.db.backends.common import get_postgres_cursor_class from django_prometheus.db.common import DatabaseWrapperMixin, ExportingCursorWrapper @@ -8,8 +8,11 @@ class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper): def get_new_connection(self, *args, **kwargs): conn = super().get_new_connection(*args, **kwargs) conn.cursor_factory = ExportingCursorWrapper( - conn.cursor_factory or get_postgres_cursor_class(), "postgis", self.vendor + conn.cursor_factory or Cursor(), + "postgis", + self.vendor, ) + return conn def create_cursor(self, name=None): diff --git a/django_prometheus/db/backends/postgresql/base.py b/django_prometheus/db/backends/postgresql/base.py index 736a66c2..525f8b80 100644 --- a/django_prometheus/db/backends/postgresql/base.py +++ b/django_prometheus/db/backends/postgresql/base.py @@ -1,6 +1,6 @@ from django.db.backends.postgresql import base +from django.db.backends.postgresql.base import Cursor -from django_prometheus.db.backends.common import get_postgres_cursor_class from django_prometheus.db.common import DatabaseWrapperMixin, ExportingCursorWrapper @@ -8,8 +8,11 @@ class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper): def get_new_connection(self, *args, **kwargs): conn = super().get_new_connection(*args, **kwargs) conn.cursor_factory = ExportingCursorWrapper( - conn.cursor_factory or get_postgres_cursor_class(), self.alias, self.vendor + conn.cursor_factory or Cursor(), + self.alias, + self.vendor, ) + return conn def create_cursor(self, name=None): diff --git a/django_prometheus/middleware.py b/django_prometheus/middleware.py index e1ba5c41..86573abc 100644 --- a/django_prometheus/middleware.py +++ b/django_prometheus/middleware.py @@ -219,10 +219,10 @@ def process_request(self, request): self.label_metric(self.metrics.requests_by_transport, request, transport=transport).inc() # Mimic the behaviour of the deprecated "Request.is_ajax()" method. - if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest": + if request.headers.get("x-requested-with") == "XMLHttpRequest": self.label_metric(self.metrics.requests_ajax, request).inc() - content_length = int(request.META.get("CONTENT_LENGTH") or 0) + content_length = int(request.headers.get("content-length") or 0) self.label_metric(self.metrics.requests_body_bytes, request).observe(content_length) request.prometheus_after_middleware_event = Time() diff --git a/django_prometheus/tests/end2end/testapp/settings.py b/django_prometheus/tests/end2end/testapp/settings.py index 3dfc51e8..31b09f1b 100644 --- a/django_prometheus/tests/end2end/testapp/settings.py +++ b/django_prometheus/tests/end2end/testapp/settings.py @@ -1,8 +1,6 @@ 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! @@ -121,6 +119,10 @@ "BACKEND": "django_prometheus.cache.backends.locmem.LocMemCache", "LOCATION": os.path.join(_tmp_cache_dir, "locmem_cache"), }, + "native_redis": { + "BACKEND": "django_prometheus.cache.backends.redis.NativeRedisCache", + "LOCATION": "redis://127.0.0.1:6379/0", + }, "redis": { "BACKEND": "django_prometheus.cache.backends.redis.RedisCache", "LOCATION": "redis://127.0.0.1:6379/1", @@ -137,12 +139,6 @@ }, } -if DJANGO_VERSION >= (4, 0): - CACHES["native_redis"] = { - "BACKEND": "django_prometheus.cache.backends.redis.NativeRedisCache", - "LOCATION": "redis://127.0.0.1:6379/0", - } - # Internationalization LANGUAGE_CODE = "en-us" diff --git a/django_prometheus/tests/end2end/testapp/test_caches.py b/django_prometheus/tests/end2end/testapp/test_caches.py index 713fd2fb..ed1ffcd6 100644 --- a/django_prometheus/tests/end2end/testapp/test_caches.py +++ b/django_prometheus/tests/end2end/testapp/test_caches.py @@ -1,13 +1,17 @@ import pytest -from django import VERSION as DJANGO_VERSION from django.core.cache import caches from redis import RedisError from django_prometheus.testutils import assert_metric_equal, get_metric -_SUPPORTED_CACHES = ["memcached.PyLibMCCache", "memcached.PyMemcacheCache", "filebased", "locmem", "redis"] -if DJANGO_VERSION >= (4, 0): - _SUPPORTED_CACHES.append("native_redis") +_SUPPORTED_CACHES = [ + "memcached.PyLibMCCache", + "memcached.PyMemcacheCache", + "filebased", + "locmem", + "native_redis", + "redis", +] class TestCachesMetrics: diff --git a/django_prometheus/tests/end2end/testapp/urls.py b/django_prometheus/tests/end2end/testapp/urls.py index 85ab72e1..54d722d4 100644 --- a/django_prometheus/tests/end2end/testapp/urls.py +++ b/django_prometheus/tests/end2end/testapp/urls.py @@ -1,16 +1,16 @@ from django.contrib import admin -from django.urls import include, path, re_path +from django.urls import include, path from testapp import views urlpatterns = [ - re_path(r"^$", views.index), - re_path(r"^help$", views.help), - re_path(r"^slow$", views.slow, name="slow"), - re_path(r"^objection$", views.objection), - re_path(r"^sql$", views.sql), - re_path(r"^newlawn/(?P[a-zA-Z0-9 ]+)$", views.newlawn), - re_path(r"^file$", views.file), + path("", views.index), + path("help", views.help), + path("slow", views.slow, name="slow"), + path("objection", views.objection), + path("sql", views.sql), + path("newlawn/", views.newlawn), + path("file", views.file), path("", include("django_prometheus.urls")), - re_path(r"^admin/", admin.site.urls), + path("admin/", admin.site.urls), ]