Skip to content

Add new default metric: inflight gauge #481

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion django_prometheus/middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.utils.deprecation import MiddlewareMixin
from prometheus_client import Counter, Histogram
from prometheus_client import Counter, Histogram, Gauge

from django_prometheus.conf import NAMESPACE, PROMETHEUS_LATENCY_BUCKETS
from django_prometheus.utils import PowersOf, Time, TimeSince
Expand All @@ -21,6 +21,12 @@ def __init__(self, *args, **kwargs):
self.register()

def register(self):
self.inflight_requests = self.register_metric(
Gauge,
"django_http_inflight_requests",
"Current number of inflight requests.",
namespace=NAMESPACE,
)
self.requests_total = self.register_metric(
Counter,
"django_http_requests_before_middlewares_total",
Expand Down Expand Up @@ -217,6 +223,7 @@ def process_request(self, request):
method = self._method(request)
self.label_metric(self.metrics.requests_by_method, request, method=method).inc()
self.label_metric(self.metrics.requests_by_transport, request, transport=transport).inc()
self.label_metric(self.metrics.inflight_requests, request).inc()

# Mimic the behaviour of the deprecated "Request.is_ajax()" method.
if request.headers.get("x-requested-with") == "XMLHttpRequest":
Expand Down Expand Up @@ -261,6 +268,7 @@ def process_response(self, request, response):
method = self._method(request)
name = self._get_view_name(request)
status = str(response.status_code)
self.label_metric(self.metrics.inflight_requests, request).dec()
self.label_metric(self.metrics.responses_by_status, request, response, status=status).inc()
self.label_metric(
self.metrics.responses_by_status_view_method,
Expand Down