AWS X-Ray is a service that helps developers debug, instrument, and performance tune applications. This example demostrates how to use the AWS [X-Ray SDK] (http://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-python.html) for Python. to instrument a "mock" application.
First, add two directives to your Python source code
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch
Second, install the required depedency
pip install aws-xray-sdk
pip3 install aws-xray-sdk
Third, wrap sections of your code in subsegements like this:
subsegment = xray_recorder.begin_subsegment('subsegment_name')
# code here...
xray_recorder.end_subsegment()
Lambda functions can be invoked either Asynchronously or Synchronously. To invoke your Lambda function in Async mode specify the --invocation-type flag Event:
aws lambda invoke --function-name xrayPyTest --invocation-type Event out.txt
This X-Ray Trace shows a Lambda function running in Async mode for the first time, with a Cold Start. Notice the Dwell time under AWS::Lambda and the overall duration was just under 1.8s.
This X-Ray Trace shows a Lambda function runnning when Warm. Notice the overall duration was under 250ms.
To invoke your function in Sync, remove the --invoication-type flag, it will default to RequestResponse
aws lambda invoke --function-name xrayPyTest out.txt
This X-Ray trace shows a Lambda function running in Sync mode for the first time, with a Cold Start. Notice the Initialization record under AWS::Lambda::Function and overall function duration of 1.6s.
This X-Ray trace shows a Lambda function running when Warm. Notice there is no Initialization and the function runs under 160ms.
- xrayPyTest - Python example that demostrates how to instrument code with the X-Ray SDK.