diff --git a/src/api/miscellaneous/diagnostics.py b/src/api/miscellaneous/diagnostics.py index 166f3ac7bea9633463acd00e557032e046ffd2f1..ea57251fbc0e238468528ea366cda7fc73dcec9a 100644 --- a/src/api/miscellaneous/diagnostics.py +++ b/src/api/miscellaneous/diagnostics.py @@ -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: diff --git a/src/api/routes/route.py b/src/api/routes/route.py index d65ca04614c98505135de6b3ca9610f8b80fb988..d5fe3bd7a91a18c23967ddd4f1e20b3499583b8f 100644 --- a/src/api/routes/route.py +++ b/src/api/routes/route.py @@ -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