Skip to content
Snippets Groups Projects
Verified Commit 49a6c008 authored by Dorian Koch's avatar Dorian Koch
Browse files

Add timings to route diagnostics

parent e480833e
Branches
Tags v1.0.5
No related merge requests found
Pipeline #6418 passed
......@@ -12,9 +12,9 @@ class DiagnosticsCounter:
self._key = key
self._tracker = tracker
def trigger(self):
def trigger(self, value: int = 1):
# noinspection PyProtectedMember
self._tracker._trigger_counter(self._key)
self._tracker._trigger_counter(self._key, value)
class DiagnosticsDataProvider(ABC):
......@@ -52,11 +52,11 @@ class DiagnosticsTracker:
with self._lock:
self._providers.append((key_prefix, provider))
def _trigger_counter(self, key: str):
def _trigger_counter(self, key: str, value: int = 1):
with self._lock:
if key not in self._current_counters:
raise ValueError("Unknown counter") # pragma: no cover
self._current_counters[key] += 1
self._current_counters[key] += value
def end_interval(self):
with self._lock:
......
......@@ -2,6 +2,7 @@ from functools import wraps
import traceback
from flask import Response, request
from re import Pattern
import time
import api
from api.authentication import is_moderator
......@@ -147,6 +148,7 @@ def api_function(track_in_diagnostics: bool = True,
"Use @api_add_route(...)@api_add_route(..)@api_function() instead")
call_counter = None
call_time_counter = None
if track_in_diagnostics:
func_name: str = func.__name__
if not func_name.startswith("api_route_"): # pragma: no cover
......@@ -157,6 +159,7 @@ def api_function(track_in_diagnostics: bool = True,
raise RuntimeError("Api route function has no name (just 'api_route_')") # pragma: no cover
call_counter = DIAGNOSTICS_TRACKER.register_counter(f"route.{func_name}")
call_time_counter = DIAGNOSTICS_TRACKER.register_counter(f"route.{func_name}.time")
@wraps(func)
def wrapper(*args, **kwargs):
......@@ -181,7 +184,10 @@ def api_function(track_in_diagnostics: bool = True,
if random.random() * 100 < int(api.config["API_ROULETTE_MODE"]):
raise ApiClientException(random.choice(ALL_ERRORS_RANDOM))
start_time = time.time()
result = func(*args, **kwargs)
if call_time_counter is not None:
call_time_counter.trigger(int((time.time() - start_time) * 1000))
if isinstance(result, Response):
return result
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment