Skip to content

EOL Django Cleaning #453

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

Merged
merged 2 commits into from
Jun 7, 2025
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
33 changes: 15 additions & 18 deletions django_prometheus/cache/backends/redis.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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
9 changes: 0 additions & 9 deletions django_prometheus/db/backends/common.py

This file was deleted.

7 changes: 5 additions & 2 deletions django_prometheus/db/backends/postgis/base.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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


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):
Expand Down
7 changes: 5 additions & 2 deletions django_prometheus/db/backends/postgresql/base.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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


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):
Expand Down
4 changes: 2 additions & 2 deletions django_prometheus/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
12 changes: 4 additions & 8 deletions django_prometheus/tests/end2end/testapp/settings.py
Original file line number Diff line number Diff line change
@@ -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!
Expand Down Expand Up @@ -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",
Expand All @@ -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"
Expand Down
12 changes: 8 additions & 4 deletions django_prometheus/tests/end2end/testapp/test_caches.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
18 changes: 9 additions & 9 deletions django_prometheus/tests/end2end/testapp/urls.py
Original file line number Diff line number Diff line change
@@ -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<location>[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/<str:location>", views.newlawn),
path("file", views.file),
path("", include("django_prometheus.urls")),
re_path(r"^admin/", admin.site.urls),
path("admin/", admin.site.urls),
]