Skip to content

Commit 12b35f5

Browse files
committed
docs(tracer): add annotation, metadata, and image
1 parent 1b307d2 commit 12b35f5

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed
81.7 KB
Loading
115 KB
Loading

docs/quickstart.md

+46-18
Original file line numberDiff line numberDiff line change
@@ -627,22 +627,28 @@ We've made the following changes in `template.yaml` for this to work seamless:
627627
* **L7-8**: Enables tracing for Amazon API Gateway
628628
* **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.
629629

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!!
631631

632632
![AWS X-Ray Console trace view](./media/tracer_xray_sdk_showcase.png)
633633

634634
### Enriching our generates traces
635635

636-
What we've done helps bring an initial visibility
636+
What we've done helps bring an initial visibility, but we can do so much more.
637637

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?
639644

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**.
641646

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
643648

649+
Let's put them into action.
644650

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"
646652
import json
647653

648654
from aws_xray_sdk.core import patch_all, xray_recorder
@@ -659,38 +665,60 @@ patch_all()
659665

660666

661667
@app.get("/hello/<name>")
668+
@xray_recorder.capture('hello_name')
662669
def hello_name(name):
663-
with xray_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}!"}
667674

668675

669676
@app.get("/hello")
677+
@xray_recorder.capture('hello')
670678
def hello():
671-
with xray_recorder.in_subsegment("hello") as subsegment:
672-
subsegment.put_annotation("User", "unknown")
673-
logger.info("Request from unknown received")
674-
return {"message": "hello unknown!"}
679+
subsegment = xray_recorder.current_subsegment()
680+
subsegment.put_annotation("User", "unknown")
681+
logger.info("Request from unknown received")
682+
return {"message": "hello unknown!"}
675683

676684

677685
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST, log_event=True)
686+
@xray_recorder.capture('handler')
678687
def lambda_handler(event, context):
679688
global cold_start
689+
690+
subsegment = xray_recorder.current_subsegment()
680691
if cold_start:
681692
subsegment.put_annotation("ColdStart", cold_start)
682693
cold_start = False
683694
else:
684695
subsegment.put_annotation("ColdStart", cold_start)
685696

686-
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
688701
```
689702

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
691711

692712
!!! 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+
![Filtering traces by annotations](./media/tracer_xray_sdk_enriched.png)
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+
![Filtering traces by annotations](./media/tracer_xray_sdk_enriched_2.png)
694722

695723
### Simplifying with Tracer
696724

0 commit comments

Comments
 (0)