Skip to content

Commit

Permalink
Measure request durations
Browse files Browse the repository at this point in the history
  • Loading branch information
anchal00 committed Sep 6, 2024
1 parent 32ab5aa commit 5b2588f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 14 additions & 5 deletions optimus/prometheus.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
from prometheus_client import Counter, start_http_server
from prometheus_client import Counter, Histogram, start_http_server

from optimus.logging.logger import log

inbound_rqc = Counter("inbound_requests", "Total Requests Received")
served_rqc = Counter("served_requests", "Total Requests Processed")
erred_rqc = Counter("erred_requests", "Total Requests Failed")
inbound_rqc = Counter("inbound_dns_requests", "Total Requests Received")
served_rqc = Counter("served_dns_requests", "Total Requests Processed")
erred_rqc = Counter("erred_dns_requests", "Total Requests Failed")

req_duration_hist = Histogram("duration_dns_request", "Total time taken to process the request")

PORT = 8000


def record_metrics(func):
"""
Requires a running Prometheus Metrics server
TODO: Check whether Prometheus server is up and running on `PORT`
"""

@req_duration_hist.time()
def wrapper(*args, **kwargs):
inbound_rqc.inc(1)
was_success: bool = func(*args, **kwargs)

Check failure on line 23 in optimus/prometheus.py

View workflow job for this annotation

GitHub Actions / Build

By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]
Expand All @@ -19,7 +28,7 @@ def wrapper(*args, **kwargs):
return wrapper


def with_prometheus_monitoring(func):
def with_prometheus_metrics_server(func):
def wrapper(*args, **kwargs):
start_http_server(PORT)
log(f"Started Prometheus Server on Port {PORT}")
Expand Down
4 changes: 2 additions & 2 deletions optimus/server/udp_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

from optimus.logging.logger import log
from optimus.networking.cache import socket_cache
from optimus.prometheus import with_prometheus_monitoring
from optimus.prometheus import with_prometheus_metrics_server
from optimus.server.context import warmup_cache
from optimus.server.router import handle_request


@with_prometheus_monitoring
@with_prometheus_metrics_server
@warmup_cache(socket_cache)
def run_forever(port: int, worker_threads: int):
master_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Expand Down

0 comments on commit 5b2588f

Please sign in to comment.