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.
    • Click the Save button. 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.
    • Click 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. You can see the subsegment.

    • Initialization subsegment: Represents the init phase of the Lambda execution environment lifecycle. During this phase, Lambda creates or opens an execution environment with configured resources, downloads the function code and all classes, runs the runtime, and initializes the function.
    • Invocation subsegment: Represents the stage when Lambda calls the function handler. This starts with the runtime and registers the extension and it ends when the runtime is ready to send a response.
    • Overhead subsegment: Represents the period that occurs between the time that the runtime sends the response and signal for the next call. During this time, the runtime finishes all tasks associated with an invocation and prepares to freeze the sandbox. 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.

    • 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.

      sam build
      sam validate
      sam deploy
      

      XrayTrace

  9. Back to book_delete page.

    • Click the Code tab.
    • Check if the book_delete function is 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. XrayTrace
    • Then, scroll down and select the current displaced trace with status OK. XrayTrace
  12. At Trace 1-… page. You can see the more specific information than in the previous trace. XrayTrace XrayTrace