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

Add context as iopipe instance attribute #328

Merged
merged 1 commit into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
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