aws / aws/aws-xray-sdk-python

global_sdk_config.is_enabled() is ignored for async

Aperta
#170 3 commenti 2 reazioni 0 assegnatari Vedi su GitHub
bug
Lingua principale
Python
Stelle
338
Fork
147
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

When disabling the XRay API using global_sdk_config. I'm still getting errors when using the Async pattern.

I've created the following script to demonstrate the bug:

```
import asyncio
import sys

from aws_xray_sdk import global_sdk_config
from aws_xray_sdk.core import xray_recorder

# Enable SDK based on argument
if 'enable' in sys.argv:
print('Enabling XRay')
global_sdk_config.set_sdk_enabled(True)
else:
print('Disbling XRay')
global_sdk_config.set_sdk_enabled(False)

@xray_recorder.capture('normal')
def test_normal():
print('IN NORMAL')

@xray_recorder.capture_async('async')
async def test_async():
print('IN ASYNC')

if 'normal' in sys.argv:
print('EXECUTE NORMAL')
test_normal()

if 'async' in sys.argv:
print('EXECUTE ASYNC')
asyncio.get_event_loop().run_until_complete(test_async())
```

### [PASS] Enable XRay, execute sync call
If I call this script:
```
python test.py enable normal
```
The output is:
```
Enabling XRay
EXECUTE NORMAL
cannot find the current segment/subsegment, please make sure you have a segment open
Traceback (most recent call last):
File "test.py", line 27, in
test_normal()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/models/subsegment.py", line 58, in __call__
meta_processor=None,
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 417, in record_subsegment
subsegment = self.begin_subsegment(name, namespace)
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 284, in begin_subsegment
segment = self.current_segment()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 267, in current_segment
entity = self.get_trace_entity()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 369, in get_trace_entity
return self.context.get_trace_entity()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/context.py", line 93, in get_trace_entity
return self.handle_context_missing()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/context.py", line 118, in handle_context_missing
raise SegmentNotFoundException(MISSING_SEGMENT_MSG)
aws_xray_sdk.core.exceptions.exceptions.SegmentNotFoundException: cannot find the current segment/subsegment, please make sure you have a segment open
```
Which is what I would expect. I never created a segment, so it is perfectly correct to raise exception.

### [PASS] Disable XRay, execute sync call
When I call the script again:
```
python test.py disable normal
```
The output is:
```
Disbling XRay
EXECUTE NORMAL
cannot find the current segment/subsegment, please make sure you have a segment open
IN NORMAL
```

Again correct. (I'm not entirely sure if the "cannot find the current segment/subsegment, please make sure you have a segment open" message is justified, as I've disabled the SDK, I actually am not expecting it to report me anything. But I'll let that one slide for this report.

### [PASS] Enable XRay, execute async call
I execute the script:
```
python test.py enable async
```
And output:
```
Enabling XRay
EXECUTE ASYNC
cannot find the current segment/subsegment, please make sure you have a segment open
Traceback (most recent call last):
File "test.py", line 31, in
asyncio.get_event_loop().run_until_complete(test_async())
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete
return future.result()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/async_recorder.py", line 33, in __call__
meta_processor=None,
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/async_recorder.py", line 75, in record_subsegment_async
subsegment = self.begin_subsegment(name, namespace)
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 284, in begin_subsegment
segment = self.current_segment()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 267, in current_segment
entity = self.get_trace_entity()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 369, in get_trace_entity
return self.context.get_trace_entity()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/context.py", line 93, in get_trace_entity
return self.handle_context_missing()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/context.py", line 118, in handle_context_missing
raise SegmentNotFoundException(MISSING_SEGMENT_MSG)
aws_xray_sdk.core.exceptions.exceptions.SegmentNotFoundException: cannot find the current segment/subsegment, please make sure you have a segment open
```

Just like the synchronous call. I was expecting this.

### [FAIL] Disable XRay, execute async call
I execute the script again:
```
python test.py disable async
```

And now the fun begins:
```
Disbling XRay
EXECUTE ASYNC
cannot find the current segment/subsegment, please make sure you have a segment open
Traceback (most recent call last):
File "test.py", line 31, in
asyncio.get_event_loop().run_until_complete(test_async())
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete
return future.result()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/async_recorder.py", line 33, in __call__
meta_processor=None,
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/async_recorder.py", line 75, in record_subsegment_async
subsegment = self.begin_subsegment(name, namespace)
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 284, in begin_subsegment
segment = self.current_segment()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 267, in current_segment
entity = self.get_trace_entity()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/recorder.py", line 369, in get_trace_entity
return self.context.get_trace_entity()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/context.py", line 93, in get_trace_entity
return self.handle_context_missing()
File "/home/idoorn/anaconda2/envs/bazelenv36/lib/python3.6/site-packages/aws_xray_sdk/core/context.py", line 118, in handle_context_missing
raise SegmentNotFoundException(MISSING_SEGMENT_MSG)
aws_xray_sdk.core.exceptions.exceptions.SegmentNotFoundException: cannot find the current segment/subsegment, please make sure you have a segment open
````

And it fails.. The XRay SDK is disabled, but still it complains that I have to open the segment.
I guess somewhere there is a check missing for the global_sdk_config.is_enabled() state.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Riproduci i quattro casi dello script dell’issue, quindi esamina aws_xray_sdk/core/async_recorder.py e gli entry point del recorder mostrati nel traceback. Confronta il percorso disabilitato con il decoratore sincrono e verifica che le chiamate asincrone disabilitate completino l’esecuzione senza cercare un segmento, mentre le chiamate abilitate mantengano il comportamento esistente.

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

Valutazione

Stack tecnologico
python
Ambito
observability-sre
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.