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 support for aiobotocore #6

Merged
merged 1 commit into from
Nov 15, 2017
Merged

Add support for aiobotocore #6

merged 1 commit into from
Nov 15, 2017

Conversation

terricain
Copy link
Contributor

Closes #5

Added Async functions for begin subsegment and capture decorator.

Also fixed typo from my last PR

The yield from syntax is not valid syntax in py27 so was causing issues when I tried to add the async functions in the recorder module. Even tried some evil generator hacks but then that didn't work when py3 tried to use the functions.
Instead the recorder is subclassed to provide some py3 compatible async functions. When the SDK is imported in py3, the async compatible recorder class will be chosen.

The get_new_stubbed_recorder function was also updated to match the above logic. All the test cases were modified to explicitly set the context, as I was having issues where botocore was using an AsyncContext and then getting in a mess.

Updated the docs to mention if one is using aioboto3/aiobotocore that they should use the AsyncContext and provided a snippet.

Tox seems to be running ok on py27,34,35,36.

def capture_async(self, name=None):
"""
A decorator that records enclosed function in a subsegment.
It only works with synchronous functions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy/paste error

return wrapped(*args, **kwargs)


def aws_meta_processor(wrapped, instance, args, kwargs,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The aws_meta_processor can be shared between botocore and aiobotocore I believe? You can probably move the aws_meta_process one level above so it can be used by two patchers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good shout, Most of the functions in that module can also be shared.

)


async def _xray_traced_aiobotocore(wrapped, instance, args, kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain more on using async def/await on the patcher and previously aiohttp while using async.corotines/yield from on the recorder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, my bad. It was a sort of lazy copy and paste, originally the methods in the async recorder were in the main recorder which had @coroutine on them from compat. Currently them being @asyncio.coroutine will allow them to be used in py34 without complaints, aiobotocore uses aiohttp which has dropped py34 support and is now using async def syntax. So either the async recorder stays compatible with py34 and people can use it with other frameworks, or we just draw a line and just do async purely on py35+. Your choice, I have no preference

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we should support py35+ only.


On top of patching aioboto3 or aiobotocore, the xray_recorder also needs to be
configured to use the ``AsyncContext``. The following snippet shows how to set
up the X-Ray SDK with an Async Context, you probably want to do this near the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove "you probably want to ....". A redirect to the section for configuring the global recorder should be sufficient. And you also need to mention that it only supports python 3.5 and above.

pass


async def test_capture(loop):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for nested async functions? An decorated async function calls another decorated async function should result in two nested subsegments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh cool I didnt realise that, but it makes sense. Will do


commands =
py{27,34}: coverage run --source aws_xray_sdk -m py.test tests --ignore tests/ext/aiohttp --ignore tests/test_async_local_storage.py
py{27,34}: coverage run --source aws_xray_sdk -m py.test tests --ignore tests/ext/aiohttp --ignore tests/ext/aiobotocore --ignore tests/test_async_local_storage.py --ignore tests/test_async_recorder.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again the keyword you used for capture_async actually works with 3.4 but the aiobotocore patcher only work with 3.5.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have used async/await throughout so best to exclude those tests from py34

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I mean let's get all async related stuff to be 3.5+ only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah 😄, async in 3.4 is pretty nasty anyway. I "think" I've excluded 3.4 from anything async and purged any @asyncio.coroutine's too

@terricain
Copy link
Contributor Author

I also updated the code that loads in json to use pkg_resources so then the module can also be used in egg/zip form without being extracted.

Had to do a from __future__ import absolute_import in ext/boto_utils.py as importing the botocore.exceptions module was failing in py27 as botocore folder was in the same directory.

@haotianw465
Copy link
Contributor

One last thing is that I like the new structure of ext/resources/botocore_para_whitelist.json. Could we rename it to aws_para_whitelist.json? It's more obvious as more and more files added under resources/ folder. Also botocore causes some confusion on if that json file is effective to boto3 or aiobotocore etc.

@terricain
Copy link
Contributor Author

Sure, yeah exactly.

@terricain
Copy link
Contributor Author

Should be good

@haotianw465
Copy link
Contributor

Oh one thing I forgot is that feel free to add capture_async and aiohttp middleware support to the repo landing page. To be more specific the README.md Quick Start section.

@terricain
Copy link
Contributor Author

Sure, I'll push some more changes tomorrow.

Also fixed typo from my last PR

yield from is not valid syntax in py27 so was causing issues in the recorder module, instead
recorder is subclassed to provide some py3 compatible async functions and the recorder class]
will be chosen depending on if py2 or py3.

Set the correct context class in all test cases as they would inherit the context of the last
test ran, in some cases botocore was trying to use a async context.

Makes use of async syntax consistent.

Reduced duplication of boto patching functions

Made the library zipsafe when loading resources

Improved test for testing nested subsegments

Renamed botocore_para_whitelist.json

Updated README
@terricain
Copy link
Contributor Author

Added capture async example and a slimmed down example of adding middleware to aiohttp

@haotianw465 haotianw465 merged commit e8a1d4f into aws:master Nov 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Supporting aiobotocore
2 participants