.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 (Exception exc)
ErrorContext get_error_context (framerecords, exception_classname, log_message, LogParametersType log_parameters, bool secondary)
None count_exception (str engine_name, Exception 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 98 of file error_recorder.py.

98def add_error_context(engine_name: str, error_context: ErrorContext) -> None:
99 errors_for_engine = errors_per_engines.setdefault(engine_name, {})
100 errors_for_engine[error_context] = errors_for_engine.get(error_context, 0) + 1
101 engines[engine_name].logger.warning('%s', str(error_context))
102
103

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 198 of file error_recorder.py.

203) -> None:
204 if not settings['general']['enable_metrics']:
205 return
206 framerecords = list(reversed(inspect.stack()[1:]))
207 try:
208 error_context = get_error_context(framerecords, None, log_message, log_parameters or (), secondary)
209 add_error_context(engine_name, error_context)
210 finally:
211 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,
Exception exc,
bool secondary = False )

Definition at line 185 of file error_recorder.py.

185def count_exception(engine_name: str, exc: Exception, secondary: bool = False) -> None:
186 if not settings['general']['enable_metrics']:
187 return
188 framerecords = inspect.trace()
189 try:
190 exception_classname = get_exception_classname(exc)
191 log_parameters = get_messages(exc, framerecords[-1][1])
192 error_context = get_error_context(framerecords, exception_classname, None, log_parameters, secondary)
193 add_error_context(engine_name, error_context)
194 finally:
195 del framerecords
196
197

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 171 of file error_recorder.py.

173) -> ErrorContext:
174 searx_frame = get_trace(framerecords)
175 filename = searx_frame.filename
176 if filename.startswith(searx_parent_dir):
177 filename = filename[len(searx_parent_dir) + 1 :]
178 function = searx_frame.function
179 line_no = searx_frame.lineno
180 code = searx_frame.code_context[0].strip()
181 del framerecords
182 return ErrorContext(filename, function, line_no, code, exception_classname, log_message, log_parameters, secondary)
183
184

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 ( Exception exc)

Definition at line 162 of file error_recorder.py.

162def get_exception_classname(exc: Exception) -> str:
163 exc_class = exc.__class__
164 exc_name = exc_class.__qualname__
165 exc_module = exc_class.__module__
166 if exc_module is None or exc_module == str.__class__.__module__:
167 return exc_name
168 return exc_module + '.' + exc_name
169
170

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 114 of file error_recorder.py.

114def get_hostname(exc: HTTPError) -> str | None:
115 url = exc.request.url
116 if url is None and exc.response is not None:
117 url = exc.response.url
118 return urlparse(url).netloc
119
120

◆ get_messages()

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

Definition at line 142 of file error_recorder.py.

142def get_messages(exc, filename) -> tuple[str, ...]: # pylint: disable=too-many-return-statements
143 if isinstance(exc, JSONDecodeError):
144 return (exc.msg,)
145 if isinstance(exc, TypeError):
146 return (str(exc),)
147 if isinstance(exc, ValueError) and 'lxml' in filename:
148 return (str(exc),)
149 if isinstance(exc, HTTPError):
150 return get_request_exception_messages(exc)
151 if isinstance(exc, SearxXPathSyntaxException):
152 return (exc.xpath_str, exc.message)
153 if isinstance(exc, SearxEngineXPathException):
154 return (exc.xpath_str, exc.message)
155 if isinstance(exc, SearxEngineAPIException):
156 return (str(exc.args[0]),)
157 if isinstance(exc, SearxEngineAccessDeniedException):
158 return (exc.message,)
159 return ()
160
161

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 121 of file error_recorder.py.

123) -> tuple[str | None, str | None, str | None]:
124 url = None
125 status_code = None
126 reason = None
127 hostname = None
128 if hasattr(exc, '_request') and exc._request is not None: # pylint: disable=protected-access
129 # exc.request is property that raise an RuntimeException
130 # if exc._request is not defined.
131 url = exc.request.url
132 if url is None and hasattr(exc, 'response') and exc.response is not None:
133 url = exc.response.url
134 if url is not None:
135 hostname = url.host
136 if isinstance(exc, HTTPStatusError):
137 status_code = str(exc.response.status_code)
138 reason = exc.response.reason_phrase
139 return (status_code, reason, hostname)
140
141

Referenced by get_messages().

Here is the caller graph for this function:

◆ get_trace()

searx.metrics.error_recorder.get_trace ( traces)

Definition at line 104 of file error_recorder.py.

104def get_trace(traces):
105 for trace in reversed(traces):
106 split_filename: list[str] = trace.filename.split('/')
107 if '/'.join(split_filename[-3:-1]) == 'searx/engines':
108 return trace
109 if '/'.join(split_filename[-4:-1]) == 'searx/search/processors':
110 return trace
111 return traces[-1]
112
113

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.