From ad398abf100695bcb2cc5fcd4cdf634a5473cade Mon Sep 17 00:00:00 2001 From: Sam Morris Date: Sun, 26 Feb 2023 14:17:37 +0000 Subject: [PATCH 1/3] Provide a message explaining that metrics are disabled when running in the Flask development server with reloading enabled --- prometheus_flask_exporter/__init__.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/prometheus_flask_exporter/__init__.py b/prometheus_flask_exporter/__init__.py index 93ec1ce..32aebf3 100644 --- a/prometheus_flask_exporter/__init__.py +++ b/prometheus_flask_exporter/__init__.py @@ -263,12 +263,17 @@ def register_endpoint(self, path, app=None): (by default it is the application registered with this class) """ - if is_running_from_reloader() and not os.environ.get('DEBUG_METRICS'): - return - if app is None: app = self.app or current_app + @self.do_not_track() + def metrics_disabled(): + return ( + 'Metrics are disabled when run in the Flask development server' + ' with reload enabled. Set the environment variable' + ' DEBUG_METRICS=1 to enable them anyway.\r\n' + ) + @self.do_not_track() def prometheus_metrics(): accept_header = request.headers.get("Accept") @@ -281,12 +286,17 @@ def prometheus_metrics(): headers = {'Content-Type': content_type} return generated_data, 200, headers + if is_running_from_reloader() and not int(os.environ.get('DEBUG_METRICS', '0')): + metrics_view = metrics_disabled + else: + metrics_view = prometheus_metrics + # apply any user supplied decorators, like authentication if self._metrics_decorator: prometheus_metrics = self._metrics_decorator(prometheus_metrics) # apply the Flask route decorator on our metrics endpoint - app.route(path)(prometheus_metrics) + app.route(path)(metrics_view) def generate_metrics(self, accept_header=None, names=None): """ From 17180200ea512a5a532fc69cd618b8168d3d0dda Mon Sep 17 00:00:00 2001 From: Sam Morris Date: Mon, 27 Feb 2023 13:04:30 +0000 Subject: [PATCH 2/3] Provide metrics disabled warning via logging instead of HTTP response --- prometheus_flask_exporter/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/prometheus_flask_exporter/__init__.py b/prometheus_flask_exporter/__init__.py index 32aebf3..355aaf2 100644 --- a/prometheus_flask_exporter/__init__.py +++ b/prometheus_flask_exporter/__init__.py @@ -7,7 +7,7 @@ import warnings from timeit import default_timer -from flask import Flask, Response +from flask import Flask, Response, abort from flask import request, make_response, current_app from flask.views import MethodView from prometheus_client import Counter, Histogram, Gauge, Summary @@ -268,11 +268,12 @@ def register_endpoint(self, path, app=None): @self.do_not_track() def metrics_disabled(): - return ( + app.logger.warning( 'Metrics are disabled when run in the Flask development server' ' with reload enabled. Set the environment variable' - ' DEBUG_METRICS=1 to enable them anyway.\r\n' + ' DEBUG_METRICS=1 to enable them anyway.' ) + abort(404) @self.do_not_track() def prometheus_metrics(): From b23d17843df07dbd5a9815f5ec3224bb605c4947 Mon Sep 17 00:00:00 2001 From: Sam Morris Date: Tue, 28 Feb 2023 12:12:36 +0000 Subject: [PATCH 3/3] Log the 'no metrics' warning once instead of in the view --- prometheus_flask_exporter/__init__.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/prometheus_flask_exporter/__init__.py b/prometheus_flask_exporter/__init__.py index 355aaf2..4d98bbe 100644 --- a/prometheus_flask_exporter/__init__.py +++ b/prometheus_flask_exporter/__init__.py @@ -7,7 +7,7 @@ import warnings from timeit import default_timer -from flask import Flask, Response, abort +from flask import Flask, Response from flask import request, make_response, current_app from flask.views import MethodView from prometheus_client import Counter, Histogram, Gauge, Summary @@ -266,14 +266,13 @@ def register_endpoint(self, path, app=None): if app is None: app = self.app or current_app - @self.do_not_track() - def metrics_disabled(): - app.logger.warning( + if is_running_from_reloader() and not os.environ.get('DEBUG_METRICS'): + app.logger.debug( 'Metrics are disabled when run in the Flask development server' ' with reload enabled. Set the environment variable' ' DEBUG_METRICS=1 to enable them anyway.' ) - abort(404) + return @self.do_not_track() def prometheus_metrics(): @@ -287,17 +286,12 @@ def prometheus_metrics(): headers = {'Content-Type': content_type} return generated_data, 200, headers - if is_running_from_reloader() and not int(os.environ.get('DEBUG_METRICS', '0')): - metrics_view = metrics_disabled - else: - metrics_view = prometheus_metrics - # apply any user supplied decorators, like authentication if self._metrics_decorator: prometheus_metrics = self._metrics_decorator(prometheus_metrics) # apply the Flask route decorator on our metrics endpoint - app.route(path)(metrics_view) + app.route(path)(prometheus_metrics) def generate_metrics(self, accept_header=None, names=None): """