diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 56ab858c..d806a88b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,10 @@ CHANGELOG ========= +Unreleased +========== +* improvement: Added support for IGNORE_ERROR option when context is missing. `PR338 `_. + 2.9.0 ========== * bugfix: Change logging behavior to avoid overflow. `PR302 `_. diff --git a/aws_xray_sdk/core/context.py b/aws_xray_sdk/core/context.py index 6999b231..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') +SUPPORTED_CONTEXT_MISSING = ('RUNTIME_ERROR', 'LOG_ERROR', 'IGNORE_ERROR') 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..3169e3a2 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_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 diff --git a/docs/configurations.rst b/docs/configurations.rst index eca0f693..87306129 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_ERROR: do nothing Segment Dynamic Naming ---------------------- diff --git a/tests/ext/aiohttp/test_client.py b/tests/ext/aiohttp/test_client.py index 78448819..d4138d87 100644 --- a/tests/ext/aiohttp/test_client.py +++ b/tests/ext/aiohttp/test_client.py @@ -1,8 +1,11 @@ +import logging + import pytest from aiohttp import ClientSession 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 +147,8 @@ 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, caplog): + caplog.set_level(logging.ERROR) xray_recorder.configure(context_missing='LOG_ERROR') trace_config = aws_xray_trace_config() status_code = 200 @@ -155,3 +159,19 @@ async def test_no_segment_not_raise(loop, recorder): # Just check that the request was done correctly assert status_received == status_code + assert MISSING_SEGMENT_MSG in [rec.message for rec in caplog.records] + + +async def test_no_segment_ignore_error(loop, recorder, caplog): + caplog.set_level(logging.ERROR) + 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) + 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 + assert MISSING_SEGMENT_MSG not in [rec.message for rec in caplog.records]