Skip to content

Modified context missing strategy default to log error #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aws_xray_sdk/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Context(object):

This data structure is thread-safe.
"""
def __init__(self, context_missing='RUNTIME_ERROR'):
def __init__(self, context_missing='LOG_ERROR'):

self._local = threading.local()
strategy = os.getenv(CXT_MISSING_STRATEGY_KEY, context_missing)
Expand Down
2 changes: 1 addition & 1 deletion aws_xray_sdk/ext/django/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
DEFAULTS = {
'AWS_XRAY_DAEMON_ADDRESS': '127.0.0.1:2000',
'AUTO_INSTRUMENT': True,
'AWS_XRAY_CONTEXT_MISSING': 'RUNTIME_ERROR',
'AWS_XRAY_CONTEXT_MISSING': 'LOG_ERROR',
'PLUGINS': (),
'SAMPLING': True,
'SAMPLING_RULES': None,
Expand Down
2 changes: 1 addition & 1 deletion docs/frameworks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The default values are as follows::
XRAY_RECORDER = {
'AWS_XRAY_DAEMON_ADDRESS': '127.0.0.1:2000',
'AUTO_INSTRUMENT': True, # If turned on built-in database queries and template rendering will be recorded as subsegments
'AWS_XRAY_CONTEXT_MISSING': 'RUNTIME_ERROR',
'AWS_XRAY_CONTEXT_MISSING': 'LOG_ERROR',
'PLUGINS': (),
'SAMPLING': True,
'SAMPLING_RULES': None,
Expand Down
3 changes: 1 addition & 2 deletions tests/ext/django/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
@pytest.fixture(scope='module', autouse=True)
def setup():
django.setup()
xray_recorder.configure(context=Context(),
context_missing='LOG_ERROR')
xray_recorder.configure(context=Context())
patch_db()


Expand Down
3 changes: 1 addition & 2 deletions tests/ext/django/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class XRayTestCase(TestCase):

def setUp(self):
django.setup()
xray_recorder.configure(context=Context(),
context_missing='LOG_ERROR')
xray_recorder.configure(context=Context())
xray_recorder.clear_trace_entities()
global_sdk_config.set_sdk_enabled(True)

Expand Down
16 changes: 12 additions & 4 deletions tests/test_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from aws_xray_sdk.core.models.segment import Segment
from aws_xray_sdk.core.models.subsegment import Subsegment
from aws_xray_sdk.core.models.dummy_entities import DummySegment, DummySubsegment
from aws_xray_sdk.core.exceptions.exceptions import SegmentNotFoundException

xray_recorder = get_new_stubbed_recorder()

Expand Down Expand Up @@ -99,19 +100,26 @@ def test_put_annotation_metadata():
assert not subsegment.metadata['default'].get('key1')


def test_pass_through_with_missing_context():
def test_default_pass_through_with_missing_context():
xray_recorder = get_new_stubbed_recorder()
xray_recorder.configure(sampling=False, context_missing='LOG_ERROR')
xray_recorder.configure(sampling=False) # default context_missing = 'LOG_ERROR'
assert not xray_recorder.is_sampled()

xray_recorder.put_annotation('key', 'value')
xray_recorder.put_metadata('key', 'value')
xray_recorder.end_segment()

def test_raise_runtime_error_with_missing_context():
xray_recorder = get_new_stubbed_recorder()
xray_recorder.configure(sampling=False, context_missing='RUNTIME_ERROR')

with pytest.raises(SegmentNotFoundException):
Copy link
Contributor

@atshaw43 atshaw43 Dec 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too familiar with Python. If there is not an exception throw, as expected, then will this test fail?

Or is the 'with' keyword more of a 'if this exception is thrown then do X'. And this test will just pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will! This is what happens when you modify the context_missing parameter to be LOG_ERROR in this test:
Failed: DID NOT RAISE <class 'aws_xray_sdk.core.exceptions.exceptions.SegmentNotFoundException'>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding to Carol's comment above, this is a common way in pytest to assert that a certain exception is thrown in a unit test. Documentation for reference: https://docs.pytest.org/en/7.1.x/how-to/assert.html#assertions-about-expected-exceptions

assert not xray_recorder.is_sampled()
xray_recorder.end_segment()

def test_capture_not_suppress_exception():
xray_recorder = get_new_stubbed_recorder()
xray_recorder.configure(sampling=False, context_missing='LOG_ERROR')
xray_recorder.configure(sampling=False)

@xray_recorder.capture()
def buggy_func():
Expand All @@ -123,7 +131,7 @@ def buggy_func():

def test_capture_not_swallow_return():
xray_recorder = get_new_stubbed_recorder()
xray_recorder.configure(sampling=False, context_missing='LOG_ERROR')
xray_recorder.configure(sampling=False)
value = 1

@xray_recorder.capture()
Expand Down