Skip to content

Commit 680c646

Browse files
authored
Adding oversampling mitigation example to ReadMe (#368)
* added oversampling mitigation example to ReadMe * update logged message * added code snippet to wrap downstream SDK calls with unsampled subsegment * changed wording for oversampling mitigation APIs + addressed feedback
1 parent b37ae11 commit 680c646

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,46 @@ xray_recorder.end_subsegment()
124124
xray_recorder.end_segment()
125125
```
126126

127+
### Oversampling Mitigation
128+
To modify the sampling decision at the subsegment level, subsegments that inherit the decision of their direct parent (segment or subsegment) can be created using `xray_recorder.begin_subsegment()` and unsampled subsegments can be created using
129+
`xray_recorder.begin_subsegment_without_sampling()`.
130+
131+
The code snippet below demonstrates creating a sampled or unsampled subsegment based on the sampling decision of each SQS message processed by Lambda.
132+
133+
```python
134+
from aws_xray_sdk.core import xray_recorder
135+
from aws_xray_sdk.core.models.subsegment import Subsegment
136+
from aws_xray_sdk.core.utils.sqs_message_helper import SqsMessageHelper
137+
138+
def lambda_handler(event, context):
139+
140+
for message in event['Records']:
141+
if SqsMessageHelper.isSampled(message):
142+
subsegment = xray_recorder.begin_subsegment('sampled_subsegment')
143+
print('sampled - processing SQS message')
144+
145+
else:
146+
subsegment = xray_recorder.begin_subsegment_without_sampling('unsampled_subsegment')
147+
print('unsampled - processing SQS message')
148+
149+
xray_recorder.end_subsegment()
150+
```
151+
152+
The code snippet below demonstrates wrapping a downstream AWS SDK request with an unsampled subsegment.
153+
```python
154+
from aws_xray_sdk.core import xray_recorder, patch_all
155+
import boto3
156+
157+
patch_all()
158+
159+
def lambda_handler(event, context):
160+
subsegment = xray_recorder.begin_subsegment_without_sampling('unsampled_subsegment')
161+
client = boto3.client('sqs')
162+
print(client.list_queues())
163+
164+
xray_recorder.end_subsegment()
165+
```
166+
127167
### Capture
128168

129169
As a decorator:

0 commit comments

Comments
 (0)