django-prometheus 2.4.0.dev28__tar.gz → 2.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {django_prometheus-2.4.0.dev28/django_prometheus.egg-info → django_prometheus-2.5.0}/PKG-INFO +5 -2
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/__init__.py +2 -3
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/cache/backends/memcached.py +0 -4
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/cache/backends/redis.py +4 -6
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/db/backends/mysql/base.py +0 -2
- django_prometheus-2.5.0/django_prometheus/db/backends/postgis/base.py +16 -0
- django_prometheus-2.5.0/django_prometheus/db/backends/postgresql/base.py +16 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/db/backends/spatialite/base.py +0 -2
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/db/backends/sqlite3/base.py +0 -2
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/db/common.py +0 -1
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/exports.py +1 -1
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/migrations.py +0 -1
- django_prometheus-2.5.0/django_prometheus/tests/test_db_wrapper.py +192 -0
- django_prometheus-2.5.0/django_prometheus/tests/test_django_prometheus.py +11 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/tests/test_testutils.py +30 -15
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/utils.py +2 -2
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0/django_prometheus.egg-info}/PKG-INFO +5 -2
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus.egg-info/SOURCES.txt +1 -0
- django_prometheus-2.5.0/django_prometheus.egg-info/requires.txt +2 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/setup.py +5 -2
- django_prometheus-2.4.0.dev28/django_prometheus/db/backends/postgis/base.py +0 -21
- django_prometheus-2.4.0.dev28/django_prometheus/db/backends/postgresql/base.py +0 -21
- django_prometheus-2.4.0.dev28/django_prometheus/tests/test_django_prometheus.py +0 -11
- django_prometheus-2.4.0.dev28/django_prometheus.egg-info/requires.txt +0 -1
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/LICENSE +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/MANIFEST.in +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/README.md +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/apps.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/cache/__init__.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/cache/backends/__init__.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/cache/backends/django_memcached_consul.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/cache/backends/filebased.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/cache/backends/locmem.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/cache/metrics.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/conf/__init__.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/db/__init__.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/db/backends/__init__.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/db/backends/mysql/__init__.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/db/backends/postgis/__init__.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/db/backends/postgresql/__init__.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/db/backends/spatialite/__init__.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/db/backends/sqlite3/__init__.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/db/metrics.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/middleware.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/models.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/tests/__init__.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/tests/test_exports.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/testutils.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/urls.py +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus.egg-info/dependency_links.txt +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus.egg-info/top_level.txt +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/pyproject.toml +0 -0
- {django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/setup.cfg +0 -0
{django_prometheus-2.4.0.dev28/django_prometheus.egg-info → django_prometheus-2.5.0}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django-prometheus
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.5.0
|
|
4
4
|
Summary: Django middlewares to monitor your application with Prometheus.io.
|
|
5
5
|
Home-page: http://github.com/korfuri/django-prometheus
|
|
6
6
|
Author: Uriel Corfa
|
|
@@ -21,14 +21,17 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
21
21
|
Classifier: Programming Language :: Python :: 3.11
|
|
22
22
|
Classifier: Programming Language :: Python :: 3.12
|
|
23
23
|
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
25
|
+
Classifier: Programming Language :: Python :: Free Threading
|
|
24
26
|
Classifier: Framework :: Django :: 4.2
|
|
25
|
-
Classifier: Framework :: Django :: 5.0
|
|
26
27
|
Classifier: Framework :: Django :: 5.1
|
|
27
28
|
Classifier: Framework :: Django :: 5.2
|
|
29
|
+
Classifier: Framework :: Django :: 6.0
|
|
28
30
|
Classifier: Topic :: System :: Monitoring
|
|
29
31
|
Classifier: License :: OSI Approved :: Apache Software License
|
|
30
32
|
Description-Content-Type: text/markdown
|
|
31
33
|
License-File: LICENSE
|
|
34
|
+
Requires-Dist: Django!=5.0.*,<6.1,>=4.2
|
|
32
35
|
Requires-Dist: prometheus-client>=0.7
|
|
33
36
|
|
|
34
37
|
# django-prometheus
|
|
@@ -10,12 +10,11 @@ from django_prometheus import middleware, models
|
|
|
10
10
|
|
|
11
11
|
__all__ = ["middleware", "models", "pip_prometheus"]
|
|
12
12
|
|
|
13
|
-
__version__ = "2.
|
|
14
|
-
|
|
13
|
+
__version__ = "2.5.0"
|
|
15
14
|
|
|
16
15
|
# Import pip_prometheus to export the pip metrics automatically.
|
|
17
16
|
try:
|
|
18
17
|
import pip_prometheus
|
|
19
18
|
except ImportError:
|
|
20
19
|
# If people don't have pip, don't export anything.
|
|
21
|
-
pass
|
|
20
|
+
pass
|
|
@@ -22,10 +22,6 @@ class MemcachedPrometheusCacheMixin:
|
|
|
22
22
|
class PyLibMCCache(MemcachedPrometheusCacheMixin, memcached.PyLibMCCache):
|
|
23
23
|
"""Inherit memcached to add metrics about hit/miss ratio"""
|
|
24
24
|
|
|
25
|
-
pass
|
|
26
|
-
|
|
27
25
|
|
|
28
26
|
class PyMemcacheCache(MemcachedPrometheusCacheMixin, memcached.PyMemcacheCache):
|
|
29
27
|
"""Inherit memcached to add metrics about hit/miss ratio"""
|
|
30
|
-
|
|
31
|
-
pass
|
{django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/cache/backends/redis.py
RENAMED
|
@@ -28,9 +28,8 @@ class RedisCache(cache.RedisCache):
|
|
|
28
28
|
if cached is not None:
|
|
29
29
|
django_cache_hits_total.labels(backend="redis").inc()
|
|
30
30
|
return cached
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return default
|
|
31
|
+
django_cache_misses_total.labels(backend="redis").inc()
|
|
32
|
+
return default
|
|
34
33
|
|
|
35
34
|
|
|
36
35
|
class NativeRedisCache(DjangoRedisCache):
|
|
@@ -44,6 +43,5 @@ class NativeRedisCache(DjangoRedisCache):
|
|
|
44
43
|
if result is not None:
|
|
45
44
|
django_cache_hits_total.labels(backend="native_redis").inc()
|
|
46
45
|
return result
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return default
|
|
46
|
+
django_cache_misses_total.labels(backend="native_redis").inc()
|
|
47
|
+
return default
|
|
@@ -6,8 +6,6 @@ from django_prometheus.db.common import DatabaseWrapperMixin, ExportingCursorWra
|
|
|
6
6
|
class DatabaseFeatures(base.DatabaseFeatures):
|
|
7
7
|
"""Our database has the exact same features as the base one."""
|
|
8
8
|
|
|
9
|
-
pass
|
|
10
|
-
|
|
11
9
|
|
|
12
10
|
class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper):
|
|
13
11
|
CURSOR_CLASS = base.CursorWrapper
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from django.contrib.gis.db.backends.postgis import base
|
|
2
|
+
|
|
3
|
+
from django_prometheus.db.common import DatabaseWrapperMixin, ExportingCursorWrapper
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper):
|
|
7
|
+
def get_connection_params(self):
|
|
8
|
+
conn_params = super().get_connection_params()
|
|
9
|
+
conn_params["cursor_factory"] = ExportingCursorWrapper(conn_params["cursor_factory"], self.alias, self.vendor)
|
|
10
|
+
return conn_params
|
|
11
|
+
|
|
12
|
+
def create_cursor(self, name=None):
|
|
13
|
+
# cursor_factory is set in get_connection_params() so psycopg already
|
|
14
|
+
# creates instrumented cursors. Delegate to Django's implementation to
|
|
15
|
+
# avoid the mixin's create_cursor which is incompatible with psycopg3.
|
|
16
|
+
return base.DatabaseWrapper.create_cursor(self, name=name)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from django.db.backends.postgresql import base
|
|
2
|
+
|
|
3
|
+
from django_prometheus.db.common import DatabaseWrapperMixin, ExportingCursorWrapper
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper):
|
|
7
|
+
def get_connection_params(self):
|
|
8
|
+
conn_params = super().get_connection_params()
|
|
9
|
+
conn_params["cursor_factory"] = ExportingCursorWrapper(conn_params["cursor_factory"], self.alias, self.vendor)
|
|
10
|
+
return conn_params
|
|
11
|
+
|
|
12
|
+
def create_cursor(self, name=None):
|
|
13
|
+
# cursor_factory is set in get_connection_params() so psycopg already
|
|
14
|
+
# creates instrumented cursors. Delegate to Django's implementation to
|
|
15
|
+
# avoid the mixin's create_cursor which is incompatible with psycopg3.
|
|
16
|
+
return base.DatabaseWrapper.create_cursor(self, name=name)
|
|
@@ -7,8 +7,6 @@ from django_prometheus.db.common import DatabaseWrapperMixin
|
|
|
7
7
|
class DatabaseFeatures(features.DatabaseFeatures):
|
|
8
8
|
"""Our database has the exact same features as the base one."""
|
|
9
9
|
|
|
10
|
-
pass
|
|
11
|
-
|
|
12
10
|
|
|
13
11
|
class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper):
|
|
14
12
|
CURSOR_CLASS = sqlite_base.SQLiteCursorWrapper
|
|
@@ -6,8 +6,6 @@ from django_prometheus.db.common import DatabaseWrapperMixin
|
|
|
6
6
|
class DatabaseFeatures(base.DatabaseFeatures):
|
|
7
7
|
"""Our database has the exact same features as the base one."""
|
|
8
8
|
|
|
9
|
-
pass
|
|
10
|
-
|
|
11
9
|
|
|
12
10
|
class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper):
|
|
13
11
|
CURSOR_CLASS = base.SQLiteCursorWrapper
|
|
@@ -21,7 +21,7 @@ logger = logging.getLogger(__name__)
|
|
|
21
21
|
def SetupPrometheusEndpointOnPort(port, addr=""):
|
|
22
22
|
"""Exports Prometheus metrics on an HTTPServer running in its own thread.
|
|
23
23
|
|
|
24
|
-
The server runs on the given port and is by default
|
|
24
|
+
The server runs on the given port and is by default listening on
|
|
25
25
|
all interfaces. This HTTPServer is fully independent of Django and
|
|
26
26
|
its stack. This offers the advantage that even if Django becomes
|
|
27
27
|
unable to respond, the HTTPServer will continue to function and
|
|
@@ -31,7 +31,6 @@ def ExportMigrations():
|
|
|
31
31
|
This is meant to be called during app startup, ideally by
|
|
32
32
|
django_prometheus.apps.AppConfig.
|
|
33
33
|
"""
|
|
34
|
-
|
|
35
34
|
# Import MigrationExecutor lazily. MigrationExecutor checks at
|
|
36
35
|
# import time that the apps are ready, and they are not when
|
|
37
36
|
# django_prometheus is imported. ExportMigrations() should be
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"""Tests for the PostgreSQL and PostGIS database backend wrappers.
|
|
2
|
+
|
|
3
|
+
Verifies that cursor_factory wrapping works correctly and does not
|
|
4
|
+
re-wrap when connections are reused from a pool (issue #445).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from unittest.mock import MagicMock, patch
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
from django_prometheus.db.common import ExportingCursorWrapper
|
|
12
|
+
|
|
13
|
+
try:
|
|
14
|
+
from django.contrib.gis.gdal import libgdal # noqa: F401
|
|
15
|
+
|
|
16
|
+
_has_gdal = True
|
|
17
|
+
except Exception:
|
|
18
|
+
_has_gdal = False
|
|
19
|
+
|
|
20
|
+
import os
|
|
21
|
+
|
|
22
|
+
_can_import_postgis_backend = False
|
|
23
|
+
if os.environ.get("DJANGO_SETTINGS_MODULE"):
|
|
24
|
+
try:
|
|
25
|
+
import django.contrib.gis.db.backends.postgis.base # noqa: F401
|
|
26
|
+
|
|
27
|
+
_can_import_postgis_backend = True
|
|
28
|
+
except Exception:
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class DummyCursor:
|
|
33
|
+
"""A stand-in cursor class used as the base cursor_factory."""
|
|
34
|
+
|
|
35
|
+
def execute(self, *args, **kwargs):
|
|
36
|
+
pass
|
|
37
|
+
|
|
38
|
+
def executemany(self, query, param_list, *args, **kwargs):
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class TestExportingCursorWrapper:
|
|
43
|
+
def test_wraps_cursor_class(self):
|
|
44
|
+
"""ExportingCursorWrapper returns a subclass of the given cursor class."""
|
|
45
|
+
wrapped = ExportingCursorWrapper(DummyCursor, "default", "postgresql")
|
|
46
|
+
assert issubclass(wrapped, DummyCursor)
|
|
47
|
+
|
|
48
|
+
def test_double_wrapping_increases_mro(self):
|
|
49
|
+
"""Wrapping an already-wrapped class creates deeper inheritance.
|
|
50
|
+
|
|
51
|
+
This is the root cause of #445: each re-wrap adds a layer to the
|
|
52
|
+
MRO, so execute() traverses more super() calls over time.
|
|
53
|
+
"""
|
|
54
|
+
wrapped_once = ExportingCursorWrapper(DummyCursor, "default", "postgresql")
|
|
55
|
+
wrapped_twice = ExportingCursorWrapper(wrapped_once, "default", "postgresql")
|
|
56
|
+
assert len(wrapped_twice.__mro__) > len(wrapped_once.__mro__)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class TestPostgresqlGetConnectionParams:
|
|
60
|
+
"""Test that the postgresql backend wraps cursor_factory in get_connection_params."""
|
|
61
|
+
|
|
62
|
+
def _make_wrapper(self):
|
|
63
|
+
"""Create a DatabaseWrapper instance with mocked internals."""
|
|
64
|
+
from django_prometheus.db.backends.postgresql import base as pg_base
|
|
65
|
+
|
|
66
|
+
wrapper = pg_base.DatabaseWrapper.__new__(pg_base.DatabaseWrapper)
|
|
67
|
+
wrapper.alias = "default"
|
|
68
|
+
wrapper.vendor = "postgresql"
|
|
69
|
+
return wrapper
|
|
70
|
+
|
|
71
|
+
@patch("django.db.backends.postgresql.base.DatabaseWrapper.get_connection_params")
|
|
72
|
+
def test_wraps_cursor_factory(self, mock_super_params):
|
|
73
|
+
mock_super_params.return_value = {"cursor_factory": DummyCursor}
|
|
74
|
+
wrapper = self._make_wrapper()
|
|
75
|
+
|
|
76
|
+
params = wrapper.get_connection_params()
|
|
77
|
+
|
|
78
|
+
assert params["cursor_factory"] is not DummyCursor
|
|
79
|
+
assert issubclass(params["cursor_factory"], DummyCursor)
|
|
80
|
+
|
|
81
|
+
@patch("django.db.backends.postgresql.base.DatabaseWrapper.get_connection_params")
|
|
82
|
+
def test_wrapping_is_single_layer(self, mock_super_params):
|
|
83
|
+
"""Calling get_connection_params() always wraps the base cursor class,
|
|
84
|
+
not a previously wrapped one, because super() returns the original params.
|
|
85
|
+
|
|
86
|
+
With connection pooling, get_connection_params() is called once at pool
|
|
87
|
+
creation, so the cursor_factory is wrapped exactly once per pool.
|
|
88
|
+
"""
|
|
89
|
+
mock_super_params.side_effect = lambda: {"cursor_factory": DummyCursor}
|
|
90
|
+
wrapper = self._make_wrapper()
|
|
91
|
+
|
|
92
|
+
params_first = wrapper.get_connection_params()
|
|
93
|
+
params_second = wrapper.get_connection_params()
|
|
94
|
+
|
|
95
|
+
# Both calls produce the same MRO depth because super() always
|
|
96
|
+
# returns the unwrapped DummyCursor as the base.
|
|
97
|
+
assert len(params_first["cursor_factory"].__mro__) == len(params_second["cursor_factory"].__mro__)
|
|
98
|
+
|
|
99
|
+
@patch("django.db.backends.postgresql.base.DatabaseWrapper.get_new_connection")
|
|
100
|
+
def test_get_new_connection_does_not_rewrap(self, mock_super_conn):
|
|
101
|
+
"""get_new_connection() no longer modifies cursor_factory on the
|
|
102
|
+
connection, so pooled connections are not re-wrapped.
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
mock_conn = MagicMock()
|
|
106
|
+
already_wrapped = ExportingCursorWrapper(DummyCursor, "default", "postgresql")
|
|
107
|
+
mock_conn.cursor_factory = already_wrapped
|
|
108
|
+
mock_super_conn.return_value = mock_conn
|
|
109
|
+
|
|
110
|
+
wrapper = self._make_wrapper()
|
|
111
|
+
conn = wrapper.get_new_connection({})
|
|
112
|
+
|
|
113
|
+
# cursor_factory should be untouched — still the same class
|
|
114
|
+
assert conn.cursor_factory is already_wrapped
|
|
115
|
+
|
|
116
|
+
@patch("django.db.backends.postgresql.base.DatabaseWrapper.create_cursor")
|
|
117
|
+
def test_create_cursor_delegates_to_django(self, mock_django_create_cursor):
|
|
118
|
+
"""create_cursor() delegates to Django's base implementation,
|
|
119
|
+
not the mixin's version which is incompatible with psycopg3.
|
|
120
|
+
"""
|
|
121
|
+
mock_django_create_cursor.return_value = MagicMock()
|
|
122
|
+
wrapper = self._make_wrapper()
|
|
123
|
+
|
|
124
|
+
wrapper.create_cursor(name=None)
|
|
125
|
+
|
|
126
|
+
# Called as an unbound method: base.DatabaseWrapper.create_cursor(self, name=None)
|
|
127
|
+
mock_django_create_cursor.assert_called_once_with(wrapper, name=None)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@pytest.mark.skipif(
|
|
131
|
+
not _has_gdal or not _can_import_postgis_backend,
|
|
132
|
+
reason="PostGIS tests require GDAL and a fully configured Django environment",
|
|
133
|
+
)
|
|
134
|
+
class TestPostgisGetConnectionParams:
|
|
135
|
+
"""Test that the postgis backend wraps cursor_factory in get_connection_params."""
|
|
136
|
+
|
|
137
|
+
def _get_django_postgis_dbwrapper(self):
|
|
138
|
+
"""Return the Django PostGIS DatabaseWrapper class for patch.object()."""
|
|
139
|
+
from django.contrib.gis.db.backends.postgis.base import DatabaseWrapper
|
|
140
|
+
|
|
141
|
+
return DatabaseWrapper
|
|
142
|
+
|
|
143
|
+
def _make_wrapper(self):
|
|
144
|
+
"""Create a PostGIS DatabaseWrapper instance with mocked internals."""
|
|
145
|
+
from django_prometheus.db.backends.postgis import base as gis_base
|
|
146
|
+
|
|
147
|
+
wrapper = gis_base.DatabaseWrapper.__new__(gis_base.DatabaseWrapper)
|
|
148
|
+
wrapper.alias = "geo_db"
|
|
149
|
+
wrapper.vendor = "postgresql"
|
|
150
|
+
return wrapper
|
|
151
|
+
|
|
152
|
+
def test_wraps_cursor_factory(self):
|
|
153
|
+
with patch.object(self._get_django_postgis_dbwrapper(), "get_connection_params") as mock_super_params:
|
|
154
|
+
mock_super_params.return_value = {"cursor_factory": DummyCursor}
|
|
155
|
+
wrapper = self._make_wrapper()
|
|
156
|
+
|
|
157
|
+
params = wrapper.get_connection_params()
|
|
158
|
+
|
|
159
|
+
assert params["cursor_factory"] is not DummyCursor
|
|
160
|
+
assert issubclass(params["cursor_factory"], DummyCursor)
|
|
161
|
+
|
|
162
|
+
def test_wrapping_is_single_layer(self):
|
|
163
|
+
"""Each call wraps the original cursor class, not a previously wrapped one."""
|
|
164
|
+
with patch.object(self._get_django_postgis_dbwrapper(), "get_connection_params") as mock_super_params:
|
|
165
|
+
mock_super_params.side_effect = lambda: {"cursor_factory": DummyCursor}
|
|
166
|
+
wrapper = self._make_wrapper()
|
|
167
|
+
|
|
168
|
+
params_first = wrapper.get_connection_params()
|
|
169
|
+
params_second = wrapper.get_connection_params()
|
|
170
|
+
|
|
171
|
+
assert len(params_first["cursor_factory"].__mro__) == len(params_second["cursor_factory"].__mro__)
|
|
172
|
+
|
|
173
|
+
def test_uses_self_alias(self):
|
|
174
|
+
"""The postgis backend uses self.alias, not a hardcoded string."""
|
|
175
|
+
with patch.object(self._get_django_postgis_dbwrapper(), "get_connection_params") as mock_super_params:
|
|
176
|
+
mock_super_params.return_value = {"cursor_factory": DummyCursor}
|
|
177
|
+
wrapper = self._make_wrapper()
|
|
178
|
+
|
|
179
|
+
with patch("django_prometheus.db.common.ExportingCursorWrapper") as mock_ecw:
|
|
180
|
+
mock_ecw.return_value = DummyCursor
|
|
181
|
+
wrapper.get_connection_params()
|
|
182
|
+
mock_ecw.assert_called_once_with(DummyCursor, "geo_db", "postgresql")
|
|
183
|
+
|
|
184
|
+
def test_create_cursor_delegates_to_django(self):
|
|
185
|
+
"""create_cursor() delegates to Django's postgis base implementation."""
|
|
186
|
+
with patch.object(self._get_django_postgis_dbwrapper(), "create_cursor") as mock_django_create_cursor:
|
|
187
|
+
mock_django_create_cursor.return_value = MagicMock()
|
|
188
|
+
wrapper = self._make_wrapper()
|
|
189
|
+
|
|
190
|
+
wrapper.create_cursor(name=None)
|
|
191
|
+
|
|
192
|
+
mock_django_create_cursor.assert_called_once_with(wrapper, name=None)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
from django_prometheus.utils import PowersOf
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class TestDjangoPrometheus:
|
|
6
|
+
def testPowersOf(self):
|
|
7
|
+
"""Tests utils.PowersOf."""
|
|
8
|
+
assert PowersOf(2, 4) == [0, 1, 2, 4, 8]
|
|
9
|
+
assert PowersOf(3, 5, lower=1) == [0, 3, 9, 27, 81, 243]
|
|
10
|
+
assert PowersOf(2, 4, include_zero=False) == [1, 2, 4, 8]
|
|
11
|
+
assert PowersOf(2, 6, lower=2, include_zero=False) == [4, 8, 16, 32, 64, 128]
|
{django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/tests/test_testutils.py
RENAMED
|
@@ -17,7 +17,7 @@ from django_prometheus.testutils import (
|
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
class TestPrometheusTestCaseMixin:
|
|
20
|
-
@pytest.fixture
|
|
20
|
+
@pytest.fixture
|
|
21
21
|
def registry(self):
|
|
22
22
|
return prometheus_client.CollectorRegistry()
|
|
23
23
|
|
|
@@ -43,20 +43,23 @@ class TestPrometheusTestCaseMixin:
|
|
|
43
43
|
|
|
44
44
|
def test_get_metric(self, registry):
|
|
45
45
|
"""Tests get_metric."""
|
|
46
|
-
assert
|
|
47
|
-
assert
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
assert get_metric("some_gauge", registry=registry) == 42
|
|
47
|
+
assert (
|
|
48
|
+
get_metric(
|
|
49
|
+
"some_labelled_gauge",
|
|
50
|
+
registry=registry,
|
|
51
|
+
labelred="pink",
|
|
52
|
+
labelblue="indigo",
|
|
53
|
+
)
|
|
54
|
+
== 1
|
|
52
55
|
)
|
|
53
56
|
|
|
54
57
|
def test_get_metrics_vector(self, registry):
|
|
55
58
|
"""Tests get_metrics_vector."""
|
|
56
59
|
vector = get_metrics_vector("some_nonexistent_gauge", registry=registry)
|
|
57
|
-
assert
|
|
60
|
+
assert vector == []
|
|
58
61
|
vector = get_metrics_vector("some_gauge", registry=registry)
|
|
59
|
-
assert [({}, 42)]
|
|
62
|
+
assert vector == [({}, 42)]
|
|
60
63
|
vector = get_metrics_vector("some_labelled_gauge", registry=registry)
|
|
61
64
|
assert sorted(
|
|
62
65
|
[
|
|
@@ -99,18 +102,30 @@ class TestPrometheusTestCaseMixin:
|
|
|
99
102
|
"""Tests save_registry and frozen registries operations."""
|
|
100
103
|
frozen_registry = save_registry(registry=registry)
|
|
101
104
|
# Test that we can manipulate a frozen scalar metric.
|
|
102
|
-
assert
|
|
105
|
+
assert get_metric_from_frozen_registry("some_gauge", frozen_registry) == 42
|
|
103
106
|
some_gauge.set(99)
|
|
104
|
-
assert
|
|
107
|
+
assert get_metric_from_frozen_registry("some_gauge", frozen_registry) == 42
|
|
105
108
|
assert_metric_diff(frozen_registry, 99 - 42, "some_gauge", registry=registry)
|
|
106
109
|
assert_metric_no_diff(frozen_registry, 1, "some_gauge", registry=registry)
|
|
107
110
|
# Now test the same thing with a labelled metric.
|
|
108
|
-
assert
|
|
109
|
-
|
|
111
|
+
assert (
|
|
112
|
+
get_metric_from_frozen_registry(
|
|
113
|
+
"some_labelled_gauge",
|
|
114
|
+
frozen_registry,
|
|
115
|
+
labelred="pink",
|
|
116
|
+
labelblue="indigo",
|
|
117
|
+
)
|
|
118
|
+
== 1
|
|
110
119
|
)
|
|
111
120
|
some_labelled_gauge.labels("pink", "indigo").set(5)
|
|
112
|
-
assert
|
|
113
|
-
|
|
121
|
+
assert (
|
|
122
|
+
get_metric_from_frozen_registry(
|
|
123
|
+
"some_labelled_gauge",
|
|
124
|
+
frozen_registry,
|
|
125
|
+
labelred="pink",
|
|
126
|
+
labelblue="indigo",
|
|
127
|
+
)
|
|
128
|
+
== 1
|
|
114
129
|
)
|
|
115
130
|
assert_metric_diff(
|
|
116
131
|
frozen_registry,
|
|
@@ -17,6 +17,7 @@ def TimeSince(t):
|
|
|
17
17
|
|
|
18
18
|
Returns:
|
|
19
19
|
the time since t, in fractional seconds.
|
|
20
|
+
|
|
20
21
|
"""
|
|
21
22
|
return default_timer() - t
|
|
22
23
|
|
|
@@ -25,5 +26,4 @@ def PowersOf(logbase, count, lower=0, include_zero=True):
|
|
|
25
26
|
"""Returns a list of count powers of logbase (from logbase**lower)."""
|
|
26
27
|
if not include_zero:
|
|
27
28
|
return [logbase**i for i in range(lower, count + lower)]
|
|
28
|
-
|
|
29
|
-
return [0] + [logbase**i for i in range(lower, count + lower)]
|
|
29
|
+
return [0] + [logbase**i for i in range(lower, count + lower)]
|
{django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0/django_prometheus.egg-info}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django-prometheus
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.5.0
|
|
4
4
|
Summary: Django middlewares to monitor your application with Prometheus.io.
|
|
5
5
|
Home-page: http://github.com/korfuri/django-prometheus
|
|
6
6
|
Author: Uriel Corfa
|
|
@@ -21,14 +21,17 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
21
21
|
Classifier: Programming Language :: Python :: 3.11
|
|
22
22
|
Classifier: Programming Language :: Python :: 3.12
|
|
23
23
|
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
25
|
+
Classifier: Programming Language :: Python :: Free Threading
|
|
24
26
|
Classifier: Framework :: Django :: 4.2
|
|
25
|
-
Classifier: Framework :: Django :: 5.0
|
|
26
27
|
Classifier: Framework :: Django :: 5.1
|
|
27
28
|
Classifier: Framework :: Django :: 5.2
|
|
29
|
+
Classifier: Framework :: Django :: 6.0
|
|
28
30
|
Classifier: Topic :: System :: Monitoring
|
|
29
31
|
Classifier: License :: OSI Approved :: Apache Software License
|
|
30
32
|
Description-Content-Type: text/markdown
|
|
31
33
|
License-File: LICENSE
|
|
34
|
+
Requires-Dist: Django!=5.0.*,<6.1,>=4.2
|
|
32
35
|
Requires-Dist: prometheus-client>=0.7
|
|
33
36
|
|
|
34
37
|
# django-prometheus
|
{django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus.egg-info/SOURCES.txt
RENAMED
|
@@ -42,6 +42,7 @@ django_prometheus/db/backends/spatialite/base.py
|
|
|
42
42
|
django_prometheus/db/backends/sqlite3/__init__.py
|
|
43
43
|
django_prometheus/db/backends/sqlite3/base.py
|
|
44
44
|
django_prometheus/tests/__init__.py
|
|
45
|
+
django_prometheus/tests/test_db_wrapper.py
|
|
45
46
|
django_prometheus/tests/test_django_prometheus.py
|
|
46
47
|
django_prometheus/tests/test_exports.py
|
|
47
48
|
django_prometheus/tests/test_testutils.py
|
|
@@ -36,7 +36,7 @@ setup(
|
|
|
36
36
|
packages=find_packages(
|
|
37
37
|
exclude=[
|
|
38
38
|
"tests",
|
|
39
|
-
]
|
|
39
|
+
],
|
|
40
40
|
),
|
|
41
41
|
test_suite="django_prometheus.tests",
|
|
42
42
|
long_description=LONG_DESCRIPTION,
|
|
@@ -45,6 +45,7 @@ setup(
|
|
|
45
45
|
setup_requires=["pytest-runner"],
|
|
46
46
|
options={"bdist_wheel": {"universal": "1"}},
|
|
47
47
|
install_requires=[
|
|
48
|
+
"Django>=4.2,<6.1,!=5.0.*",
|
|
48
49
|
"prometheus-client>=0.7",
|
|
49
50
|
],
|
|
50
51
|
classifiers=[
|
|
@@ -58,10 +59,12 @@ setup(
|
|
|
58
59
|
"Programming Language :: Python :: 3.11",
|
|
59
60
|
"Programming Language :: Python :: 3.12",
|
|
60
61
|
"Programming Language :: Python :: 3.13",
|
|
62
|
+
"Programming Language :: Python :: 3.14",
|
|
63
|
+
"Programming Language :: Python :: Free Threading",
|
|
61
64
|
"Framework :: Django :: 4.2",
|
|
62
|
-
"Framework :: Django :: 5.0",
|
|
63
65
|
"Framework :: Django :: 5.1",
|
|
64
66
|
"Framework :: Django :: 5.2",
|
|
67
|
+
"Framework :: Django :: 6.0",
|
|
65
68
|
"Topic :: System :: Monitoring",
|
|
66
69
|
"License :: OSI Approved :: Apache Software License",
|
|
67
70
|
],
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
from django.contrib.gis.db.backends.postgis import base
|
|
2
|
-
from django.db.backends.postgresql.base import Cursor
|
|
3
|
-
|
|
4
|
-
from django_prometheus.db.common import DatabaseWrapperMixin, ExportingCursorWrapper
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper):
|
|
8
|
-
def get_new_connection(self, *args, **kwargs):
|
|
9
|
-
conn = super().get_new_connection(*args, **kwargs)
|
|
10
|
-
conn.cursor_factory = ExportingCursorWrapper(
|
|
11
|
-
conn.cursor_factory or Cursor(),
|
|
12
|
-
"postgis",
|
|
13
|
-
self.vendor,
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
return conn
|
|
17
|
-
|
|
18
|
-
def create_cursor(self, name=None):
|
|
19
|
-
# cursor_factory is a kwarg to connect() so restore create_cursor()'s
|
|
20
|
-
# default behavior
|
|
21
|
-
return base.DatabaseWrapper.create_cursor(self, name=name)
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
from django.db.backends.postgresql import base
|
|
2
|
-
from django.db.backends.postgresql.base import Cursor
|
|
3
|
-
|
|
4
|
-
from django_prometheus.db.common import DatabaseWrapperMixin, ExportingCursorWrapper
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper):
|
|
8
|
-
def get_new_connection(self, *args, **kwargs):
|
|
9
|
-
conn = super().get_new_connection(*args, **kwargs)
|
|
10
|
-
conn.cursor_factory = ExportingCursorWrapper(
|
|
11
|
-
conn.cursor_factory or Cursor(),
|
|
12
|
-
self.alias,
|
|
13
|
-
self.vendor,
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
return conn
|
|
17
|
-
|
|
18
|
-
def create_cursor(self, name=None):
|
|
19
|
-
# cursor_factory is a kwarg to connect() so restore create_cursor()'s
|
|
20
|
-
# default behavior
|
|
21
|
-
return base.DatabaseWrapper.create_cursor(self, name=name)
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
from django_prometheus.utils import PowersOf
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class TestDjangoPrometheus:
|
|
6
|
-
def testPowersOf(self):
|
|
7
|
-
"""Tests utils.PowersOf."""
|
|
8
|
-
assert [0, 1, 2, 4, 8] == PowersOf(2, 4)
|
|
9
|
-
assert [0, 3, 9, 27, 81, 243] == PowersOf(3, 5, lower=1)
|
|
10
|
-
assert [1, 2, 4, 8] == PowersOf(2, 4, include_zero=False)
|
|
11
|
-
assert [4, 8, 16, 32, 64, 128] == PowersOf(2, 6, lower=2, include_zero=False)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
prometheus-client>=0.7
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/cache/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/cache/backends/locmem.py
RENAMED
|
File without changes
|
{django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/cache/metrics.py
RENAMED
|
File without changes
|
{django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/conf/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/db/backends/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/tests/__init__.py
RENAMED
|
File without changes
|
{django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus/tests/test_exports.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_prometheus-2.4.0.dev28 → django_prometheus-2.5.0}/django_prometheus.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|