From a57dfcd8d9af6299fc264f26e1bcb2907e02a1f9 Mon Sep 17 00:00:00 2001 From: Michael Lavers Date: Tue, 25 Sep 2018 10:41:25 -0700 Subject: [PATCH] Make event info plugin toggleable via env var Closes #253 --- iopipe/contrib/eventinfo/plugin.py | 26 +++++++++++++++++++++++--- tests/contrib/eventinfo/test_plugin.py | 11 +++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/iopipe/contrib/eventinfo/plugin.py b/iopipe/contrib/eventinfo/plugin.py index 95a7d147..a68ff83a 100644 --- a/iopipe/contrib/eventinfo/plugin.py +++ b/iopipe/contrib/eventinfo/plugin.py @@ -1,3 +1,6 @@ +from distutils.util import strtobool +import os + from iopipe.plugins import Plugin from .event_types import metrics_for_event_type @@ -5,9 +8,25 @@ class EventInfoPlugin(Plugin): name = "event-info" - version = "1.2.0" + version = "1.3.0" homepage = "https://github.com/iopipe/iopipe-python#event-info-plugin" - enabled = True + + def __init__(self, enabled=True): + """ + Instantiates the event info plugin + + :param enabled: Whether or not event info should be enabled for all + invocations. Alternatively this plugin can be enabled/disabled + via the `IOPIPE_EVENT_INFO_ENABLED` environment variable. + :type enabled: bool + """ + self._enabled = enabled + + @property + def enabled(self): + return self._enabled is True or bool( + strtobool(os.getenv("IOPIPE_EVENT_INFO_ENABLED", "false")) + ) def pre_setup(self, iopipe): pass @@ -19,7 +38,8 @@ def pre_invoke(self, event, context): pass def post_invoke(self, event, context): - metrics_for_event_type(event, context) + if self.enabled: + metrics_for_event_type(event, context) def pre_report(self, report): pass diff --git a/tests/contrib/eventinfo/test_plugin.py b/tests/contrib/eventinfo/test_plugin.py index ee01a14c..35a613a8 100644 --- a/tests/contrib/eventinfo/test_plugin.py +++ b/tests/contrib/eventinfo/test_plugin.py @@ -1,4 +1,7 @@ import mock +import os + +from iopipe.contrib.eventinfo import EventInfoPlugin @mock.patch("iopipe.report.send_report", autospec=True) @@ -89,3 +92,11 @@ def test__eventinfo_plugin__scheduled( event_type = [m for m in metrics if m["name"] == "@iopipe/event-info.eventType"] assert len(event_type) == 1 assert event_type[0]["s"] == "scheduled" + + +def test__eventinfo_plugin__enabled(monkeypatch): + monkeypatch.setattr(os, "environ", {"IOPIPE_EVENT_INFO_ENABLED": "true"}) + + plugin = EventInfoPlugin(enabled=False) + + assert plugin.enabled is True