Skip to content

Commit 263d22b

Browse files
authored
Merge pull request #218 from alisoam/add-db-query-duration
Add db query duration metric
2 parents 39b5458 + e8ded99 commit 263d22b

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

django_prometheus/db/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
errors_total,
77
execute_many_total,
88
execute_total,
9+
query_duration_seconds,
910
)
1011

1112
__all__ = [
@@ -15,4 +16,5 @@
1516
"errors_total",
1617
"execute_many_total",
1718
"execute_total",
19+
"query_duration_seconds",
1820
]

django_prometheus/db/common.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
errors_total,
55
execute_many_total,
66
execute_total,
7+
query_duration_seconds,
78
)
89

910

@@ -57,21 +58,23 @@ def ExportingCursorWrapper(cursor_class, alias, vendor):
5758
vendor name.
5859
"""
5960

61+
labels = {"alias": alias, "vendor": vendor}
62+
6063
class CursorWrapper(cursor_class):
6164
"""Extends the base CursorWrapper to count events."""
6265

6366
def execute(self, *args, **kwargs):
6467
execute_total.labels(alias, vendor).inc()
65-
with ExceptionCounterByType(
66-
errors_total, extra_labels={"alias": alias, "vendor": vendor}
68+
with query_duration_seconds.labels(**labels).time(), (
69+
ExceptionCounterByType(errors_total, extra_labels=labels)
6770
):
6871
return super(CursorWrapper, self).execute(*args, **kwargs)
6972

7073
def executemany(self, query, param_list, *args, **kwargs):
7174
execute_total.labels(alias, vendor).inc(len(param_list))
7275
execute_many_total.labels(alias, vendor).inc(len(param_list))
73-
with ExceptionCounterByType(
74-
errors_total, extra_labels={"alias": alias, "vendor": vendor}
76+
with query_duration_seconds.labels(**labels).time(), (
77+
ExceptionCounterByType(errors_total, extra_labels=labels)
7578
):
7679
return super(CursorWrapper, self).executemany(
7780
query, param_list, *args, **kwargs

django_prometheus/db/metrics.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from prometheus_client import Counter
1+
from prometheus_client import Counter, Histogram
22

33
from django_prometheus.conf import NAMESPACE
44

@@ -41,3 +41,10 @@
4141
["alias", "vendor", "type"],
4242
namespace=NAMESPACE,
4343
)
44+
45+
query_duration_seconds = Histogram(
46+
"django_db_query_duration_seconds",
47+
("Histogram of query duration by database and vendor."),
48+
["alias", "vendor"],
49+
namespace=NAMESPACE,
50+
)

django_prometheus/tests/end2end/testapp/test_db.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@ def testCounters(self):
6262
>= 200
6363
)
6464

65+
def testHistograms(self):
66+
cursor_db1 = connections["test_db_1"].cursor()
67+
cursor_db2 = connections["test_db_2"].cursor()
68+
cursor_db1.execute("SELECT 1")
69+
for _ in range(200):
70+
cursor_db2.execute("SELECT 2")
71+
assert (
72+
self.getMetric(
73+
"django_db_query_duration_seconds_count",
74+
alias="test_db_1",
75+
vendor="sqlite",
76+
)
77+
> 0
78+
)
79+
assert (
80+
self.getMetric(
81+
"django_db_query_duration_seconds_count",
82+
alias="test_db_2",
83+
vendor="sqlite",
84+
)
85+
>= 200
86+
)
87+
6588
def testExecuteMany(self):
6689
registry = self.saveRegistry()
6790
cursor_db1 = connections["test_db_1"].cursor()

0 commit comments

Comments
 (0)