diff --git a/docs/user-guide.rst b/docs/user-guide.rst index 83b42a4c..4e3580f1 100644 --- a/docs/user-guide.rst +++ b/docs/user-guide.rst @@ -240,7 +240,8 @@ Prometheus metrics This library automatically collects `Prometheus `_ metrics if the ``prometheus_client`` Python module is available. -The feature is disabled when this module is not installed. +The feature is disabled when this module is not installed or if the +``MORE_EXECUTORS_PROMETHEUS`` environment variable is set to ``0``. If you want to ensure that ``more-executors`` is installed along with all prometheus dependencies, you may request the 'prometheus' extras, diff --git a/more_executors/_impl/metrics/__init__.py b/more_executors/_impl/metrics/__init__.py index e14e0fc5..d314b2a7 100644 --- a/more_executors/_impl/metrics/__init__.py +++ b/more_executors/_impl/metrics/__init__.py @@ -1,3 +1,4 @@ +import os import logging from functools import partial @@ -7,14 +8,17 @@ LOG = logging.getLogger("more-executors.metrics") -try: # pylint: disable=import-error - from .prometheus import PrometheusMetrics +if os.environ.get("MORE_EXECUTORS_PROMETHEUS", "1") == "0": + metrics = NullMetrics() # type: ignore +else: + try: # pylint: disable=import-error + from .prometheus import PrometheusMetrics - metrics = PrometheusMetrics() -except Exception: - LOG.debug("disabling prometheus support", exc_info=True) + metrics = PrometheusMetrics() + except Exception: + LOG.debug("disabling prometheus support", exc_info=True) - metrics = NullMetrics() # type: ignore + metrics = NullMetrics() # type: ignore def record_done(f, started_when, time, inprogress, cancelled, failed): @@ -60,3 +64,11 @@ def track_future(f, **labels): f.add_done_callback(cb) return f + + +def track_future_noop(f, **_labels): + return f + + +if isinstance(metrics, NullMetrics): + track_future = track_future_noop