diff options
author | Bnyro <bnyro@tutanota.com> | 2024-09-17 16:43:48 +0200 |
---|---|---|
committer | Markus Heiser <markus.heiser@darmarIT.de> | 2024-11-24 14:25:49 +0100 |
commit | 8744dd3c71bcf29e8df7fdaf18448c3ff65c1035 (patch) | |
tree | e4c1b50f1aa53b8b2023ca8b5c1d94551b1b4cdb /searx/webapp.py | |
parent | 7927baf545013b829611834c20042c7b17e18ca0 (diff) | |
download | searxng-8744dd3c71bcf29e8df7fdaf18448c3ff65c1035.tar.gz searxng-8744dd3c71bcf29e8df7fdaf18448c3ff65c1035.zip |
[feat] metrics: support for open metrics
Diffstat (limited to 'searx/webapp.py')
-rwxr-xr-x | searx/webapp.py | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/searx/webapp.py b/searx/webapp.py index d2d486d20..1c0158382 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -87,10 +87,7 @@ from searx.webadapter import ( get_selected_categories, parse_lang, ) -from searx.utils import ( - gen_useragent, - dict_subset, -) +from searx.utils import gen_useragent, dict_subset from searx.version import VERSION_STRING, GIT_URL, GIT_BRANCH from searx.query import RawTextQuery from searx.plugins import Plugin, plugins, initialize as plugin_initialize @@ -104,13 +101,7 @@ from searx.answerers import ( answerers, ask, ) -from searx.metrics import ( - get_engines_stats, - get_engine_errors, - get_reliabilities, - histogram, - counter, -) +from searx.metrics import get_engines_stats, get_engine_errors, get_reliabilities, histogram, counter, openmetrics from searx.flaskfix import patch_application from searx.locales import ( @@ -1218,6 +1209,30 @@ def stats_checker(): return jsonify(result) +@app.route('/metrics') +def stats_open_metrics(): + password = settings['general'].get("open_metrics") + + if not (settings['general'].get("enable_metrics") and password): + return Response('open metrics is disabled', status=404, mimetype='text/plain') + + if not request.authorization or request.authorization.password != password: + return Response('access forbidden', status=401, mimetype='text/plain') + + filtered_engines = dict(filter(lambda kv: request.preferences.validate_token(kv[1]), engines.items())) + + checker_results = checker_get_result() + checker_results = ( + checker_results['engines'] if checker_results['status'] == 'ok' and 'engines' in checker_results else {} + ) + + engine_stats = get_engines_stats(filtered_engines) + engine_reliabilities = get_reliabilities(filtered_engines, checker_results) + metrics_text = openmetrics(engine_stats, engine_reliabilities) + + return Response(metrics_text, mimetype='text/plain') + + @app.route('/robots.txt', methods=['GET']) def robots(): return Response( |