.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)
 
typing.Optional[None] get_hostname (HTTPError exc)
 
typing.Tuple[typing.Optional[str], typing.Optional[str], typing.Optional[str]] get_request_exception_messages (HTTPError exc)
 
typing.Tuple get_messages (exc, filename)
 
str get_exception_classname (Exception exc)
 
ErrorContext get_error_context (framerecords, exception_classname, log_message, log_parameters, secondary)
 
None count_exception (str engine_name, Exception exc, bool secondary=False)
 
None count_error (str engine_name, str log_message, typing.Optional[typing.Tuple] log_parameters=None, bool secondary=False)
 

Variables

dict errors_per_engines = {}
 

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 searx.metrics.error_recorder.count_error(), and searx.metrics.error_recorder.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,
typing.Optional[typing.Tuple] log_parameters = None,
bool secondary = False )

Definition at line 185 of file error_recorder.py.

187) -> None:
188 if not settings['general']['enable_metrics']:
189 return
190 framerecords = list(reversed(inspect.stack()[1:]))
191 try:
192 error_context = get_error_context(framerecords, None, log_message, log_parameters or (), secondary)
193 add_error_context(engine_name, error_context)
194 finally:
195 del framerecords

References searx.metrics.error_recorder.add_error_context(), and searx.metrics.error_recorder.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 172 of file error_recorder.py.

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

References searx.metrics.error_recorder.add_error_context(), searx.metrics.error_recorder.get_error_context(), searx.metrics.error_recorder.get_exception_classname(), and searx.metrics.error_recorder.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,
log_parameters,
secondary )

Definition at line 160 of file error_recorder.py.

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

References searx.metrics.error_recorder.get_trace().

Referenced by searx.metrics.error_recorder.count_error(), and searx.metrics.error_recorder.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 151 of file error_recorder.py.

151def get_exception_classname(exc: Exception) -> 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 searx.metrics.error_recorder.count_exception().

+ Here is the caller graph for this function:

◆ get_hostname()

typing.Optional[None] searx.metrics.error_recorder.get_hostname ( HTTPError exc)

Definition at line 103 of file error_recorder.py.

103def get_hostname(exc: HTTPError) -> typing.Optional[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()

typing.Tuple searx.metrics.error_recorder.get_messages ( exc,
filename )

Definition at line 131 of file error_recorder.py.

131def get_messages(exc, filename) -> typing.Tuple: # 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 searx.metrics.error_recorder.get_request_exception_messages().

Referenced by searx.metrics.error_recorder.count_exception().

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

◆ get_request_exception_messages()

typing.Tuple[typing.Optional[str], typing.Optional[str], typing.Optional[str]] searx.metrics.error_recorder.get_request_exception_messages ( HTTPError exc)

Definition at line 110 of file error_recorder.py.

112) -> typing.Tuple[typing.Optional[str], typing.Optional[str], typing.Optional[str]]:
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 searx.metrics.error_recorder.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 = 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 searx.metrics.error_recorder.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 19 of file error_recorder.py.