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
@@ -20,43 +20,36 @@ Tracer is an opinionated thin wrapper for [AWS X-Ray Python SDK](https://github.
20
20
21
21
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) to send traces to AWS X-Ray.
22
22
23
-
???+ example
24
-
**AWS Serverless Application Model (SAM)**
25
-
26
-
=== "template.yml"
27
-
28
-
```yaml hl_lines="6 9"
29
-
Resources:
30
-
HelloWorldFunction:
31
-
Type: AWS::Serverless::Function
32
-
Properties:
33
-
Runtime: python3.8
34
-
Tracing: Active
35
-
Environment:
36
-
Variables:
37
-
POWERTOOLS_SERVICE_NAME: example
38
-
```
23
+
```yaml hl_lines="6 9" title="AWS Serverless Application Model (SAM) example"
24
+
Resources:
25
+
HelloWorldFunction:
26
+
Type: AWS::Serverless::Function
27
+
Properties:
28
+
Runtime: python3.8
29
+
Tracing: Active
30
+
Environment:
31
+
Variables:
32
+
POWERTOOLS_SERVICE_NAME: example
33
+
```
39
34
40
35
### Lambda handler
41
36
42
-
You can quickly start by importing the `Tracer` class, initialize it outside the Lambda handler, and use `capture_lambda_handler` decorator.
43
-
44
-
=== "app.py"
37
+
You can quickly start by initializing `Tracer` and use `capture_lambda_handler` decorator for your Lambda handler.
45
38
46
-
```python hl_lines="1 3 6"
47
-
from aws_lambda_powertools import Tracer
39
+
```python hl_lines="1 3 6" title="Tracing Lambda handler with capture_lambda_handler"
40
+
from aws_lambda_powertools import Tracer
48
41
49
-
tracer = Tracer() # Sets service via env var
50
-
# OR tracer = Tracer(service="example")
42
+
tracer = Tracer() # Sets service via env var
43
+
# OR tracer = Tracer(service="example")
51
44
52
-
@tracer.capture_lambda_handler
53
-
def handler(event, context):
54
-
charge_id = event.get('charge_id')
55
-
payment = collect_payment(charge_id)
56
-
...
57
-
```
45
+
@tracer.capture_lambda_handler
46
+
defhandler(event, context):
47
+
charge_id = event.get('charge_id')
48
+
payment = collect_payment(charge_id)
49
+
...
50
+
```
58
51
59
-
When using this `capture_lambda_handler` decorator, Tracer performs these additional tasks to ease operations:
52
+
`capture_lambda_handler` performs these additional tasks to ease operations:
60
53
61
54
* Creates a `ColdStart` annotation to easily filter traces that have had an initialization overhead
62
55
* Creates a `Service` annotation if `service` parameter or `POWERTOOLS_SERVICE_NAME` is set
@@ -66,51 +59,48 @@ When using this `capture_lambda_handler` decorator, Tracer performs these additi
66
59
67
60
**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/) to slice and dice your transactions.
68
61
69
-
**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.
70
-
71
-
=== "Annotations"
72
-
You can add annotations using `put_annotation` method.
62
+
```python hl_lines="7" title="Adding annotations with put_annotation method"
**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.
85
73
86
-
```python hl_lines="8"
87
-
from aws_lambda_powertools import Tracer
88
-
tracer = Tracer()
74
+
```python hl_lines="8" title="Adding arbitrary metadata with put_metadata method"
The serialization is performed by the aws-xray-sdk which uses the `jsonpickle` module. This can cause
104
-
unintended consequences if there are side effects to recursively reading the returned value, for example if the
105
-
decorated function response contains a file-like object or a <a href="https://botocore.amazonaws.com/v1/documentation/api/latest/reference/response.html#botocore.response.StreamingBody">`StreamingBody`</a> for S3 objects.
97
+
???+ note "Note: Function responses are auto-captured and stored as JSON, by default."
98
+
99
+
Use [capture_response](#disabling-response-auto-capture) parameter to override this behaviour.
100
+
101
+
The serialization is performed by aws-xray-sdk via `jsonpickle` module. This can cause
102
+
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.
@@ -164,21 +154,6 @@ You can trace asynchronous functions and generator functions (including context
164
154
...
165
155
```
166
156
167
-
The decorator will detect whether your function is asynchronous, a generator, or a context manager and adapt its behaviour accordingly.
168
-
169
-
=== "app.py"
170
-
171
-
```python
172
-
@tracer.capture_lambda_handler
173
-
def handler(evt, ctx):
174
-
asyncio.run(collect_payment())
175
-
176
-
with collect_payment_ctxman as result:
177
-
do_something_with(result)
178
-
179
-
another_result = list(collect_payment_gen())
180
-
```
181
-
182
157
## Advanced
183
158
184
159
### Patching modules
@@ -187,17 +162,15 @@ Tracer automatically patches all [supported libraries by X-Ray](https://docs.aws
187
162
188
163
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:
189
164
190
-
=== "app.py"
191
-
192
-
```python hl_lines="7"
193
-
import boto3
194
-
import requests
165
+
```python hl_lines="7" title="Example of explicitly patching boto3 and requests only"
raiseValueError("some sensitive info in the stack trace...")
222
+
```
252
223
253
224
### Tracing aiohttp requests
254
225
@@ -257,43 +228,39 @@ Use **`capture_error=False`** parameter in both `capture_lambda_handler` and `ca
257
228
258
229
You can use `aiohttp_trace_config` function to create a valid [aiohttp trace_config object](https://docs.aiohttp.org/en/stable/tracing_reference.html). This is necessary since X-Ray utilizes aiohttp trace hooks to capture requests end-to-end.
from aws_lambda_powertools.tracing import aiohttp_trace_config
235
+
from aws_lambda_powertools import Tracer
236
+
from aws_lambda_powertools.tracing import aiohttp_trace_config
268
237
269
-
tracer = Tracer()
238
+
tracer = Tracer()
270
239
271
-
async def aiohttp_task():
272
-
async with aiohttp.ClientSession(trace_configs=[aiohttp_trace_config()]) as session:
273
-
async with session.get("https://httpbin.org/json") as resp:
274
-
resp = await resp.json()
275
-
return resp
276
-
```
240
+
asyncdefaiohttp_task():
241
+
asyncwith aiohttp.ClientSession(trace_configs=[aiohttp_trace_config()]) as session:
242
+
asyncwith session.get("https://httpbin.org/json") as resp:
243
+
resp =await resp.json()
244
+
return resp
245
+
```
277
246
278
247
### Escape hatch mechanism
279
248
280
249
You can use `tracer.provider` attribute to access all methods provided by AWS X-Ray `xray_recorder` object.
281
250
282
251
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), or [context managers](https://github.com/aws/aws-xray-sdk-python/#user-content-start-a-custom-segmentsubsegment).
283
252
284
-
=== "escape_hatch_context_manager_example.py"
285
-
286
-
```python hl_lines="7"
287
-
from aws_lambda_powertools import Tracer
253
+
```python hl_lines="7" title="Tracing a code block with in_subsegment escape hatch"
254
+
from aws_lambda_powertools import Tracer
288
255
289
-
tracer = Tracer()
256
+
tracer = Tracer()
290
257
291
-
@tracer.capture_lambda_handler
292
-
def handler(event, context):
293
-
with tracer.provider.in_subsegment('## custom subsegment') as subsegment:
294
-
ret = some_work()
295
-
subsegment.put_metadata('response', ret)
296
-
```
258
+
@tracer.capture_lambda_handler
259
+
defhandler(event, context):
260
+
with tracer.provider.in_subsegment('## custom subsegment') as subsegment:
261
+
ret = some_work()
262
+
subsegment.put_metadata('response', ret)
263
+
```
297
264
298
265
### Concurrent asynchronous functions
299
266
@@ -302,28 +269,26 @@ This is useful when you need a feature available in X-Ray that is not available
302
269
303
270
A safe workaround mechanism is to use `in_subsegment_async` available via Tracer escape hatch (`tracer.provider`).
0 commit comments