summaryrefslogtreecommitdiff
path: root/searx/search
diff options
context:
space:
mode:
authorAlexandre Flament <alex@al-f.net>2021-04-14 17:23:15 +0200
committerAlexandre Flament <alex@al-f.net>2021-04-21 16:24:46 +0200
commit7acd7ffc02d14d175ec2a99ba984e47d8cb65d7d (patch)
tree000b6e4b0038ed627bb114f8a2de83681bbf7ad4 /searx/search
parentaae7830d14242ac1f98232f428654c5d2c9c5eb2 (diff)
downloadsearxng-7acd7ffc02d14d175ec2a99ba984e47d8cb65d7d.tar.gz
searxng-7acd7ffc02d14d175ec2a99ba984e47d8cb65d7d.zip
[enh] rewrite and enhance metrics
Diffstat (limited to 'searx/search')
-rw-r--r--searx/search/__init__.py13
-rw-r--r--searx/search/checker/impl.py4
-rw-r--r--searx/search/processors/abstract.py29
-rw-r--r--searx/search/processors/online.py8
4 files changed, 24 insertions, 30 deletions
diff --git a/searx/search/__init__.py b/searx/search/__init__.py
index 5049d9ff7..9b26f38de 100644
--- a/searx/search/__init__.py
+++ b/searx/search/__init__.py
@@ -18,7 +18,7 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
import typing
import gc
import threading
-from time import time
+from timeit import default_timer
from uuid import uuid4
from _thread import start_new_thread
@@ -31,6 +31,7 @@ from searx.plugins import plugins
from searx.search.models import EngineRef, SearchQuery
from searx.search.processors import processors, initialize as initialize_processors
from searx.search.checker import initialize as initialize_checker
+from searx.metrics import initialize as initialize_metrics, counter_inc, histogram_observe_time
logger = logger.getChild('search')
@@ -50,6 +51,7 @@ else:
def initialize(settings_engines=None, enable_checker=False):
settings_engines = settings_engines or settings['engines']
initialize_processors(settings_engines)
+ initialize_metrics([engine['name'] for engine in settings_engines])
if enable_checker:
initialize_checker()
@@ -115,8 +117,7 @@ class Search:
if request_params is None:
continue
- with processor.lock:
- processor.engine.stats['sent_search_count'] += 1
+ counter_inc('engine', engineref.name, 'search', 'count', 'sent')
# append request to list
requests.append((engineref.name, self.search_query.query, request_params))
@@ -161,7 +162,7 @@ class Search:
for th in threading.enumerate():
if th.name == search_id:
- remaining_time = max(0.0, self.actual_timeout - (time() - self.start_time))
+ remaining_time = max(0.0, self.actual_timeout - (default_timer() - self.start_time))
th.join(remaining_time)
if th.is_alive():
th._timeout = True
@@ -184,12 +185,10 @@ class Search:
# do search-request
def search(self):
- self.start_time = time()
-
+ self.start_time = default_timer()
if not self.search_external_bang():
if not self.search_answerers():
self.search_standard()
-
return self.result_container
diff --git a/searx/search/checker/impl.py b/searx/search/checker/impl.py
index 1893a82b9..5cb289ec6 100644
--- a/searx/search/checker/impl.py
+++ b/searx/search/checker/impl.py
@@ -16,6 +16,7 @@ from searx import network, logger
from searx.results import ResultContainer
from searx.search.models import SearchQuery, EngineRef
from searx.search.processors import EngineProcessor
+from searx.metrics import counter_inc
logger = logger.getChild('searx.search.checker')
@@ -384,8 +385,7 @@ class Checker:
engineref_category = search_query.engineref_list[0].category
params = self.processor.get_params(search_query, engineref_category)
if params is not None:
- with self.processor.lock:
- self.processor.engine.stats['sent_search_count'] += 1
+ counter_inc('engine', search_query.engineref_list[0].name, 'search', 'count', 'sent')
self.processor.search(search_query.query, params, result_container, time(), 5)
return result_container
diff --git a/searx/search/processors/abstract.py b/searx/search/processors/abstract.py
index e32d8f067..854f6df6a 100644
--- a/searx/search/processors/abstract.py
+++ b/searx/search/processors/abstract.py
@@ -2,12 +2,12 @@
import threading
from abc import abstractmethod, ABC
-from time import time
+from timeit import default_timer
from searx import logger
from searx.engines import settings
from searx.network import get_time_for_thread, get_network
-from searx.metrology.error_recorder import record_exception, record_error
+from searx.metrics import histogram_observe, counter_inc, count_exception, count_error
from searx.exceptions import SearxEngineAccessDeniedException
@@ -27,7 +27,7 @@ class SuspendedStatus:
@property
def is_suspended(self):
- return self.suspend_end_time >= time()
+ return self.suspend_end_time >= default_timer()
def suspend(self, suspended_time, suspend_reason):
with self.lock:
@@ -36,7 +36,7 @@ class SuspendedStatus:
if suspended_time is None:
suspended_time = min(settings['search']['max_ban_time_on_fail'],
self.continuous_errors * settings['search']['ban_time_on_fail'])
- self.suspend_end_time = time() + suspended_time
+ self.suspend_end_time = default_timer() + suspended_time
self.suspend_reason = suspend_reason
logger.debug('Suspend engine for %i seconds', suspended_time)
@@ -55,7 +55,6 @@ class EngineProcessor(ABC):
def __init__(self, engine, engine_name):
self.engine = engine
self.engine_name = engine_name
- self.lock = threading.Lock()
key = get_network(self.engine_name)
key = id(key) if key else self.engine_name
self.suspended_status = SUSPENDED_STATUS.setdefault(key, SuspendedStatus())
@@ -65,12 +64,11 @@ class EngineProcessor(ABC):
error_message = str(exception) if display_exception and exception else None
result_container.add_unresponsive_engine(self.engine_name, reason, error_message)
# metrics
- with self.lock:
- self.engine.stats['errors'] += 1
+ counter_inc('engine', self.engine_name, 'search', 'count', 'error')
if exception:
- record_exception(self.engine_name, exception)
+ count_exception(self.engine_name, exception)
else:
- record_error(self.engine_name, reason)
+ count_error(self.engine_name, reason)
# suspend the engine ?
if suspend:
suspended_time = None
@@ -81,17 +79,14 @@ class EngineProcessor(ABC):
def _extend_container_basic(self, result_container, start_time, search_results):
# update result_container
result_container.extend(self.engine_name, search_results)
- engine_time = time() - start_time
+ engine_time = default_timer() - start_time
page_load_time = get_time_for_thread()
result_container.add_timing(self.engine_name, engine_time, page_load_time)
# metrics
- with self.lock:
- self.engine.stats['engine_time'] += engine_time
- self.engine.stats['engine_time_count'] += 1
- # update stats with the total HTTP time
- if page_load_time is not None and 'page_load_time' in self.engine.stats:
- self.engine.stats['page_load_time'] += page_load_time
- self.engine.stats['page_load_count'] += 1
+ counter_inc('engine', self.engine_name, 'search', 'count', 'successful')
+ histogram_observe(engine_time, 'engine', self.engine_name, 'time', 'total')
+ if page_load_time is not None:
+ histogram_observe(page_load_time, 'engine', self.engine_name, 'time', 'http')
def extend_container(self, result_container, start_time, search_results):
if getattr(threading.current_thread(), '_timeout', False):
diff --git a/searx/search/processors/online.py b/searx/search/processors/online.py
index 1483897d6..bca74b746 100644
--- a/searx/search/processors/online.py
+++ b/searx/search/processors/online.py
@@ -10,7 +10,7 @@ from searx import logger
from searx.utils import gen_useragent
from searx.exceptions import (SearxEngineAccessDeniedException, SearxEngineCaptchaException,
SearxEngineTooManyRequestsException,)
-from searx.metrology.error_recorder import record_error
+from searx.metrics.error_recorder import count_error
from searx.search.processors.abstract import EngineProcessor
@@ -90,9 +90,9 @@ class OnlineProcessor(EngineProcessor):
status_code = str(response.status_code or '')
reason = response.reason_phrase or ''
hostname = response.url.host
- record_error(self.engine_name,
- '{} redirects, maximum: {}'.format(len(response.history), soft_max_redirects),
- (status_code, reason, hostname))
+ count_error(self.engine_name,
+ '{} redirects, maximum: {}'.format(len(response.history), soft_max_redirects),
+ (status_code, reason, hostname))
return response