concurrent asyncio tasks and xray are not working with Lambda
See original GitHub issueHello, I am opening this issue on request of @NathanielRN after a discussion with him in https://github.com/aws/aws-xray-sdk-python/issues/203 .
The problem is that when using aioboto3, asyncio (with gather or wait) and xray in Lambda we either get an exception or no subsegments showing calls to S3 or SQS. Here is the last code snippet from that issue:
import asyncio
from io import BytesIO
import aioboto3
from aws_xray_sdk.core import xray_recorder, patch_all
from aws_xray_sdk.core.async_context import AsyncContext
# here I tried adding your solution (not added when solution to use AsyncContext() is used below)
# xray_recorder.configure(service='repro_xray_issue') # uncomment this for second solution trial
patch_all()
def lambda_handler(a,b):
asyncio.run(main())
async def main():
# here I tried adding your solution (not added when solution to remove AsyncContext() is used above)
xray_recorder.configure(service='repro_xray_issue', context=AsyncContext()) # comment this for second solution trial
async with xray_recorder.in_segment_async('my_segment_name') as segment: # comment this for second solution trial
filelike1 = BytesIO()
filelike2 = BytesIO()
res1, res2 = await asyncio.gather(s3_get(filelike1), s3_get(filelike2)) # .wait() doesn't work either
@xray_recorder.capture_async('s3_get') # tried with this and without also
async def s3_get(filelike):
async with aioboto3.Session().client('s3') as s3:
return await s3.download_fileobj('s3-validation-files-003', 'test.txt', filelike)
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Asyncio is not working within my Python3.7 lambda
I am using AWS Xray to look at the timing and it is easy to verify that the async is not working correctly....
Read more >Using AWS Lambda with AWS X-Ray - AWS Documentation
You can use AWS X-Ray to visualize the components of your application, identify performance bottlenecks, and troubleshoot requests that resulted in an error ......
Read more >Tracer - AWS Lambda Powertools for Python
Tracer is an opinionated thin wrapper for AWS X-Ray Python SDK. Tracer showcase ... Auto-disable when not running in AWS Lambda environment ...
Read more >aws-xray-sdk - PyPI
Opening Issues. If you encounter a bug with the AWS X-Ray SDK for Python, we want to hear about it. Before opening a...
Read more >The Right Way of Tracing AWS Lambda Functions - InfoQ
Despite Lambda's integration with CloudWatch for metrics and logs, and X-Ray for distributed tracing, it is still a considerable challenge ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@NathanielRN this, propagation, happens automatically when I don’t use
Something happens here that detaches the API incoming trace from Lambda custom trace. We can see that the trace is reaching the Lambda handler in the other parallel trace :
I ended up doing this, though it feels counter-intuitive, it shows one full trace:
Hey @abivolmv, thanks for your patience! Can you see release 2.10.0 now? https://github.com/aws/aws-xray-sdk-python/releases/tag/2.10.0
Let us know if it works for you 🙂