aws / aws/aws-xray-sdk-python

Document how to create new subsegments inside a ThreadPoolExecutor thread

Open
#341 1 comment 0 reactions 0 assignees View on GitHub
documentation
Dominant language
Python
Stars
338
Forks
147
PR merge metrics
No merged PRs in 30d

Description

The current [documentation](https://github.com/aws/aws-xray-sdk-python#trace-threadpoolexecutor) shows how to set the parent thread's X-ray entity in the worker thread:

```py
def load_url(url, trace_entity):
# Set the parent X-Ray entity for the worker thread.
xray_recorder.set_trace_entity(trace_entity)
# Subsegment captured from the following HTTP GET will be
# a child of parent entity passed from the main thread.
resp = requests.get(url)
# prevent thread pollution
xray_recorder.clear_trace_entities()
return resp
```

However it's not clear how this interacts with `capture`/`in_segment`. For example, I assume this isn't correct, as the capture starts before we call `set_trace_entity`:

```py
@xray_recorder.capture('subsegment_name')
def load_url(url, trace_entity):
# Set the parent X-Ray entity for the worker thread.
xray_recorder.set_trace_entity(trace_entity)
# Subsegment captured from the following HTTP GET will be
# a child of parent entity passed from the main thread.
resp = requests.get(url)
# prevent thread pollution
xray_recorder.clear_trace_entities()
return resp
```

However, are these two OK?

```py
def load_url(url, trace_entity):
# Set the parent X-Ray entity for the worker thread.
xray_recorder.set_trace_entity(trace_entity)
with xray_recorder.capture('subsegment_name'):
# Subsegment captured from the following HTTP GET will be
# a child of parent entity passed from the main thread.
resp = requests.get(url)
# prevent thread pollution
xray_recorder.clear_trace_entities()
return resp
```

```py
def load_url(url, trace_entity):
# Set the parent X-Ray entity for the worker thread.
xray_recorder.set_trace_entity(trace_entity)
with xray_recorder.in_segment('segment_name') as segment:
segment.put_metadata('key', dict, 'namespace')
# Subsegment captured from the following HTTP GET will be
# a child of parent entity passed from the main thread.
resp = requests.get(url)
# prevent thread pollution
xray_recorder.clear_trace_entities()
return resp
```

It would be great if the docs on threading had a more complete example.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.