Skip to content

Commit

Permalink
Add prometheus metric for the number of requests in flight. (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh authored Mar 24, 2020
1 parent 699aff8 commit 7b6e798
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog.d/87.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add prometheus metric for the number of requests in flight.
25 changes: 18 additions & 7 deletions sygnal/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@
import traceback
from uuid import uuid4

from opentracing import Format, tags, logs
from prometheus_client import Counter, Histogram
from opentracing import Format, logs, tags
from prometheus_client import Counter, Gauge, Histogram
from twisted.internet.defer import ensureDeferred
from twisted.web import server
from twisted.web.http import (
proxiedLogFormatter,
datetimeToLogString,
combinedLogFormatter,
datetimeToLogString,
proxiedLogFormatter,
)
from twisted.web.resource import Resource
from twisted.web.server import NOT_DONE_YET

from sygnal.notifications import NotificationContext
from sygnal.utils import NotificationLoggerAdapter

from .exceptions import InvalidNotificationException, NotificationDispatchException
from .notifications import Notification

Expand Down Expand Up @@ -66,6 +67,12 @@
labelnames=["code"],
)

REQUESTS_IN_FLIGHT_GUAGE = Gauge(
"sygnal_requests_in_flight",
"Number of HTTP requests in flight",
labelnames=["resource"],
)


class V1NotifyHandler(Resource):
def __init__(self, sygnal):
Expand Down Expand Up @@ -164,9 +171,13 @@ def _handle_request(self, request):

root_span_accounted_for = True

ensureDeferred(
self._handle_dispatch(root_span, request, log, notif, context)
)
async def cb():
with REQUESTS_IN_FLIGHT_GUAGE.labels(
self.__class__.__name__
).track_inprogress():
await self._handle_dispatch(root_span, request, log, notif, context)

ensureDeferred(cb())

# we have to try and send the notifications first,
# so we can find out which ones to reject
Expand Down

0 comments on commit 7b6e798

Please sign in to comment.