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
+93-30Lines changed: 93 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -523,18 +523,22 @@ By having structured logs like this, we can easily search and analyse them in [C
523
523
!!! note
524
524
You won't see any traces in AWS X-Ray when executing your function locally.
525
525
526
-
The next improvement is to add an appropriate tracking mechanism to your stack. Developers want to analyze traces of queries that pass via the API gateway to your Lambda.
527
-
With structured logs, it is an important step to provide the observability of your application!
528
-
The AWS service that has these capabilities is [AWS X-RAY](https://aws.amazon.com/xray/). How do we send application trace to the AWS X-RAY service then?
526
+
The next improvement is to add distributed tracing to your stack. Traces help you visualize end-to-end transactions or parts of it to easily debug upstream/downstream anomalies.
529
527
530
-
Let's first explore how we can achieve this with [x-ray SDK](https://docs.aws.amazon.com/xray-sdk-for-python/latest/reference/index.html), and then try to simplify it with the Powertools library.
528
+
Combined with structured logs, it is an important step to be able to observe how your application runs in production.
529
+
530
+
### Generating traces
531
+
532
+
[AWS X-Ray](https://aws.amazon.com/xray/){target="_blank"} is the distributed tracing service we're going to use. But how do we generate application traces in the first place?
533
+
534
+
It's a [two-step process](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html){target="_blank"}: **1/** enable tracing in your Lambda function, and **2/** instrument your application code. Let's explore how we can instrument our code with [AWS X-Ray SDK](https://docs.aws.amazon.com/xray-sdk-for-python/latest/reference/index.html){target="_blank"}, and then simplify it with [Lambda Powertools Tracer](core/tracer.md){target="_blank"} feature.
531
535
532
536
=== "app.py"
533
537
534
-
```python hl_lines="3 14 19-20 27-28 35-41"
538
+
```python hl_lines="3 12 16 23 30"
535
539
import json
536
540
537
-
from aws_xray_sdk.core import xray_recorder
541
+
from aws_xray_sdk.core import patch_all, xray_recorder
538
542
539
543
from aws_lambda_powertools import Logger
540
544
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
@@ -543,42 +547,32 @@ Let's first explore how we can achieve this with [x-ray SDK](https://docs.aws.am
543
547
logger = Logger(service="APP")
544
548
545
549
app = ApiGatewayResolver()
546
-
547
-
548
-
cold_start = True
550
+
patch_all()
549
551
550
552
551
553
@app.get("/hello/<name>")
554
+
@xray_recorder.capture('hello_name')
552
555
def hello_name(name):
553
-
with xray_recorder.in_subsegment("hello_name") as subsegment:
* First, we import required X-ray SDK classes. `xray_recorder` is a global AWS X-ray recorder class instance that starts/ends segments/sub-segments and sends them to the X-ray daemon.
614
-
* To build new sub-segments, we use `xray_recorder.in_subsegment` method as a context manager.
615
-
* 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.
607
+
Let's break it down:
608
+
609
+
***L1**: First, we import AWS X-Ray SDK. `xray_recorder` records blocks of code being traced ([subsegment](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-subsegments){target="_blank"}). It also sends generated traces to the AWS X-Ray daemon running in the Lambda service who subsequently forwards them to AWS X-Ray service
610
+
***L12**: We patch [supported libraries](https://docs.aws.amazon.com/xray-sdk-for-python/latest/reference/thirdparty.html#patching-supported-libraries){target="_blank"} that might have been imported, e.g., AWS SDK, requests, httplib, etc.
611
+
***L16**: We decorate our function so the SDK traces the end-to-end execution, and the argument names the generated block being traced
612
+
613
+
???+ question
614
+
But how do I enable tracing for the Lambda function and what permissions do I need?
615
+
616
+
Within `template.yaml` on line 14, we added a new Serverless Function property: `Tracing: Active`. This will enable tracing for the [Lambda function resource](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html#services-xray-cloudformation){target="_blank"}, and 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.
617
+
618
+
!!! danger "TODO: Revisit to see if it's still necessary"
616
619
617
-
To allow the tracking of our Lambda, we need to set it up in our SAM template and add `Tracing: Active` under Lambda `Properties` section.
618
620
!!! Info
619
621
Want to know more about context managers and understand the benefits of using them? Follow [article](https://realpython.com/python-with-statement/) from Real Python.
622
+
623
+
624
+
### Enriching our generates traces
625
+
626
+
627
+
cold start invocations
628
+
629
+
> 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
630
+
631
+
> Metadata are key-value pairs with values of any type, including objects and lists, but that are not indexed
632
+
633
+
634
+
```python title="Capturing cold start as a tracing annotation" hl_lines="3 12-13 17-18 25-26 35-40 42"
635
+
import json
636
+
637
+
from aws_xray_sdk.core import patch_all, xray_recorder
638
+
639
+
from aws_lambda_powertools import Logger
640
+
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
641
+
from aws_lambda_powertools.logging import correlation_paths
642
+
643
+
logger = Logger(service="APP")
644
+
645
+
app = ApiGatewayResolver()
646
+
cold_start =True
647
+
patch_all()
648
+
649
+
650
+
@app.get("/hello/<name>")
651
+
defhello_name(name):
652
+
with xray_recorder.in_subsegment("hello_name") as subsegment:
653
+
subsegment.put_annotation("User", name)
654
+
logger.info(f"Request from {name} received")
655
+
return {"message": f"hello {name}!"}
656
+
657
+
658
+
@app.get("/hello")
659
+
defhello():
660
+
with xray_recorder.in_subsegment("hello") as subsegment:
with xray_recorder.in_subsegment("handler") as subsegment:
676
+
return app.resolve(event, context)
677
+
```
678
+
679
+
* 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.
680
+
620
681
!!! Info
621
682
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/).
622
683
684
+
### Simplifying with Tracer
685
+
623
686
Now, let's try to simplify it with Lambda Powertools:
0 commit comments