Document how to create new subsegments inside a ThreadPoolExecutor thread
- Vorherrschende Sprache
- Python
- Sterne
- 338
- Forks
- 147
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
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.
Beitragsleitfaden
Rechercherichtung
Start with the threading documentation linked in the issue and review the capture and in_segment entry points after set_trace_entity. Clarify the supported pattern for creating subsegments in worker threads, including cleanup, and update the example so a reader can distinguish the valid approaches.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- aws, python
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100