Skip to content

Commit 06d58d4

Browse files
authored
Merge pull request #244 from awslabs/docs/capture_method_clarification
docs: add clarification to Tracer docs for `capture_method`
2 parents 3ac1ae3 + dedfc20 commit 06d58d4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
- **Docs**: Add clarification to Tracer docs for how `capture_method` decorator can cause function responses to be read and serialized.
89

910
## [1.9.0] - 2020-12-04
1011

docs/content/core/tracer.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def handler(event, context):
7171
You can disable Tracer from capturing their responses as tracing metadata with <strong><code>capture_response=False</code></strong> parameter in both capture_lambda_handler and capture_method decorators.
7272
</Note><br/>
7373

74+
7475
```python:title=do_not_capture_response_as_metadata.py
7576
# Disables Tracer from capturing response and adding as metadata
7677
# Useful when dealing with sensitive data
@@ -116,6 +117,14 @@ def handler(event, context):
116117

117118
You can trace a synchronous function using the `capture_method`.
118119

120+
<Note type="warning">
121+
<strong>When <code>capture_response</code> is enabled, the function response will be read and serialized as json.</strong>
122+
<br/><br/>
123+
The serialization is performed by the aws-xray-sdk which uses the <code>jsonpickle</code> module. This can cause
124+
unintended consequences if there are side effects to recursively reading the returned value, for example if the
125+
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"><code>StreamingBody</code></a> for S3 objects.
126+
</Note><br/>
127+
119128
```python:title=app.py
120129
@tracer.capture_method
121130
def collect_payment(charge_id):
@@ -126,6 +135,14 @@ def collect_payment(charge_id):
126135
@tracer.capture_method(capture_response=False) # highlight-line
127136
def sensitive_information_to_be_processed():
128137
return "sensitive_information"
138+
139+
# If we capture response, the s3_object["Body"].read() method will be called by x-ray-sdk when
140+
# trying to serialize the object. This will cause it to return empty next time it is called.
141+
@tracer.capture_method(capture_response=False) # highlight-line
142+
def get_s3_object(bucket_name, object_key):
143+
s3 = boto3.client("s3")
144+
s3_object = get_object(Bucket=bucket_name, Key=object_key)
145+
return s3_object
129146
```
130147

131148
## Asynchronous and generator functions

0 commit comments

Comments
 (0)