Skip to content

global_sdk_config.is_enabled() is ignored for async #170

Open
@IvDoorn

Description

@IvDoorn

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions