.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.metrics.error_recorder Namespace Reference

Classes

class  ErrorContext

Functions

None add_error_context (str engine_name, ErrorContext error_context)
 get_trace (traces)
str|None get_hostname (HTTPError exc)
tuple[str|None, str|None, str|None] get_request_exception_messages (HTTPError exc)
tuple[str,...] get_messages (exc, filename)
str get_exception_classname (BaseException exc)
ErrorContext get_error_context (framerecords, exception_classname, log_message, LogParametersType log_parameters, bool secondary)
None count_exception (str engine_name, BaseException exc, bool secondary=False)
None count_error (str engine_name, str log_message, LogParametersType|None log_parameters=None, bool secondary=False)

Variables

dict errors_per_engines = {}
 LogParametersType = tuple[str, ...]

Function Documentation

◆ add_error_context()

None searx.metrics.error_recorder.add_error_context ( str engine_name,
ErrorContext error_context )

Definition at line 87 of file error_recorder.py.

87def add_error_context(engine_name: str, error_context: ErrorContext) -> None:
88 errors_for_engine = errors_per_engines.setdefault(engine_name, {})
89 errors_for_engine[error_context] = errors_for_engine.get(error_context, 0) + 1
90 engines[engine_name].logger.warning('%s', str(error_context))
91
92

Referenced by count_error(), and count_exception().

Here is the caller graph for this function:

◆ count_error()

None searx.metrics.error_recorder.count_error ( str engine_name,
str log_message,
LogParametersType | None log_parameters = None,
bool secondary = False )

Definition at line 187 of file error_recorder.py.

192) -> None:
193 if not settings['general']['enable_metrics']:
194 return
195 framerecords = list(reversed(inspect.stack()[1:]))
196 try:
197 error_context = get_error_context(framerecords, None, log_message, log_parameters or (), secondary)
198 add_error_context(engine_name, error_context)
199 finally:
200 del framerecords

References add_error_context(), and get_error_context().

Here is the call graph for this function:

◆ count_exception()

None searx.metrics.error_recorder.count_exception ( str engine_name,
BaseException exc,
bool secondary = False )

Definition at line 174 of file error_recorder.py.

174def count_exception(engine_name: str, exc: BaseException, secondary: bool = False) -> None:
175 if not settings['general']['enable_metrics']:
176 return
177 framerecords = inspect.trace()
178 try:
179 exception_classname = get_exception_classname(exc)
180 log_parameters = get_messages(exc, framerecords[-1][1])
181 error_context = get_error_context(framerecords, exception_classname, None, log_parameters, secondary)
182 add_error_context(engine_name, error_context)
183 finally:
184 del framerecords
185
186

References add_error_context(), get_error_context(), get_exception_classname(), and get_messages().

Here is the call graph for this function:

◆ get_error_context()

ErrorContext searx.metrics.error_recorder.get_error_context ( framerecords,
exception_classname,
log_message,
LogParametersType log_parameters,
bool secondary )

Definition at line 160 of file error_recorder.py.

162) -> ErrorContext:
163 searx_frame = get_trace(framerecords)
164 filename = searx_frame.filename
165 if filename.startswith(searx_parent_dir):
166 filename = filename[len(searx_parent_dir) + 1 :]
167 function = searx_frame.function
168 line_no = searx_frame.lineno
169 code = searx_frame.code_context[0].strip()
170 del framerecords
171 return ErrorContext(filename, function, line_no, code, exception_classname, log_message, log_parameters, secondary)
172
173

References get_trace().

Referenced by count_error(), and count_exception().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_exception_classname()

str searx.metrics.error_recorder.get_exception_classname ( BaseException exc)

Definition at line 151 of file error_recorder.py.

151def get_exception_classname(exc: BaseException) -> str:
152 exc_class = exc.__class__
153 exc_name = exc_class.__qualname__
154 exc_module = exc_class.__module__
155 if exc_module is None or exc_module == str.__class__.__module__:
156 return exc_name
157 return exc_module + '.' + exc_name
158
159

Referenced by count_exception().

Here is the caller graph for this function:

◆ get_hostname()

str | None searx.metrics.error_recorder.get_hostname ( HTTPError exc)

Definition at line 103 of file error_recorder.py.

103def get_hostname(exc: HTTPError) -> str | None:
104 url = exc.request.url
105 if url is None and exc.response is not None:
106 url = exc.response.url
107 return urlparse(url).netloc
108
109

◆ get_messages()

tuple[str, ...] searx.metrics.error_recorder.get_messages ( exc,
filename )

Definition at line 131 of file error_recorder.py.

131def get_messages(exc, filename) -> tuple[str, ...]: # pylint: disable=too-many-return-statements
132 if isinstance(exc, JSONDecodeError):
133 return (exc.msg,)
134 if isinstance(exc, TypeError):
135 return (str(exc),)
136 if isinstance(exc, ValueError) and 'lxml' in filename:
137 return (str(exc),)
138 if isinstance(exc, HTTPError):
139 return get_request_exception_messages(exc)
140 if isinstance(exc, SearxXPathSyntaxException):
141 return (exc.xpath_str, exc.message)
142 if isinstance(exc, SearxEngineXPathException):
143 return (exc.xpath_str, exc.message)
144 if isinstance(exc, SearxEngineAPIException):
145 return (str(exc.args[0]),)
146 if isinstance(exc, SearxEngineAccessDeniedException):
147 return (exc.message,)
148 return ()
149
150

References get_request_exception_messages().

Referenced by count_exception().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_request_exception_messages()

tuple[str | None, str | None, str | None] searx.metrics.error_recorder.get_request_exception_messages ( HTTPError exc)

Definition at line 110 of file error_recorder.py.

112) -> tuple[str | None, str | None, str | None]:
113 url = None
114 status_code = None
115 reason = None
116 hostname = None
117 if hasattr(exc, '_request') and exc._request is not None: # pylint: disable=protected-access
118 # exc.request is property that raise an RuntimeException
119 # if exc._request is not defined.
120 url = exc.request.url
121 if url is None and hasattr(exc, 'response') and exc.response is not None:
122 url = exc.response.url
123 if url is not None:
124 hostname = url.host
125 if isinstance(exc, HTTPStatusError):
126 status_code = str(exc.response.status_code)
127 reason = exc.response.reason_phrase
128 return (status_code, reason, hostname)
129
130

Referenced by get_messages().

Here is the caller graph for this function:

◆ get_trace()

searx.metrics.error_recorder.get_trace ( traces)

Definition at line 93 of file error_recorder.py.

93def get_trace(traces):
94 for trace in reversed(traces):
95 split_filename: list[str] = trace.filename.split('/')
96 if '/'.join(split_filename[-3:-1]) == 'searx/engines':
97 return trace
98 if '/'.join(split_filename[-4:-1]) == 'searx/search/processors':
99 return trace
100 return traces[-1]
101
102

Referenced by get_error_context().

Here is the caller graph for this function:

Variable Documentation

◆ errors_per_engines

dict searx.metrics.error_recorder.errors_per_engines = {}

Definition at line 20 of file error_recorder.py.

◆ LogParametersType

searx.metrics.error_recorder.LogParametersType = tuple[str, ...]

Definition at line 22 of file error_recorder.py.