diff options
author | Alexandre Flament <alex@al-f.net> | 2021-12-26 22:44:46 +0100 |
---|---|---|
committer | Alexandre Flament <alex@al-f.net> | 2022-01-05 19:03:04 +0100 |
commit | 2134703b4bb9847d0efeac3b28ceefb7d1f26271 (patch) | |
tree | 054d617ef416a552749cf1cf7761d45f12f348b3 /searx/metrics | |
parent | a7199bc08552fbd3a8cf8b257aeded5b26591afb (diff) | |
download | searxng-2134703b4bb9847d0efeac3b28ceefb7d1f26271.tar.gz searxng-2134703b4bb9847d0efeac3b28ceefb7d1f26271.zip |
[enh] settings.yml: implement general.enable_metrics
* allow not to record metrics (response time, etc...)
* this commit doesn't change the UI. If the metrics are disabled
/stats and /stats/errors will return empty response.
in /preferences, the columns response time and reliability will be empty.
Diffstat (limited to 'searx/metrics')
-rw-r--r-- | searx/metrics/__init__.py | 12 | ||||
-rw-r--r-- | searx/metrics/error_recorder.py | 6 | ||||
-rw-r--r-- | searx/metrics/models.py | 17 |
3 files changed, 27 insertions, 8 deletions
diff --git a/searx/metrics/__init__.py b/searx/metrics/__init__.py index 37f0ba121..bc755b96b 100644 --- a/searx/metrics/__init__.py +++ b/searx/metrics/__init__.py @@ -9,7 +9,7 @@ from timeit import default_timer from operator import itemgetter from searx.engines import engines -from .models import HistogramStorage, CounterStorage +from .models import HistogramStorage, CounterStorage, VoidHistogram, VoidCounterStorage from .error_recorder import count_error, count_exception, errors_per_engines __all__ = [ @@ -69,14 +69,18 @@ def counter(*args): return counter_storage.get(*args) -def initialize(engine_names=None): +def initialize(engine_names=None, enabled=True): """ Initialize metrics """ global counter_storage, histogram_storage # pylint: disable=global-statement - counter_storage = CounterStorage() - histogram_storage = HistogramStorage() + if enabled: + counter_storage = CounterStorage() + histogram_storage = HistogramStorage() + else: + counter_storage = VoidCounterStorage() + histogram_storage = HistogramStorage(histogram_class=VoidHistogram) # max_timeout = max of all the engine.timeout max_timeout = 2 diff --git a/searx/metrics/error_recorder.py b/searx/metrics/error_recorder.py index 76d27f64f..1d0d6e7a3 100644 --- a/searx/metrics/error_recorder.py +++ b/searx/metrics/error_recorder.py @@ -9,7 +9,7 @@ from searx.exceptions import ( SearxEngineAPIException, SearxEngineAccessDeniedException, ) -from searx import searx_parent_dir +from searx import searx_parent_dir, settings from searx.engines import engines @@ -165,6 +165,8 @@ def get_error_context(framerecords, exception_classname, log_message, log_parame def count_exception(engine_name: str, exc: Exception, secondary: bool = False) -> None: + if not settings['general']['enable_metrics']: + return framerecords = inspect.trace() try: exception_classname = get_exception_classname(exc) @@ -178,6 +180,8 @@ def count_exception(engine_name: str, exc: Exception, secondary: bool = False) - def count_error( engine_name: str, log_message: str, log_parameters: typing.Optional[typing.Tuple] = None, secondary: bool = False ) -> None: + if not settings['general']['enable_metrics']: + return framerecords = list(reversed(inspect.stack()[1:])) try: error_context = get_error_context(framerecords, None, log_message, log_parameters or (), secondary) diff --git a/searx/metrics/models.py b/searx/metrics/models.py index d42569b7f..900a7fa93 100644 --- a/searx/metrics/models.py +++ b/searx/metrics/models.py @@ -102,16 +102,17 @@ class Histogram: class HistogramStorage: - __slots__ = 'measures' + __slots__ = 'measures', 'histogram_class' - def __init__(self): + def __init__(self, histogram_class=Histogram): self.clear() + self.histogram_class = histogram_class def clear(self): self.measures = {} def configure(self, width, size, *args): - measure = Histogram(width, size) + measure = self.histogram_class(width, size) self.measures[args] = measure return measure @@ -154,3 +155,13 @@ class CounterStorage: logger.debug("Counters:") for k in ks: logger.debug("- %-60s %s", '|'.join(k), self.counters[k]) + + +class VoidHistogram(Histogram): + def observe(self, value): + pass + + +class VoidCounterStorage(CounterStorage): + def add(self, value, *args): + pass |