aws / aws/aws-xray-sdk-python

global_sdk_config.is_enabled() is ignored for async

オープン
#170 コメント 3 件 リアクション 2 件 担当者 0 名 GitHub で見る
bug
主要言語
Python
スター
339
フォーク
147
PR マージ指標
30日以内にマージされた PR はありません

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

issue のスクリプトにある 4 つのケースを再現し、その後 aws_xray_sdk/core/async_recorder.py と、traceback に示されている recorder のエントリポイントを調べます。無効化されたパスを同期デコレータと比較し、無効化された非同期呼び出しが segment を探さずに完了すること、また有効化された呼び出しが既存の動作を維持することを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
observability-sre
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。