From 59711bebf84e5755481523d19be3717ec30fe30a Mon Sep 17 00:00:00 2001 From: Stijn De Haes Date: Tue, 24 May 2022 15:45:06 +0200 Subject: [PATCH 1/6] Added the option to ignore missing segments --- aws_xray_sdk/core/context.py | 4 ++-- aws_xray_sdk/core/recorder.py | 1 + docs/configurations.rst | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/aws_xray_sdk/core/context.py b/aws_xray_sdk/core/context.py index 6999b231..d613f5f4 100644 --- a/aws_xray_sdk/core/context.py +++ b/aws_xray_sdk/core/context.py @@ -10,7 +10,7 @@ log = logging.getLogger(__name__) MISSING_SEGMENT_MSG = 'cannot find the current segment/subsegment, please make sure you have a segment open' -SUPPORTED_CONTEXT_MISSING = ('RUNTIME_ERROR', 'LOG_ERROR') +SUPPORTED_CONTEXT_MISSING = ('RUNTIME_ERROR', 'LOG_ERROR', 'IGNORE') CXT_MISSING_STRATEGY_KEY = 'AWS_XRAY_CONTEXT_MISSING' @@ -121,7 +121,7 @@ def handle_context_missing(self): """ if self.context_missing == 'RUNTIME_ERROR': raise SegmentNotFoundException(MISSING_SEGMENT_MSG) - else: + elif self.context_missing == 'LOG_ERROR': log.error(MISSING_SEGMENT_MSG) def _is_subsegment(self, entity): diff --git a/aws_xray_sdk/core/recorder.py b/aws_xray_sdk/core/recorder.py index 7f223e24..51550c94 100644 --- a/aws_xray_sdk/core/recorder.py +++ b/aws_xray_sdk/core/recorder.py @@ -112,6 +112,7 @@ def configure(self, sampling=None, plugins=None, RUNTIME_ERROR means the recorder will raise an exception. LOG_ERROR means the recorder will only log the error and do nothing. + IGNORE means the recorder will do nothing :param str daemon_address: The X-Ray daemon address where the recorder sends data to. :param str service: default segment name if creating a segment without diff --git a/docs/configurations.rst b/docs/configurations.rst index eca0f693..7758985d 100644 --- a/docs/configurations.rst +++ b/docs/configurations.rst @@ -92,6 +92,7 @@ Supported strategies are: * RUNTIME_ERROR: throw an SegmentNotFoundException * LOG_ERROR: log an error and continue +* IGNORE: will do nothing Segment Dynamic Naming ---------------------- From 57dbc36e8f4531d6b6e90873bef00461d594bcf0 Mon Sep 17 00:00:00 2001 From: Stijn De Haes Date: Thu, 26 May 2022 10:49:04 +0200 Subject: [PATCH 2/6] Added test for segment ignore --- docs/configurations.rst | 2 +- tests/ext/aiohttp/test_client.py | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/configurations.rst b/docs/configurations.rst index 7758985d..fdc7b042 100644 --- a/docs/configurations.rst +++ b/docs/configurations.rst @@ -92,7 +92,7 @@ Supported strategies are: * RUNTIME_ERROR: throw an SegmentNotFoundException * LOG_ERROR: log an error and continue -* IGNORE: will do nothing +* IGNORE: do nothing Segment Dynamic Naming ---------------------- diff --git a/tests/ext/aiohttp/test_client.py b/tests/ext/aiohttp/test_client.py index 78448819..d3d7c1cf 100644 --- a/tests/ext/aiohttp/test_client.py +++ b/tests/ext/aiohttp/test_client.py @@ -3,6 +3,7 @@ from aws_xray_sdk.core import xray_recorder from aws_xray_sdk.core.async_context import AsyncContext +from aws_xray_sdk.core.context import MISSING_SEGMENT_MSG from aws_xray_sdk.core.exceptions.exceptions import SegmentNotFoundException from aws_xray_sdk.ext.util import strip_url, get_hostname from aws_xray_sdk.ext.aiohttp.client import aws_xray_trace_config @@ -144,7 +145,7 @@ async def test_no_segment_raise(loop, recorder): pass -async def test_no_segment_not_raise(loop, recorder): +async def test_no_segment_log_error(loop, recorder, capsys): xray_recorder.configure(context_missing='LOG_ERROR') trace_config = aws_xray_trace_config() status_code = 200 @@ -155,3 +156,20 @@ async def test_no_segment_not_raise(loop, recorder): # Just check that the request was done correctly assert status_received == status_code + captured = capsys.readouterr() + assert MISSING_SEGMENT_MSG in captured.out + + +async def test_no_segment_ignore(loop, recorder, capsys): + xray_recorder.configure(context_missing='IGNORE') + trace_config = aws_xray_trace_config() + status_code = 200 + url = 'http://{}/status/{}?foo=bar'.format(BASE_URL, status_code) + async with ClientSession(loop=loop, trace_configs=[trace_config]) as session: + async with session.get(url) as resp: + status_received = resp.status + + # Just check that the request was done correctly + assert status_received == status_code + captured = capsys.readouterr() + assert MISSING_SEGMENT_MSG not in captured.out From 07f5b321395611076c572d9ac3a471382a23eaa5 Mon Sep 17 00:00:00 2001 From: Stijn De Haes Date: Thu, 26 May 2022 11:22:35 +0200 Subject: [PATCH 3/6] Use caplog to capture test records --- tests/ext/aiohttp/test_client.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/ext/aiohttp/test_client.py b/tests/ext/aiohttp/test_client.py index d3d7c1cf..fc1f1189 100644 --- a/tests/ext/aiohttp/test_client.py +++ b/tests/ext/aiohttp/test_client.py @@ -1,3 +1,5 @@ +import logging + import pytest from aiohttp import ClientSession @@ -145,7 +147,8 @@ async def test_no_segment_raise(loop, recorder): pass -async def test_no_segment_log_error(loop, recorder, capsys): +async def test_no_segment_log_error(loop, recorder, caplog): + caplog.set_level(logging.ERROR) xray_recorder.configure(context_missing='LOG_ERROR') trace_config = aws_xray_trace_config() status_code = 200 @@ -156,11 +159,11 @@ async def test_no_segment_log_error(loop, recorder, capsys): # Just check that the request was done correctly assert status_received == status_code - captured = capsys.readouterr() - assert MISSING_SEGMENT_MSG in captured.out + assert MISSING_SEGMENT_MSG in [rec.message for rec in caplog.records] -async def test_no_segment_ignore(loop, recorder, capsys): +async def test_no_segment_ignore(loop, recorder, caplog): + caplog.set_level(logging.ERROR) xray_recorder.configure(context_missing='IGNORE') trace_config = aws_xray_trace_config() status_code = 200 @@ -171,5 +174,4 @@ async def test_no_segment_ignore(loop, recorder, capsys): # Just check that the request was done correctly assert status_received == status_code - captured = capsys.readouterr() - assert MISSING_SEGMENT_MSG not in captured.out + assert MISSING_SEGMENT_MSG not in [rec.message for rec in caplog.records] From a715d858e29fc686b70e8107f62bf035aeb7f670 Mon Sep 17 00:00:00 2001 From: Stijn De Haes Date: Mon, 30 May 2022 11:58:56 +0200 Subject: [PATCH 4/6] Added release notes --- CHANGELOG.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 56ab858c..c41e40e8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,10 @@ CHANGELOG ========= +Unreleased +========== +* improvement: Added support for IGNORE option when context is missing. `PR338 `_. + 2.9.0 ========== * bugfix: Change logging behavior to avoid overflow. `PR302 `_. From 52e5b7fc16eb806e84a9eedb170b173ec80bc1fd Mon Sep 17 00:00:00 2001 From: Stijn De Haes Date: Tue, 31 May 2022 10:32:53 +0200 Subject: [PATCH 5/6] Change IGNORE to IGNORE_ERROR This is the same naming used in other SDK's --- CHANGELOG.rst | 2 +- aws_xray_sdk/core/context.py | 2 +- docs/configurations.rst | 2 +- tests/ext/aiohttp/test_client.py | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c41e40e8..d806a88b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,7 +4,7 @@ CHANGELOG Unreleased ========== -* improvement: Added support for IGNORE option when context is missing. `PR338 `_. +* improvement: Added support for IGNORE_ERROR option when context is missing. `PR338 `_. 2.9.0 ========== diff --git a/aws_xray_sdk/core/context.py b/aws_xray_sdk/core/context.py index d613f5f4..64b1e729 100644 --- a/aws_xray_sdk/core/context.py +++ b/aws_xray_sdk/core/context.py @@ -10,7 +10,7 @@ log = logging.getLogger(__name__) MISSING_SEGMENT_MSG = 'cannot find the current segment/subsegment, please make sure you have a segment open' -SUPPORTED_CONTEXT_MISSING = ('RUNTIME_ERROR', 'LOG_ERROR', 'IGNORE') +SUPPORTED_CONTEXT_MISSING = ('RUNTIME_ERROR', 'LOG_ERROR', 'IGNORE_ERROR') CXT_MISSING_STRATEGY_KEY = 'AWS_XRAY_CONTEXT_MISSING' diff --git a/docs/configurations.rst b/docs/configurations.rst index fdc7b042..87306129 100644 --- a/docs/configurations.rst +++ b/docs/configurations.rst @@ -92,7 +92,7 @@ Supported strategies are: * RUNTIME_ERROR: throw an SegmentNotFoundException * LOG_ERROR: log an error and continue -* IGNORE: do nothing +* IGNORE_ERROR: do nothing Segment Dynamic Naming ---------------------- diff --git a/tests/ext/aiohttp/test_client.py b/tests/ext/aiohttp/test_client.py index fc1f1189..d4138d87 100644 --- a/tests/ext/aiohttp/test_client.py +++ b/tests/ext/aiohttp/test_client.py @@ -162,9 +162,9 @@ async def test_no_segment_log_error(loop, recorder, caplog): assert MISSING_SEGMENT_MSG in [rec.message for rec in caplog.records] -async def test_no_segment_ignore(loop, recorder, caplog): +async def test_no_segment_ignore_error(loop, recorder, caplog): caplog.set_level(logging.ERROR) - xray_recorder.configure(context_missing='IGNORE') + xray_recorder.configure(context_missing='IGNORE_ERROR') trace_config = aws_xray_trace_config() status_code = 200 url = 'http://{}/status/{}?foo=bar'.format(BASE_URL, status_code) From 5bedc7dd66f8c073144c54c0a4eee9a7d84aaab0 Mon Sep 17 00:00:00 2001 From: Stijn De Haes Date: Tue, 31 May 2022 10:33:42 +0200 Subject: [PATCH 6/6] Change IGNORE to IGNORE_ERROR This is the same naming used in other SDK's --- aws_xray_sdk/core/recorder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws_xray_sdk/core/recorder.py b/aws_xray_sdk/core/recorder.py index 51550c94..3169e3a2 100644 --- a/aws_xray_sdk/core/recorder.py +++ b/aws_xray_sdk/core/recorder.py @@ -112,7 +112,7 @@ def configure(self, sampling=None, plugins=None, RUNTIME_ERROR means the recorder will raise an exception. LOG_ERROR means the recorder will only log the error and do nothing. - IGNORE means the recorder will do nothing + IGNORE_ERROR means the recorder will do nothing :param str daemon_address: The X-Ray daemon address where the recorder sends data to. :param str service: default segment name if creating a segment without