Skip to content

Commit

Permalink
Make event info plugin toggleable via env var (#264)
Browse files Browse the repository at this point in the history
Closes #253
  • Loading branch information
kolanos authored Sep 25, 2018
1 parent a6120ff commit aff23f2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
26 changes: 23 additions & 3 deletions iopipe/contrib/eventinfo/plugin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
from distutils.util import strtobool
import os

from iopipe.plugins import Plugin

from .event_types import metrics_for_event_type


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
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/contrib/eventinfo/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import mock
import os

from iopipe.contrib.eventinfo import EventInfoPlugin


@mock.patch("iopipe.report.send_report", autospec=True)
Expand Down Expand Up @@ -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

0 comments on commit aff23f2

Please sign in to comment.