Tracing with X-ray

In this section we will enable X-ray for the Lambda function to track incoming and outgoing requests to the function, knowing how much time each segment of the function takes. Then we will know where the function is slow and from there easily optimize it.

  1. Open AWS Lambda console.

    • Click Functions on the left menu.
    • Choose the book_delete function. XrayTrace
  2. At book_delete page.

    • Click the Configuration tab.
    • Click the Monitoring and operations tools on the lef menu.
    • Click the Edit button. XrayTrace
  3. At Edit monitoring tools page.

    • Check on the Enable at Lambda service traces. XrayTrace
    • Scroll down and click the Save button. XrayTrace
    • You should see a banner indicating the function was successfully updated. XrayTrace
  4. Open Postman to call api.

    • Click + to add a new tab.
    • Select DELETE method.
    • Enter URL of the deleting API with book_id, for example is 2.
    • Click Send.
    • After completing, the book with id 2 is deleted. XrayTrace
  5. Back to book_delete page.

    • Click the Monitor tab.
    • Drop down View CloudWatch logs to choose the View X-Ray traces button. XrayTrace
  6. At CloudWatch page.

    • Click the Traces on the left menu.
    • Click the Run query button. XrayTrace
    • Then, scroll down and select the current displaced trace with status OK. XrayTrace
  7. At Trace 1-… page. Currently, because we haven’t implemented the SDK patching, the trace map will only show basic blocks (Client -> Lambda Context -> Lambda Function). XrayTrace

  8. Go to the root directory of fcj-book-store-sam-ws8 project. Open the fcj-book-store-sam-ws8/fcj-book-shop/book_delete directory in your editor.

    • Create a file called requirements.txt with the below content.

      aws_xray_sdk
      

      XrayTrace

    • Open the fcj-book-store-sam-ws8/fcj-book-shop/book_delete/book_delete.py and copy the below content to that file.

      from aws_xray_sdk.core import patch_all
      
      patch_all()
      

      XrayTrace

    • Open your terminal and run the below commands at the root directory of fcj-book-store-sam-ws8 project to apply updates.

      sam build
      

      XrayTrace

      sam deploy
      

      XrayTrace

    • If prompted to deploy changeset, type y to confirm. XrayTrace

    • Wait for the Cloudformation stack update to show successfully updated. XrayTrace

  9. Back to book_delete in the AWS console.

    • Click the Code tab.
    • Check if the book_delete function is safely updated. XrayTrace
  10. Open Postman to recall the DELETE api with the id 4. XrayTrace

  11. Back to CloudWatch page.

    • Click the Traces on the left menu.
    • Click the Run query button and select the newly traced request with status OK XrayTrace
  12. At Trace 1-… page. Because we applied SDK patching, the trace map now captures more specific downstream dependencies (such as the S3 bucket node along with DynamoDB connections) natively! XrayTrace

    • Scroll down to the Segments Timeline, you will see a detailed subsegment breakdown for specific services:
    • Initialization subsegment: Represents the init phase of the Lambda execution environment lifecycle.
    • DynamoDB and S3 subsegments: Shows the specific latency for API requests toward these specific resources.
    • Overhead: Represents the period that occurs between the time that the runtime sends the response and signal for the next call. XrayTrace