Skip to content

Commit

Permalink
Add context as iopipe instance attribute (#328)
Browse files Browse the repository at this point in the history
Closes #327
  • Loading branch information
kolanos authored Jun 12, 2019
1 parent 093f1e5 commit 9d989c2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ This package provides analytics and distributed tracing for event-driven applica
- [Chalice](https://github.com/iopipe/iopipe-python#chalice)
- [Serverless](https://github.com/iopipe/iopipe-python#serverless)
- [Zappa](https://github.com/iopipe/iopipe-python#zappa)
- [Accessing Context](https://github.com/iopipe/iopipe-python#accessing-context)

- [Contributing](https://github.com/iopipe/iopipe-python#contributing)
- [Running Tests](https://github.com/iopipe/iopipe-python#running-tests)
- [License](https://github.com/iopipe/iopipe-python#license)
Expand Down Expand Up @@ -675,6 +677,20 @@ Then in your `zappa_settings.json` file include the following:

Where `module-path.to.lambda_handler` is the Python module path to the `lambda_handler` you created above. For example, if you put it in `myproject/__init__.py` the path would be `myproject.lambda_handler`.

### Accessing Context

If the framework you're using makes it non-trivial to access the Lamba context, you can use `iopipe.context`. The `iopipe.context` is `None` if outside of an invocation.

```python
from iopipe import IOpipe

iopipe = IOpipe()

# Be sure to check, can be None
if iopipe.context:
# do something with context
```

## Contributing

Contributions are welcome. We use the [black](https://github.com/ambv/black) code formatter.
Expand Down
3 changes: 2 additions & 1 deletion iopipe/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(self, token=None, url=None, debug=None, plugins=None, **options):

self.config = set_config(**options)
self.config["plugins"] = self.load_plugins(self.config["plugins"])
self.context = None
self.futures = []
self.pool = futures.ThreadPoolExecutor()
self.report = None
Expand Down Expand Up @@ -88,7 +89,7 @@ def __call__(self, func):
def wrapped(event, context):
logger.debug("%s wrapped with IOpipe decorator" % repr(func))

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

# if env var IOPIPE_ENABLED is set to False skip reporting
if self.config["enabled"] is False:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,16 @@ def test_disabled_reporting_with_error(
handler(None, mock_context)

assert iopipe.report.sent is False


@mock.patch("iopipe.report.send_report", autospec=True)
def test_context_attribute(mock_send_report, handler, mock_context):
"""Assert that the current context is available as an iopipe attribute"""
iopipe, handler = handler

assert hasattr(iopipe, "context")
assert iopipe.context is None

handler(None, mock_context)

assert iopipe.context is not None

0 comments on commit 9d989c2

Please sign in to comment.