Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response Event Info, Step Function Meta Data #341

Merged
merged 5 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ jobs:
- run:
name: Check code style
command: |
pip install black==18.6b2
black --check --line-length=88 --safe iopipe
black --check --line-length=88 --safe tests
pip install black==19.3b0
black iopipe
black tests

coverage:
working_directory: ~/iopipe-python
Expand Down
9 changes: 3 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
repos:
- repo: https://github.com/ambv/black
rev: 18.6b2

- repo: https://github.com/psf/black
rev: stable
hooks:
- id: black
args: [--line-length=88, --safe]
python_version: python3.6
language_version: python3.7
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@ class MyPlugin(Plugin):
def post_invoke(self, event, context):
pass

def post_response(self, response):
pass

def pre_report(self, report):
pass

Expand All @@ -519,12 +522,13 @@ A plugin has the following properties defined:

A plugin has the following methods defined:

- `pre_setup`: Is called once prior to the agent initialization. Is passed the `iopipe` instance.
- `post_setup`: Is called once after the agent is initialized, is passed the `iopipe` instance.
- `pre_invoke`: Is called prior to each invocation, is passed the `event` and `context` of the invocation.
- `post_invoke`: Is called after each invocation, is passed the `event` and `context` of the invocation.
- `pre_report`: Is called prior to each report being sent, is passed the `report` instance.
- `post_report`: Is called after each report is sent, is passed the `report` instance.
- `pre_setup`: Is called once prior to the agent initialization; is passed the `iopipe` instance.
- `post_setup`: Is called once after the agent is initialized; is passed the `iopipe` instance.
- `pre_invoke`: Is called prior to each invocation; is passed the `event` and `context` of the invocation.
- `post_invoke`: Is called after each invocation; is passed the `event` and `context` of the invocation.
- `post_response`: Is called after the invocation response; is passed the `response`value.
- `pre_report`: Is called prior to each report being sent; is passed the `report` instance.
- `post_report`: Is called after each report is sent; is passed the `report` instance.

## Supported Python Versions

Expand Down
7 changes: 5 additions & 2 deletions iopipe/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def error(self, error):

err = error

def __call__(self, func):
def __call__(self, func, **kwargs):
@functools.wraps(func)
def wrapped(event, context):
# Skip if function is already instrumented
Expand All @@ -96,7 +96,7 @@ def wrapped(event, context):

logger.debug("Wrapping %s with IOpipe decorator" % repr(func))

self.context = context = ContextWrapper(context, self)
self.context = context = ContextWrapper(context, self, **kwargs)

# if env var IOPIPE_ENABLED is set to False skip reporting
if self.config["enabled"] is False:
Expand Down Expand Up @@ -197,6 +197,9 @@ def wrapped(event, context):

decorator = __call__

def step(self, func):
return self(func, step_function=True)

def load_plugins(self, plugins):
"""
Loads plugins that match the `Plugin` interface and are instantiated.
Expand Down
18 changes: 15 additions & 3 deletions iopipe/context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import decimal
import logging
import numbers
import uuid
import warnings

from . import constants
Expand All @@ -25,20 +26,22 @@ def __getattr__(self, name):


class ContextWrapper(object):
def __init__(self, base_context, instance):
def __init__(self, base_context, instance, **kwargs):
self.base_context = base_context
self.instance = instance
self.iopipe = IOpipeContext(self.instance)
self.iopipe = IOpipeContext(self.instance, **kwargs)

def __getattr__(self, name):
return getattr(self.base_context, name)


class IOpipeContext(object):
def __init__(self, instance):
def __init__(self, instance, **kwargs):
self.instance = instance
self.log = LogWrapper(self)
self.disabled = False
self.is_step_function = kwargs.pop("step_function", False)
self.step_meta = None

def metric(self, key, value):
if self.instance.report is None:
Expand Down Expand Up @@ -129,3 +132,12 @@ def unregister(self, name):

def disable(self):
self.disabled = True

def collect_step_meta(self, event):
if self.is_step_function:
self.step_meta = event.get("iopipe", {"id": str(uuid.uuid4()), "step": 0})

def inject_step_meta(self, response):
if self.step_meta and isinstance(response, dict):
self.step_meta["step"] += 1
response["iopipe"] = self.step_meta
7 changes: 7 additions & 0 deletions iopipe/contrib/eventinfo/event_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

class EventType(object):
keys = []
response_keys = []
exclude_keys = []
required_keys = []
source = None
Expand Down Expand Up @@ -266,3 +267,9 @@ def metrics_for_event_type(event, context):
event_info = event_type.collect()
[context.iopipe.metric(k, v) for k, v in event_info.items()]
break

if context.iopipe.is_step_function:
context.iopipe.collect_step_meta(event)
if context.iopipe.step_meta:
for key, value in context.iopipe.step_meta.items():
context.iopipe.metric("@iopipe/event-info.stepFunction.%s" % key, value)
5 changes: 3 additions & 2 deletions iopipe/contrib/eventinfo/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ def post_setup(self, iopipe):
pass

def pre_invoke(self, event, context):
pass
self.context = context

def post_invoke(self, event, context):
if self.enabled:
metrics_for_event_type(event, context)

def post_response(self, response):
pass
if self.enabled:
self.context.iopipe.inject_step_meta(response)

def pre_report(self, report):
pass
Expand Down
18 changes: 18 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[tool.black]
line-length = 88
target-version = ['py27', 'py36', 'py37', 'py38']
include = '\.pyi?$'
exclude = '''
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
)/
'''
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
packages=find_packages(exclude=("tests", "tests.*")),
extras_require={
"coverage": coverage_requires,
"dev": tests_require + ["black==18.6b2", "pre-commit"],
"dev": tests_require + ["black==19.3b0", "pre-commit"],
},
install_requires=install_requires,
setup_requires=["pytest-runner==4.2"],
Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/logger/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _handler(event, context):
except Exception as e:
context.iopipe.log.exception(e)

print("This is not a misprint.")
print ("This is not a misprint.")

return iopipe_with_logger, _handler

Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/logger/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def test__logger_plugin__use_tmp__disk_used(
pytest.skip("this test requires linux, skipping")

disk_usage = read_disk()
print(disk_usage)
print (disk_usage)

iopipe, handler = handler_with_logger_use_tmp
handler({}, mock_context)
Expand Down