__init__.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. # pylint: disable=missing-module-docstring, missing-function-docstring
  4. import typing
  5. import math
  6. import contextlib
  7. from timeit import default_timer
  8. from operator import itemgetter
  9. from searx.engines import engines
  10. from .models import HistogramStorage, CounterStorage
  11. from .error_recorder import count_error, count_exception, errors_per_engines
  12. __all__ = ["initialize",
  13. "get_engines_stats", "get_engine_errors",
  14. "histogram", "histogram_observe", "histogram_observe_time",
  15. "counter", "counter_inc", "counter_add",
  16. "count_error", "count_exception"]
  17. ENDPOINTS = {'search'}
  18. histogram_storage: typing.Optional[HistogramStorage] = None
  19. counter_storage: typing.Optional[CounterStorage] = None
  20. @contextlib.contextmanager
  21. def histogram_observe_time(*args):
  22. h = histogram_storage.get(*args)
  23. before = default_timer()
  24. yield before
  25. duration = default_timer() - before
  26. if h:
  27. h.observe(duration)
  28. else:
  29. raise ValueError("histogram " + repr((*args,)) + " doesn't not exist")
  30. def histogram_observe(duration, *args):
  31. histogram_storage.get(*args).observe(duration)
  32. def histogram(*args, raise_on_not_found=True):
  33. h = histogram_storage.get(*args)
  34. if raise_on_not_found and h is None:
  35. raise ValueError("histogram " + repr((*args,)) + " doesn't not exist")
  36. return h
  37. def counter_inc(*args):
  38. counter_storage.add(1, *args)
  39. def counter_add(value, *args):
  40. counter_storage.add(value, *args)
  41. def counter(*args):
  42. return counter_storage.get(*args)
  43. def initialize(engine_names=None):
  44. """
  45. Initialize metrics
  46. """
  47. global counter_storage, histogram_storage # pylint: disable=global-statement
  48. counter_storage = CounterStorage()
  49. histogram_storage = HistogramStorage()
  50. # max_timeout = max of all the engine.timeout
  51. max_timeout = 2
  52. for engine_name in (engine_names or engines):
  53. if engine_name in engines:
  54. max_timeout = max(max_timeout, engines[engine_name].timeout)
  55. # histogram configuration
  56. histogram_width = 0.1
  57. histogram_size = int(1.5 * max_timeout / histogram_width)
  58. # engines
  59. for engine_name in (engine_names or engines):
  60. # search count
  61. counter_storage.configure('engine', engine_name, 'search', 'count', 'sent')
  62. counter_storage.configure('engine', engine_name, 'search', 'count', 'successful')
  63. # global counter of errors
  64. counter_storage.configure('engine', engine_name, 'search', 'count', 'error')
  65. # score of the engine
  66. counter_storage.configure('engine', engine_name, 'score')
  67. # result count per requests
  68. histogram_storage.configure(1, 100, 'engine', engine_name, 'result', 'count')
  69. # time doing HTTP requests
  70. histogram_storage.configure(histogram_width, histogram_size, 'engine', engine_name, 'time', 'http')
  71. # total time
  72. # .time.request and ...response times may overlap .time.http time.
  73. histogram_storage.configure(histogram_width, histogram_size, 'engine', engine_name, 'time', 'total')
  74. def get_engine_errors(engline_name_list):
  75. result = {}
  76. engine_names = list(errors_per_engines.keys())
  77. engine_names.sort()
  78. for engine_name in engine_names:
  79. if engine_name not in engline_name_list:
  80. continue
  81. error_stats = errors_per_engines[engine_name]
  82. sent_search_count = max(counter('engine', engine_name, 'search', 'count', 'sent'), 1)
  83. sorted_context_count_list = sorted(error_stats.items(), key=lambda context_count: context_count[1])
  84. r = []
  85. for context, count in sorted_context_count_list:
  86. percentage = round(20 * count / sent_search_count) * 5
  87. r.append({
  88. 'filename': context.filename,
  89. 'function': context.function,
  90. 'line_no': context.line_no,
  91. 'code': context.code,
  92. 'exception_classname': context.exception_classname,
  93. 'log_message': context.log_message,
  94. 'log_parameters': context.log_parameters,
  95. 'secondary': context.secondary,
  96. 'percentage': percentage,
  97. })
  98. result[engine_name] = sorted(r, reverse=True, key=lambda d: d['percentage'])
  99. return result
  100. def get_reliabilities(engline_name_list, checker_results):
  101. reliabilities = {}
  102. engine_errors = get_engine_errors(engline_name_list)
  103. for engine_name in engline_name_list:
  104. checker_result = checker_results.get(engine_name, {})
  105. checker_success = checker_result.get('success', True)
  106. errors = engine_errors.get(engine_name) or []
  107. if counter('engine', engine_name, 'search', 'count', 'sent') == 0:
  108. # no request
  109. reliablity = None
  110. elif checker_success and not errors:
  111. reliablity = 100
  112. elif 'simple' in checker_result.get('errors', {}):
  113. # the basic (simple) test doesn't work: the engine is broken accoding to the checker
  114. # even if there is no exception
  115. reliablity = 0
  116. else:
  117. reliablity = 100 - sum([error['percentage'] for error in errors if not error.get('secondary')])
  118. reliabilities[engine_name] = {
  119. 'reliablity': reliablity,
  120. 'errors': errors,
  121. 'checker': checker_results.get(engine_name, {}).get('errors', {}),
  122. }
  123. return reliabilities
  124. def round_or_none(number, digits):
  125. '''return None if number is None
  126. return 0 if number is 0
  127. otherwise round number with "digits numbers.
  128. '''
  129. return round(number, digits) if number is not None else number
  130. def get_engines_stats(engine_name_list):
  131. assert counter_storage is not None
  132. assert histogram_storage is not None
  133. list_time = []
  134. max_time_total = max_result_count = None # noqa
  135. for engine_name in engine_name_list:
  136. sent_count = counter('engine', engine_name, 'search', 'count', 'sent')
  137. if sent_count == 0:
  138. continue
  139. successful_count = counter('engine', engine_name, 'search', 'count', 'successful')
  140. time_total = histogram('engine', engine_name, 'time', 'total').percentage(50)
  141. time_http = histogram('engine', engine_name, 'time', 'http').percentage(50)
  142. time_total_p80 = histogram('engine', engine_name, 'time', 'total').percentage(80)
  143. time_http_p80 = histogram('engine', engine_name, 'time', 'http').percentage(80)
  144. time_total_p95 = histogram('engine', engine_name, 'time', 'total').percentage(95)
  145. time_http_p95 = histogram('engine', engine_name, 'time', 'http').percentage(95)
  146. result_count = histogram('engine', engine_name, 'result', 'count').percentage(50)
  147. result_count_sum = histogram('engine', engine_name, 'result', 'count').sum
  148. if successful_count and result_count_sum:
  149. score = counter('engine', engine_name, 'score') # noqa
  150. score_per_result = score / float(result_count_sum)
  151. else:
  152. score = score_per_result = 0.0
  153. max_time_total = max(time_total or 0, max_time_total or 0)
  154. max_result_count = max(result_count or 0, max_result_count or 0)
  155. time_total_is_number = time_total is not None
  156. list_time.append({
  157. 'name': engine_name,
  158. 'total': round_or_none(time_total, 1),
  159. 'total_p80': round_or_none(time_total_p80, 1),
  160. 'total_p95': round_or_none(time_total_p95, 1),
  161. 'http': round_or_none(time_http, 1),
  162. 'http_p80': round_or_none(time_http_p80, 1),
  163. 'http_p95': round_or_none(time_http_p95, 1),
  164. 'processing': round(time_total - (time_http or 0), 1) if time_total_is_number else None,
  165. 'processing_p80': round(time_total_p80 - (time_http_p80 or 0), 1) if time_total_is_number else None,
  166. 'processing_p95': round(time_total_p95 - (time_http_p95 or 0), 1) if time_total_is_number else None,
  167. 'score': score,
  168. 'score_per_result': score_per_result,
  169. 'result_count': result_count,
  170. })
  171. return {
  172. 'time': list_time,
  173. 'max_time': math.ceil(max_time_total or 0),
  174. 'max_result_count': math.ceil(max_result_count or 0),
  175. }