Skip to content

Commit

Permalink
Add a method of disabling prometheus & make it efficient
Browse files Browse the repository at this point in the history
Gathering metrics can have a non-negligible cost in time or memory,
so make it possible to disable. Make the disabled case avoid
installation of a callback.
  • Loading branch information
rohanpm committed Apr 27, 2022
1 parent c268c1b commit cbf94b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion docs/user-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ Prometheus metrics

This library automatically collects `Prometheus <https://prometheus.io/>`_
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,
Expand Down
24 changes: 18 additions & 6 deletions more_executors/_impl/metrics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import logging
from functools import partial

Expand All @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit cbf94b2

Please sign in to comment.