Skip to content

Commit f35321f

Browse files
committed
docs(tracer): update ServiceLens image w/ API GW, copywriting
1 parent 12b35f5 commit f35321f

File tree

2 files changed

+53
-42
lines changed

2 files changed

+53
-42
lines changed
-336 KB
Loading

docs/quickstart.md

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -718,69 +718,80 @@ Repeat the process of building, deploying, and invoking your application via the
718718

719719
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.
720720

721-
![Filtering traces by annotations](./media/tracer_xray_sdk_enriched_2.png)
721+
![Filtering traces by metadata](./media/tracer_xray_sdk_enriched_2.png)
722722

723723
### Simplifying with Tracer
724724

725-
Now, let's try to simplify it with Lambda Powertools:
725+
Cross-cutting concerns like filtering traces by Cold Start, including response as well as exceptions as tracing metadata can take a considerable amount of boilerplate.
726726

727-
=== "app.py"
727+
We can simplify our previous patterns by using [Lambda Powertools Tracer](core/tracer.md){target="_blank"}; a thin wrapper on top of X-Ray SDK.
728728

729-
```python hl_lines="3 13 15 21 23 29"
730-
import json
729+
!!! note
730+
You can now safely remove `aws-xray-sdk` from `requirements.txt`; keep `aws-lambda-powertools` only.
731731

732-
from aws_lambda_powertools import Logger, Tracer
733-
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
734-
from aws_lambda_powertools.logging import correlation_paths
732+
```python title="Refactoring with Lambda Powertools Tracer" hl_lines="3 8 13 15 21 23 29"
733+
import json
735734

736-
logger = Logger(service="APP")
737-
tracer = Tracer()
738-
app = ApiGatewayResolver()
735+
from aws_lambda_powertools import Logger, Tracer
736+
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
737+
from aws_lambda_powertools.logging import correlation_paths
739738

739+
logger = Logger(service="APP")
740+
tracer = Tracer(service="APP")
741+
app = ApiGatewayResolver()
740742

741-
@app.get("/hello/<name>")
742-
@tracer.capture_method
743-
def hello_name(name):
744-
tracer.put_annotation("User", name)
745-
logger.info(f"Request from {name} received")
746-
return {"message": f"hello {name}!"}
747743

744+
@app.get("/hello/<name>")
745+
@tracer.capture_method
746+
def hello_name(name):
747+
tracer.put_annotation(key="User", value=name)
748+
logger.info(f"Request from {name} received")
749+
return {"message": f"hello {name}!"}
748750

749-
@app.get("/hello")
750-
@tracer.capture_method
751-
def hello():
752-
tracer.put_annotation("User", "unknown")
753-
logger.info("Request from unknown received")
754-
return {"message": "hello unknown!"}
755751

752+
@app.get("/hello")
753+
@tracer.capture_method
754+
def hello():
755+
tracer.put_annotation(key="User", value="unknown")
756+
logger.info("Request from unknown received")
757+
return {"message": "hello unknown!"}
756758

757-
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST, log_event=True)
758-
@tracer.capture_lambda_handler
759-
def lambda_handler(event, context):
760-
return app.resolve(event, context)
761-
```
762759

763-
With powertools tracer we have much cleaner code right now.
760+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST, log_event=True)
761+
@tracer.capture_lambda_handler
762+
def lambda_handler(event, context):
763+
return app.resolve(event, context)
764+
```
765+
766+
Decorators, annotations and metadata are largely the same, except we now have a much cleaner code as the boilerplate is gone. Here's what's changed compared to AWS X-Ray SDK approach:
767+
768+
* **L8**: We initialize `Tracer` and define the name of our service (`APP`). We automatically run `patch_all` from AWS X-Ray SDK on your behalf. Any previously patched or non-imported library is simply ignored
769+
* **L13**: We use `@tracer.capture_method` decorator instead of `xray_recorder.capture`. We automatically **1/** create a subsegment named after the function name (`## hello_name`), and **2/** add the response/exception as tracing metadata
770+
* **L15**: Putting annotations remain exactly the same UX
771+
* **L29**: We use `@tracer.lambda_handler` so we automatically add `ColdStart` annotation within Tracer itself. We also add a new `Service` annotation using the value of `Tracer(service="APP")`, so that you can filter traces by the service your function(s) represent.
772+
773+
Another subtle difference is that you can now run your Lambda functions and unit test them locally without having to explicitly disable Tracer.
774+
775+
Lambda Powertools optimizes for Lambda compute environment. As such, we add these and other common approaches to accelerate your development, so you don't worry about implementing every cross-cutting concern.
764776

765-
To make our methods visible in the traces, we add `@tracer.capture_method` decorator to the processing methods.
766-
We add annotations directly in the code without adding it with the context handler using the `tracer.put_annotation` method.
767-
Since we add the `@tracer.capture_lambda_handler` decorator for our `lambda_handler`, powertools automatically adds cold start information as an annotation.
768-
It also automatically append Lambda response as a metadata into trace, so we don't need to worry about it.
769777
!!! tip
770-
For differences between annotations and metadata in traces, please follow [link](https://awslabs.github.io/aws-lambda-powertools-python/latest/core/tracer/#annotations-metadata).
778+
You can [opt-out some of these behaviours](./core/tracer/#advanced){target="_blank"} like disabling response capturing, explicitly patching only X modules, etc.
771779

772-
Therefore, you should see traces of your Lambda in the X-ray console.
773-
=== "Example X-RAY Console View"
774-
![Tracer utility](./media/tracer_utility_showcase_2.png)
780+
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 see a similar view:
775781

776-
You may consider using **CloudWatch ServiceLens** which links the CloudWatch metrics and logs, in addition to traces from the AWS X-Ray.
777782

778-
It gives you a complete view of your apps and their dependencies, making your services more observable.
779-
From here, you can browse to specific logs in CloudWatch Logs Insight, Metrics Dashboard or Traces in CloudWatch X-Ray traces.
780-
=== "Example CloudWatch ServiceLens View"
783+
![AWS X-Ray Console trace view using Lambda Powertools Tracer](./media/tracer_utility_showcase_2.png)
784+
785+
!!! tip
786+
Consider using **Amazon CloudWatch ServiceLens** view as it aggregates AWS X-Ray traces and CloudWatch metrics and logs in one view.
787+
788+
From here, you can browse to specific logs in CloudWatch Logs Insight, Metrics Dashboard or AWS X-Ray traces.
789+
781790
![CloudWatch ServiceLens View](./media/tracer_utility_showcase_3.png)
791+
782792
!!! Info
783-
For more information on CloudWatch ServiceLens, please visit [link](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/ServiceLens.html).
793+
For more information on Amazon CloudWatch ServiceLens, please visit [link](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/ServiceLens.html).
794+
784795
## Custom Metrics
785796
The final step to provide complete observability is to add business metrics (such as number of sales or reservations).
786797
Lambda adds technical metrics (such as Invocations, Duration, Error Count & Success Rate) to the CloudWatch metrics out of the box.

0 commit comments

Comments
 (0)