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 1 commit
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
18 changes: 17 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,19 @@ 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_by_view = self.register_metric(
Counter,
"django_http_requests_total_by_view",
"Total count of requests by view.",
["view"],
namespace=NAMESPACE,
)
self.requests_total = self.register_metric(
Counter,
"django_http_requests_before_middlewares_total",
Expand Down Expand Up @@ -217,6 +230,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 +275,8 @@ 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.requests_total_by_view, request, view=name).inc()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this additional metric. As it can be easily constructed from the responses_by_status_view_method metric by sum(...) by (view). Is there a reason this doesn't work for your use case?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think you are right, this should suffice for this usecase. Thanks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this dupe from my latest commit, kept the inflights one.

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