You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/quickstart.md
+46-18
Original file line number
Diff line number
Diff line change
@@ -627,22 +627,28 @@ We've made the following changes in `template.yaml` for this to work seamless:
627
627
***L7-8**: Enables tracing for Amazon API Gateway
628
628
***L14**: Enables tracing for our Serverless Function. This will also add a managed IAM Policy named [AWSXRayDaemonWriteAccess](https://console.aws.amazon.com/iam/home#/policies/arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess){target="_blank"} to allow Lambda to send traces to AWS X-Ray.
629
629
630
-
You can now build and deploy our updates with `sam build && sam deploy`. Once deployed, try invoking the application via the API endpoint, then you should see the following result within the [AWS X-Ray Console](https://console.aws.amazon.com/xray/home#/traces/){target="_blank"}.
630
+
You can now build and deploy our updates with `sam build && sam deploy`. Once deployed, try invoking the application via the API endpoint, and visit [AWS X-Ray Console](https://console.aws.amazon.com/xray/home#/traces/){target="_blank"} to see how much progress we've made so far!!
What we've done helps bring an initial visibility, but we can do so much more.
637
637
638
-
cold start invocations
638
+
???+ question
639
+
You're probably asking yourself at least the following questions:
640
+
641
+
* What if I know to search traces by customer name?
642
+
* What about grouping traces with cold starts?
643
+
* Better yet, what if we want to include the request or response of our functions as part of the trace?
639
644
640
-
> Annotations are simple key-value pairs that are indexed for use with filter expressionsMetadata are key-value pairs with values of any type, including objects and lists, but that are not indexed
645
+
Within AWS X-Ray, we can answer these questions by using two features: tracing **Annotations**and **Metadata**.
641
646
642
-
> Metadata are key-value pairs with values of any type, including objects and lists, but that are not indexed
647
+
**Annotations** are simple key-value pairs that are indexed for use with [filter expressions](https://docs.aws.amazon.com/xray/latest/devguide/xray-console-filters.html){target="_blank"}. **Metadata** are key-value pairs with values of any type, including objects and lists, but that are not indexed
643
648
649
+
Let's put them into action.
644
650
645
-
```python title="Capturing cold start as a tracing annotation" hl_lines="3 12-13 17-18 25-26 35-40 42"
651
+
```python title="Enriching traces with annotations and metadata" hl_lines="12 19-20 28-29 37 39-44 47"
646
652
import json
647
653
648
654
from aws_xray_sdk.core import patch_all, xray_recorder
@@ -659,38 +665,60 @@ patch_all()
659
665
660
666
661
667
@app.get("/hello/<name>")
668
+
@xray_recorder.capture('hello_name')
662
669
defhello_name(name):
663
-
withxray_recorder.in_subsegment("hello_name") as subsegment:
664
-
subsegment.put_annotation("User", name)
665
-
logger.info(f"Request from {name} received")
666
-
return {"message": f"hello {name}!"}
670
+
subsegment =xray_recorder.current_subsegment()
671
+
subsegment.put_annotation("User", name)
672
+
logger.info(f"Request from {name} received")
673
+
return {"message": f"hello {name}!"}
667
674
668
675
669
676
@app.get("/hello")
677
+
@xray_recorder.capture('hello')
670
678
defhello():
671
-
withxray_recorder.in_subsegment("hello") as subsegment:
with xray_recorder.in_subsegment("handler") as subsegment:
687
-
return app.resolve(event, context)
697
+
result = app.resolve(event, context)
698
+
subsegment.put_metadata("response", result)
699
+
700
+
return result
688
701
```
689
702
690
-
* We track Lambda cold start by setting global variable outside of a handler. The variable is defined only upon Lambda initialization. This information provides an overview of how often the runtime is reused by Lambda invoked, which directly impacts Lambda performance and latency.
703
+
Let's break it down:
704
+
705
+
***L12**: We track Lambda cold start by setting global variable outside the handler; this is executed once per sandbox Lambda creates. This information provides an overview of how often the sandbox is reused by Lambda, which directly impacts the performance of each transaction
706
+
***L19-20**: We use AWS X-Ray SDK to add `User` annotation on `hello_name` subsegment. This will allow us to filter traces using the `User` value
707
+
***L28-29**: We repeat what we did in L19-29 except we use the value `unknown` since we don't have that information
708
+
***L37**: We use `global` to modify our global variable defined in the outer scope
709
+
***39-44**: We add `ColdStart` annotation and flip the value of `cold_start` variable, so that subsequent requests annotates the value `false` when the sandbox is reused
710
+
***L47**: We include the final response under `response` key as part of the `handler` subsegment
691
711
692
712
!!! Info
693
-
If you want to understand how the Lambda execution environment works and why cold starts can occur, follow [blog series](https://aws.amazon.com/blogs/compute/operating-lambda-performance-optimization-part-1/).
713
+
If you want to understand how the Lambda execution environment (sandbox) works and why cold starts can occur, see this [blog series on Lambda performance](https://aws.amazon.com/blogs/compute/operating-lambda-performance-optimization-part-1/).
714
+
715
+
Repeat the process of building, deploying, and invoking your application via the API endpoint. Within the [AWS X-Ray Console](https://console.aws.amazon.com/xray/home#/traces/){target="_blank"}, you should now be able to group traces by the `User` and `ColdStart` annotation.
716
+
717
+

718
+
719
+
If you choose any of the traces available, try opening the `handler` subsegment and you should see the response of your Lambda function under the `Metadata` tab.
720
+
721
+

0 commit comments