__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring
  3. import typing
  4. import math
  5. import contextlib
  6. from timeit import default_timer
  7. from operator import itemgetter
  8. from searx.engines import engines
  9. from searx.openmetrics import OpenMetricsFamily
  10. from .models import HistogramStorage, CounterStorage, VoidHistogram, VoidCounterStorage
  11. from .error_recorder import count_error, count_exception, errors_per_engines
  12. __all__ = [
  13. "initialize",
  14. "get_engines_stats",
  15. "get_engine_errors",
  16. "histogram",
  17. "histogram_observe",
  18. "histogram_observe_time",
  19. "counter",
  20. "counter_inc",
  21. "counter_add",
  22. "count_error",
  23. "count_exception",
  24. ]
  25. ENDPOINTS = {'search'}
  26. histogram_storage: typing.Optional[HistogramStorage] = None
  27. counter_storage: typing.Optional[CounterStorage] = None
  28. @contextlib.contextmanager
  29. def histogram_observe_time(*args):
  30. h = histogram_storage.get(*args)
  31. before = default_timer()
  32. yield before
  33. duration = default_timer() - before
  34. if h:
  35. h.observe(duration)
  36. else:
  37. raise ValueError("histogram " + repr((*args,)) + " doesn't not exist")
  38. def histogram_observe(duration, *args):
  39. histogram_storage.get(*args).observe(duration)
  40. def histogram(*args, raise_on_not_found=True):
  41. h = histogram_storage.get(*args)
  42. if raise_on_not_found and h is None:
  43. raise ValueError("histogram " + repr((*args,)) + " doesn't not exist")
  44. return h
  45. def counter_inc(*args):
  46. counter_storage.add(1, *args)
  47. def counter_add(value, *args):
  48. counter_storage.add(value, *args)
  49. def counter(*args):
  50. return counter_storage.get(*args)
  51. def initialize(engine_names=None, enabled=True):
  52. """
  53. Initialize metrics
  54. """
  55. global counter_storage, histogram_storage # pylint: disable=global-statement
  56. if enabled:
  57. counter_storage = CounterStorage()
  58. histogram_storage = HistogramStorage()
  59. else:
  60. counter_storage = VoidCounterStorage()
  61. histogram_storage = HistogramStorage(histogram_class=VoidHistogram)
  62. # max_timeout = max of all the engine.timeout
  63. max_timeout = 2
  64. for engine_name in engine_names or engines:
  65. if engine_name in engines:
  66. max_timeout = max(max_timeout, engines[engine_name].timeout)
  67. # histogram configuration
  68. histogram_width = 0.1
  69. histogram_size = int(1.5 * max_timeout / histogram_width)
  70. # engines
  71. for engine_name in engine_names or engines:
  72. # search count
  73. counter_storage.configure('engine', engine_name, 'search', 'count', 'sent')
  74. counter_storage.configure('engine', engine_name, 'search', 'count', 'successful')
  75. # global counter of errors
  76. counter_storage.configure('engine', engine_name, 'search', 'count', 'error')
  77. # score of the engine
  78. counter_storage.configure('engine', engine_name, 'score')
  79. # result count per requests
  80. histogram_storage.configure(1, 100, 'engine', engine_name, 'result', 'count')
  81. # time doing HTTP requests
  82. histogram_storage.configure(histogram_width, histogram_size, 'engine', engine_name, 'time', 'http')
  83. # total time
  84. # .time.request and ...response times may overlap .time.http time.
  85. histogram_storage.configure(histogram_width, histogram_size, 'engine', engine_name, 'time', 'total')
  86. def get_engine_errors(engline_name_list):
  87. result = {}
  88. engine_names = list(errors_per_engines.keys())
  89. engine_names.sort()
  90. for engine_name in engine_names:
  91. if engine_name not in engline_name_list:
  92. continue
  93. error_stats = errors_per_engines[engine_name]
  94. sent_search_count = max(counter('engine', engine_name, 'search', 'count', 'sent'), 1)
  95. sorted_context_count_list = sorted(error_stats.items(), key=lambda context_count: context_count[1])
  96. r = []
  97. for context, count in sorted_context_count_list:
  98. percentage = round(20 * count / sent_search_count) * 5
  99. r.append(
  100. {
  101. 'filename': context.filename,
  102. 'function': context.function,
  103. 'line_no': context.line_no,
  104. 'code': context.code,
  105. 'exception_classname': context.exception_classname,
  106. 'log_message': context.log_message,
  107. 'log_parameters': context.log_parameters,
  108. 'secondary': context.secondary,
  109. 'percentage': percentage,
  110. }
  111. )
  112. result[engine_name] = sorted(r, reverse=True, key=lambda d: d['percentage'])
  113. return result
  114. def get_reliabilities(engline_name_list, checker_results):
  115. reliabilities = {}
  116. engine_errors = get_engine_errors(engline_name_list)
  117. for engine_name in engline_name_list:
  118. checker_result = checker_results.get(engine_name, {})
  119. checker_success = checker_result.get('success', True)
  120. errors = engine_errors.get(engine_name) or []
  121. sent_count = counter('engine', engine_name, 'search', 'count', 'sent')
  122. if sent_count == 0:
  123. # no request
  124. reliability = None
  125. elif checker_success and not errors:
  126. reliability = 100
  127. elif 'simple' in checker_result.get('errors', {}):
  128. # the basic (simple) test doesn't work: the engine is broken according to the checker
  129. # even if there is no exception
  130. reliability = 0
  131. else:
  132. # pylint: disable=consider-using-generator
  133. reliability = 100 - sum([error['percentage'] for error in errors if not error.get('secondary')])
  134. reliabilities[engine_name] = {
  135. 'reliability': reliability,
  136. 'sent_count': sent_count,
  137. 'errors': errors,
  138. 'checker': checker_result.get('errors', {}),
  139. }
  140. return reliabilities
  141. def get_engines_stats(engine_name_list):
  142. assert counter_storage is not None
  143. assert histogram_storage is not None
  144. list_time = []
  145. max_time_total = max_result_count = None
  146. for engine_name in engine_name_list:
  147. sent_count = counter('engine', engine_name, 'search', 'count', 'sent')
  148. if sent_count == 0:
  149. continue
  150. result_count = histogram('engine', engine_name, 'result', 'count').percentage(50)
  151. result_count_sum = histogram('engine', engine_name, 'result', 'count').sum
  152. successful_count = counter('engine', engine_name, 'search', 'count', 'successful')
  153. time_total = histogram('engine', engine_name, 'time', 'total').percentage(50)
  154. max_time_total = max(time_total or 0, max_time_total or 0)
  155. max_result_count = max(result_count or 0, max_result_count or 0)
  156. stats = {
  157. 'name': engine_name,
  158. 'total': None,
  159. 'total_p80': None,
  160. 'total_p95': None,
  161. 'http': None,
  162. 'http_p80': None,
  163. 'http_p95': None,
  164. 'processing': None,
  165. 'processing_p80': None,
  166. 'processing_p95': None,
  167. 'score': 0,
  168. 'score_per_result': 0,
  169. 'result_count': result_count,
  170. }
  171. if successful_count and result_count_sum:
  172. score = counter('engine', engine_name, 'score')
  173. stats['score'] = score
  174. stats['score_per_result'] = score / float(result_count_sum)
  175. time_http = histogram('engine', engine_name, 'time', 'http').percentage(50)
  176. time_http_p80 = time_http_p95 = 0
  177. if time_http is not None:
  178. time_http_p80 = histogram('engine', engine_name, 'time', 'http').percentage(80)
  179. time_http_p95 = histogram('engine', engine_name, 'time', 'http').percentage(95)
  180. stats['http'] = round(time_http, 1)
  181. stats['http_p80'] = round(time_http_p80, 1)
  182. stats['http_p95'] = round(time_http_p95, 1)
  183. if time_total is not None:
  184. time_total_p80 = histogram('engine', engine_name, 'time', 'total').percentage(80)
  185. time_total_p95 = histogram('engine', engine_name, 'time', 'total').percentage(95)
  186. stats['total'] = round(time_total, 1)
  187. stats['total_p80'] = round(time_total_p80, 1)
  188. stats['total_p95'] = round(time_total_p95, 1)
  189. stats['processing'] = round(time_total - (time_http or 0), 1)
  190. stats['processing_p80'] = round(time_total_p80 - time_http_p80, 1)
  191. stats['processing_p95'] = round(time_total_p95 - time_http_p95, 1)
  192. list_time.append(stats)
  193. return {
  194. 'time': list_time,
  195. 'max_time': math.ceil(max_time_total or 0),
  196. 'max_result_count': math.ceil(max_result_count or 0),
  197. }
  198. def openmetrics(engine_stats, engine_reliabilities):
  199. metrics = [
  200. OpenMetricsFamily(
  201. key="searxng_engines_response_time_total_seconds",
  202. type_hint="gauge",
  203. help_hint="The average total response time of the engine",
  204. data_info=[{'engine_name': engine['name']} for engine in engine_stats['time']],
  205. data=[engine['total'] or 0 for engine in engine_stats['time']],
  206. ),
  207. OpenMetricsFamily(
  208. key="searxng_engines_response_time_processing_seconds",
  209. type_hint="gauge",
  210. help_hint="The average processing response time of the engine",
  211. data_info=[{'engine_name': engine['name']} for engine in engine_stats['time']],
  212. data=[engine['processing'] or 0 for engine in engine_stats['time']],
  213. ),
  214. OpenMetricsFamily(
  215. key="searxng_engines_response_time_http_seconds",
  216. type_hint="gauge",
  217. help_hint="The average HTTP response time of the engine",
  218. data_info=[{'engine_name': engine['name']} for engine in engine_stats['time']],
  219. data=[engine['http'] or 0 for engine in engine_stats['time']],
  220. ),
  221. OpenMetricsFamily(
  222. key="searxng_engines_result_count_total",
  223. type_hint="counter",
  224. help_hint="The total amount of results returned by the engine",
  225. data_info=[{'engine_name': engine['name']} for engine in engine_stats['time']],
  226. data=[engine['result_count'] or 0 for engine in engine_stats['time']],
  227. ),
  228. OpenMetricsFamily(
  229. key="searxng_engines_request_count_total",
  230. type_hint="counter",
  231. help_hint="The total amount of user requests made to this engine",
  232. data_info=[{'engine_name': engine['name']} for engine in engine_stats['time']],
  233. data=[
  234. engine_reliabilities.get(engine['name'], {}).get('sent_count', 0) or 0
  235. for engine in engine_stats['time']
  236. ],
  237. ),
  238. OpenMetricsFamily(
  239. key="searxng_engines_reliability_total",
  240. type_hint="counter",
  241. help_hint="The overall reliability of the engine",
  242. data_info=[{'engine_name': engine['name']} for engine in engine_stats['time']],
  243. data=[
  244. engine_reliabilities.get(engine['name'], {}).get('reliability', 0) or 0
  245. for engine in engine_stats['time']
  246. ],
  247. ),
  248. ]
  249. return "".join([str(metric) for metric in metrics])