aws / aws-xray-sdk-python

AWS X-Ray SDK for the Python programming language
Apache License 2.0
324 stars 142 forks source link

global_sdk_config.is_enabled() is ignored for async #170

Open IvDoorn opened 4 years ago

IvDoorn commented 4 years ago

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 <module>
    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 <module>
    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 <module>
    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.

IvDoorn commented 4 years ago

Additionally, if I tweak the script to:

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)

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

async def test_async():
    with xray_recorder.capture_async('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())

Note that I replaced the: @xray_recorder.capture{_async} sections with: with xray_recorder.capture{_async} In this case, all 4 variants of calling the function are failing with the same error. So even for the normal synchronous method the Python doesn't adhere to the fact that the SDK is disabled.

awssandra commented 4 years ago

Hi IvDoorn,

Thanks for the detailed bug report. I think it's safe to assume we're missing a disabled check (and a test along with it). We'll dive into this and verify as soon as we can. We are also happy to take PRs from the community.

Thanks, Sandra

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs in next 7 days. Thank you for your contributions.