From 60434dc39d41aba63feaacbb247c3d163c3b8c83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sat, 15 Jul 2023 11:27:44 +0200 Subject: [PATCH] misc: remove support for publishing prometheus metrics (#79709) --- debian/control | 1 - debian/debian_config_common.py | 2 - hobo/middleware/__init__.py | 1 - hobo/middleware/stats.py | 108 --------------------------------- requirements.txt | 1 - setup.py | 1 - 6 files changed, 114 deletions(-) delete mode 100644 hobo/middleware/stats.py diff --git a/debian/control b/debian/control index 3b05fed..03e8a2d 100644 --- a/debian/control +++ b/debian/control @@ -19,7 +19,6 @@ Depends: python3-apt, python3-dnspython, python3-memcache, python3-num2words, - python3-prometheus-client, python3-publik-django-templatetags, python3-requests, python3-systemd, diff --git a/debian/debian_config_common.py b/debian/debian_config_common.py index 821bed9..4ddc1ee 100644 --- a/debian/debian_config_common.py +++ b/debian/debian_config_common.py @@ -382,8 +382,6 @@ MIDDLEWARE = ( 'hobo.middleware.xforwardedfor.XForwardedForMiddleware', ) + MIDDLEWARE -MIDDLEWARE = MIDDLEWARE + ('hobo.middleware.PrometheusStatsMiddleware',) - HOBO_MANAGER_HOMEPAGE_URL_VAR = 'portal_agent_url' HOBO_MANAGER_HOMEPAGE_TITLE_VAR = 'portal_agent_title' diff --git a/hobo/middleware/__init__.py b/hobo/middleware/__init__.py index a68c04f..e805e9b 100644 --- a/hobo/middleware/__init__.py +++ b/hobo/middleware/__init__.py @@ -1,5 +1,4 @@ from .cookies_samesite import CookiesSameSiteFixMiddleware from .cors import CORSMiddleware from .seo import RobotsTxtMiddleware -from .stats import PrometheusStatsMiddleware from .version import VersionMiddleware diff --git a/hobo/middleware/stats.py b/hobo/middleware/stats.py deleted file mode 100644 index 931e3b0..0000000 --- a/hobo/middleware/stats.py +++ /dev/null @@ -1,108 +0,0 @@ -# hobo - portal to configure and deploy applications -# Copyright (C) 2017 Entr'ouvert -# -# This program is free software: you can redistribute it and/or modify it -# under the terms of the GNU Affero General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . - -import inspect -import time - -import django -import prometheus_client -from django.db import connection -from django.http import HttpResponse -from django.utils.deprecation import MiddlewareMixin - -requests_total_by_host_view_status_method = prometheus_client.Counter( - 'django_http_requests_total_by_host_view_status_method', - 'Count of requests by host, view, status, method.', - ['host', 'view', 'status', 'method'], -) - -requests_bytes_by_host_view_status_method = prometheus_client.Summary( - 'django_http_requests_bytes_by_host_view_status_method', - 'Bytes of requests by host, view, status, method.', - ['host', 'view', 'status', 'method'], -) - -requests_times_by_host_view_status_method = prometheus_client.Summary( - 'django_http_requests_times_by_host_view_status_method', - 'Duration of requests by host, view, status, method.', - ['host', 'view', 'status', 'method'], -) - -requests_queries_count_by_host_view_status_method = prometheus_client.Summary( - 'django_http_requests_queries_count_by_host_view_status_method', - 'Query count by host, view, status, method.', - ['host', 'view', 'status', 'method'], -) - -requests_queries_time_by_host_view_status_method = prometheus_client.Summary( - 'django_http_requests_queries_time_by_host_view_status_method', - 'Query time by host, view, status, method.', - ['host', 'view', 'status', 'method'], -) - - -class PrometheusStatsMiddleware(MiddlewareMixin): - def process_request(self, request): - if request.method == 'GET' and request.path == '/__metrics__/': - return HttpResponse( - prometheus_client.generate_latest(), content_type=prometheus_client.CONTENT_TYPE_LATEST - ) - - connection.force_debug_cursor = True # to count queries - request._stats_t0 = time.time() - return None - - def process_view(self, request, view_func, view_args, view_kwargs): - view = view_func - if not inspect.isfunction(view_func): - view = view.__class__ - try: - request._view_name = '%s.%s' % (view.__module__, view.__name__) - except AttributeError: - pass - - def process_response(self, request, response): - if not hasattr(request, '_stats_t0'): - return response - http_method = request.method - host_name = request.get_host() - total_time = time.time() - request._stats_t0 - - status_code = response.status_code - view_name = getattr(request, '_view_name', '') - requests_total_by_host_view_status_method.labels(host_name, view_name, status_code, http_method).inc() - - if hasattr(response, 'content'): - response_size = len(response.content) - requests_bytes_by_host_view_status_method.labels( - host_name, view_name, status_code, http_method - ).observe(response_size) - - requests_times_by_host_view_status_method.labels( - host_name, view_name, status_code, http_method - ).observe(total_time) - - if connection.queries_logged: - sql_queries_count = len(connection.queries) - sql_queries_time = sum(float(x['time']) for x in connection.queries) - requests_queries_count_by_host_view_status_method.labels( - host_name, view_name, status_code, http_method - ).observe(sql_queries_count) - requests_queries_time_by_host_view_status_method.labels( - host_name, view_name, status_code, http_method - ).observe(sql_queries_time) - - return response diff --git a/requirements.txt b/requirements.txt index 5a81e60..af2db32 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,3 @@ django>=1.11,<1.12 celery<4 django-mellon lxml -prometheus_client diff --git a/setup.py b/setup.py index 372e1f8..fee7c07 100644 --- a/setup.py +++ b/setup.py @@ -153,7 +153,6 @@ setup( 'celery<4' if sys.version_info < (3, 7) else 'celery>=4', 'django-mellon', 'django-tenant-schemas', - 'prometheus_client', 'djangorestframework>=3.12, <3.14', 'dnspython', 'lxml',