Solving Problems You Can't See: How AWS X-Ray and CloudWatch Provide User-Level Observability in Serverless Microservices Applications
Avoid blind spots by tracking users on apps built on AWS.
Step 1: Deploy the aws-serverless-shopping-cart Sample Application
Step 2: Explore the Tracing Capabilities Already Implemented in the Sample Application
A Brief Tour of the Frontend Code
From Your Browser to API Gateway
AWS X-Ray - Your Application’s Tracing Companion
Step 3: Update the Application to Capture the Cognito User ID in AWS X-Ray
Step 4: Group Application Requests by Registered and Anonymous Users
Step 5: View and Diagnose Issues by Registered and Anonymous Users in the AWS X-Ray Console
- How to configure and implement AWS X-Ray in a serverless application that uses Amazon Cognito, Amazon API Gateway, and AWS Lambda
- How to follow and observe your user requests as they flow through Amazon Cognito, Amazon API Gateway, and AWS Lambda
- How to use the AWS X-Ray SDK for python to instrument your python applications with AWS X-Ray
- How to use AWS X-Ray groups to identify and diagnose applications based on user segmentation
- How to use AWS X-Ray CloudWatch metrics to alarm on AWS X-Ray groups
About | |
---|---|
✅ AWS Level | Advanced - 300 |
⏱ Time to complete | 120 minutes |
💰 Cost to complete | Free when using the AWS Free Tier |
🧩 Prerequisites | - AWS Account - Git client |
💻 Code Sample | aws-serverless-shopping-cart |
📢 Feedback | Any feedback, issues, or just a 👍 / 👎 ? |
⏰ Last Updated | 2023-09-26 |
@tracer.capture_lambda_handler
is all that is needed to create an AWS X-Ray annotation named ColdStart as well as an annotation identifying your service in X-Ray traces. It will also capture exceptions generated by your lambda function as AWS X-Ray metadata.<router-view />
. You can also see that the cart drawer (<cart-drawer />
) remains constant, even if the path and route for the application changes.mounted()
), the shopping cart for the user is retrieved:fetchCart()
call is defined as an action in frontend/src/store/actions.js:getCart()
in frontend/src/backend/api.js:getCart()
function initiates the call to retrieve the cart contents from the aws-serverless-shopping-cart-shop-ListCartFunction<hash> AWS Lambda function through API Gateway using the Amplify JavaScript SDK.<router-view></router-view>
position.getProducts()
and getCart()
calls from our frontend code are what initiate our call to retrieve our products and cart contents from our sample applications AWS Lambda functions using API Gateway with the Amplify JavaScript SDK.API.get()
from the Amplify SDK. These endpoints are defined in the frontend/src/aws-exports.js file:@tracer.capture_lambda_handler
annotation.getCart()
function in frontend/src/backend/api.js:cognito:username
) and Cognito user ID (sub
) from the token.- On line 5, you imported the xray_recorder function from the Python AWS X-Ray SDK.
- On line 9, you imported the new get_username function in shared.py that you will add in the next step.
- On line 24, you create a new subsegment called annotations using the X-Ray SDK for Python as soon as your Lambda function handler begins execution.
- On lines 28-32, you create an annotation called generated_cart to indicate whether or not the cart ID was retrieved from the cookie cartId set by the front end user interface.
- On lines 37-38 and 42, you retrieve the username and create an annotation called username which is set to the Cognito username if an authorized login is detected or set to the value 'anonymous'.
- On line 65, you closed the annotations subsegment you created on line 24.
- On lines 62-74, you create a new function named get_username that takes a Cognito JSON Web Token, validates it, and retrieves the username from the token.
remove()
Python list function on the individual product dictionary. Next, you will fix this error and also improve the exception handling for the function by wrapping the execution in a try / except block:- Delete the Anonymous, RegisteredUsers, and SlowRegisteredUsers AWS X-Ray groups you created in Step 4.
- Delete the SlowRegisteredUsersNotification SNS topic you created in Step 5.
- Delete the SlowRegisteredUsersAlarm CloudWatch Alarm you created in Step 5.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.