aws / aws/aws-xray-sdk-python

Document how to create new subsegments inside a ThreadPoolExecutor thread

Aperta
#341 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
documentation
Lingua principale
Python
Stelle
338
Fork
147
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
aws, python
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.