File size: 11,202 Bytes
26e6f31 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | ---
title: Tracer
description: Core utility
---
Tracer is an opinionated thin wrapper for [AWS X-Ray Python SDK](https://github.com/aws/aws-xray-sdk-python/){target="_blank" rel="nofollow"}.

## Key features
* Auto capture cold start as annotation, and responses or full exceptions as metadata
* Auto-disable when not running in AWS Lambda environment
* Support tracing async methods, generators, and context managers
* Auto patch supported modules by AWS X-Ray
## Getting started
???+ tip
All examples shared in this documentation are available within the [project repository](https://github.com/aws-powertools/powertools-lambda-python/tree/develop/examples){target="_blank"}.
!!! note "Tracer relies on AWS X-Ray SDK over [OpenTelememetry Distro (ADOT)](https://aws-otel.github.io/docs/getting-started/lambda){target="_blank" rel="nofollow"} for optimal cold start (lower latency)."
### Install
!!! info "This is not necessary if you're installing Powertools for AWS Lambda (Python) via [Lambda Layer/SAR](../index.md#lambda-layer){target="_blank"}"
Add `aws-lambda-powertools[tracer]` as a dependency in your preferred tool: _e.g._, _requirements.txt_, _pyproject.toml_. This will ensure you have the required dependencies before using Tracer.
### Permissions
Before your use this utility, your AWS Lambda function [must have permissions](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html#services-xray-permissions){target="_blank"} to send traces to AWS X-Ray.
```yaml hl_lines="9 12" title="AWS Serverless Application Model (SAM) example"
--8<-- "examples/tracer/sam/template.yaml"
```
### Lambda handler
You can quickly start by initializing `Tracer` and use `capture_lambda_handler` decorator for your Lambda handler.
```python hl_lines="1 4 12" title="Tracing Lambda handler with capture_lambda_handler"
--8<-- "examples/tracer/src/capture_lambda_handler.py"
```
`capture_lambda_handler` performs these additional tasks to ease operations:
* Creates a `ColdStart` annotation to easily filter traces that have had an initialization overhead
* Creates a `Service` annotation if `service` parameter or `POWERTOOLS_SERVICE_NAME` is set
* Captures any response, or full exceptions generated by the handler, and include as tracing metadata
### Annotations & Metadata
**Annotations** are key-values associated with traces and indexed by AWS X-Ray. You can use them to filter traces and to create [Trace Groups](https://aws.amazon.com/about-aws/whats-new/2018/11/aws-xray-adds-the-ability-to-group-traces/){target="_blank"} to slice and dice your transactions.
```python hl_lines="8" title="Adding annotations with put_annotation method"
--8<-- "examples/tracer/src/put_trace_annotations.py"
```
**Metadata** are key-values also associated with traces but not indexed by AWS X-Ray. You can use them to add additional context for an operation using any native object.
```python hl_lines="19" title="Adding arbitrary metadata with put_metadata method"
--8<-- "examples/tracer/src/put_trace_metadata.py"
```
### Synchronous functions
You can trace synchronous functions using the `capture_method` decorator.
```python hl_lines="7" title="Tracing an arbitrary function with capture_method"
--8<-- "examples/tracer/src/capture_method.py"
```
???+ note "Note: Function responses are auto-captured and stored as JSON, by default."
Use [capture_response](#disabling-response-auto-capture) parameter to override this behaviour.
The serialization is performed by aws-xray-sdk via `jsonpickle` module. This can cause
side effects for file-like objects like boto S3 <a href="https://botocore.amazonaws.com/v1/documentation/api/latest/reference/response.html#botocore.response.StreamingBody">`StreamingBody`</a>, where its response will be read only once during serialization.
### Asynchronous and generator functions
???+ warning
We do not support asynchronous Lambda handler
You can trace asynchronous functions and generator functions (including context managers) using `capture_method`.
=== "capture_method_async.py"
```python hl_lines="9"
--8<-- "examples/tracer/src/capture_method_async.py"
```
=== "capture_method_context_manager.py"
```python hl_lines="12-13"
--8<-- "examples/tracer/src/capture_method_context_manager.py"
```
=== "capture_method_generators.py"
```python hl_lines="9"
--8<-- "examples/tracer/src/capture_method_generators.py"
```
### Environment variables
The following environment variables are available to configure Tracer at a global scope:
| Setting | Description | Environment variable | Default |
|-----------------------|--------------------------------------------------|--------------------------------------|---------|
| **Disable Tracing** | Explicitly disables all tracing. | `POWERTOOLS_TRACE_DISABLED` | `false` |
| **Response Capture** | Captures Lambda or method return as metadata. | `POWERTOOLS_TRACER_CAPTURE_RESPONSE` | `true` |
| **Exception Capture** | Captures Lambda or method exception as metadata. | `POWERTOOLS_TRACER_CAPTURE_ERROR` | `true` |
Both [`POWERTOOLS_TRACER_CAPTURE_RESPONSE`](#disabling-response-auto-capture) and [`POWERTOOLS_TRACER_CAPTURE_ERROR`](#disabling-exception-auto-capture) can be set on a per-method basis, consequently overriding the environment variable value.
## Advanced
### Patching modules
Tracer automatically patches all [supported libraries by X-Ray](https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-python-patching.html){target="_blank"} during initialization, by default. Underneath, AWS X-Ray SDK checks whether a supported library has been imported before patching.
If you're looking to shave a few microseconds, or milliseconds depending on your function memory configuration, you can patch specific modules using `patch_modules` param:
```python hl_lines="8" title="Example of explicitly patching requests only"
--8<-- "examples/tracer/src/patch_modules.py"
```
### Disabling response auto-capture
Use **`capture_response=False`** parameter in both `capture_lambda_handler` and `capture_method` decorators to instruct Tracer **not** to serialize function responses as metadata.
???+ info "Info: This is useful in three common scenarios"
1. You might **return sensitive** information you don't want it to be added to your traces
2. You might manipulate **streaming objects that can be read only once**; this prevents subsequent calls from being empty
3. You might return **more than 64K** of data _e.g., `message too long` error_
=== "disable_capture_response.py"
```python hl_lines="8 15"
--8<-- "examples/tracer/src/disable_capture_response.py"
```
=== "disable_capture_response_streaming_body.py"
```python hl_lines="19"
--8<-- "examples/tracer/src/disable_capture_response_streaming_body.py"
```
### Disabling exception auto-capture
Use **`capture_error=False`** parameter in both `capture_lambda_handler` and `capture_method` decorators to instruct Tracer **not** to serialize exceptions as metadata.
???+ info
Useful when returning sensitive information in exceptions/stack traces you don't control
```python hl_lines="16 26" title="Disabling exception auto-capture for tracing metadata"
--8<-- "examples/tracer/src/disable_capture_error.py"
```
### Ignoring certain HTTP endpoints
You might have endpoints you don't want requests to be traced, perhaps due to the volume of calls or sensitive URLs.
You can use `ignore_endpoint` method with the hostname and/or URLs you'd like it to be ignored - globs (`*`) are allowed.
```python hl_lines="12-13" title="Ignoring certain HTTP endpoints from being traced"
--8<-- "examples/tracer/src/ignore_endpoints.py"
```
### Tracing aiohttp requests
???+ info
This snippet assumes you have aiohttp as a dependency
You can use `aiohttp_trace_config` function to create a valid [aiohttp trace_config object](https://docs.aiohttp.org/en/stable/tracing_reference.html){target="_blank" rel="nofollow"}. This is necessary since X-Ray utilizes [aiohttp](https://docs.aiohttp.org/en/stable/){target="_blank" rel="nofollow"} trace hooks to capture requests end-to-end.
```python hl_lines="7 17" title="Tracing aiohttp requests"
--8<-- "examples/tracer/src/tracing_aiohttp.py"
```
### Escape hatch mechanism
You can use `tracer.provider` attribute to access all methods provided by AWS X-Ray `xray_recorder` object.
This is useful when you need a feature available in X-Ray that is not available in the Tracer utility, for example [thread-safe](https://github.com/aws/aws-xray-sdk-python/#user-content-trace-threadpoolexecutor){target="_blank"}, or [context managers](https://github.com/aws/aws-xray-sdk-python/#user-content-start-a-custom-segmentsubsegment){target="_blank"}.
```python hl_lines="14" title="Tracing a code block with in_subsegment escape hatch"
--8<-- "examples/tracer/src/sdk_escape_hatch.py"
```
### Concurrent asynchronous functions
???+ warning
[X-Ray SDK will raise an exception](https://github.com/aws/aws-xray-sdk-python/issues/164){target="_blank"} when async functions are run and traced concurrently
A safe workaround mechanism is to use `in_subsegment_async` available via Tracer escape hatch (`tracer.provider`).
```python hl_lines="10 17 24" title="Workaround to safely trace async concurrent functions"
--8<-- "examples/tracer/src/capture_method_async_concurrency.py"
```
### Reusing Tracer across your code
Tracer keeps a copy of its configuration after the first initialization. This is useful for scenarios where you want to use Tracer in more than one location across your code base.
???+ warning "Warning: Import order matters when using Lambda Layers or multiple modules"
**Do not set `auto_patch=False`** when reusing Tracer in Lambda Layers, or in multiple modules.
This can result in the first Tracer config being inherited by new instances, and their modules not being patched.
Tracer will automatically ignore imported modules that have been patched.
=== "tracer_reuse.py"
```python hl_lines="1 6"
--8<-- "examples/tracer/src/tracer_reuse.py"
```
=== "tracer_reuse_module.py"
A new instance of Tracer will be created but will reuse the previous Tracer instance configuration, similar to a Singleton.
```python hl_lines="3"
--8<-- "examples/tracer/src/tracer_reuse_module.py"
```
## Testing your code
Tracer is disabled by default when not running in the AWS Lambda environment, including AWS SAM CLI and Chalice environments. This means no code changes or environment variables to be set.
## Tips
* Use annotations on key operations to slice and dice traces, create unique views, and create metrics from it via Trace Groups
* Use a namespace when adding metadata to group data more easily
* Annotations and metadata are added to the current subsegment opened. If you want them in a specific subsegment, use a [context manager](https://github.com/aws/aws-xray-sdk-python/#start-a-custom-segmentsubsegment){target="_blank"} via the escape hatch mechanism
|