Skip to content
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

Added the option to ignore missing segments #338

Merged
merged 6 commits into from
May 31, 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
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
CHANGELOG
=========

Unreleased
==========
* improvement: Added support for IGNORE_ERROR option when context is missing. `PR338 <https://github.com/aws/aws-xray-sdk-python/pull/338>`_.

2.9.0
==========
* bugfix: Change logging behavior to avoid overflow. `PR302 <https://github.com/aws/aws-xray-sdk-python/pull/302>`_.
Expand Down
4 changes: 2 additions & 2 deletions aws_xray_sdk/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'


Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions aws_xray_sdk/core/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/configurations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------------
Expand Down
22 changes: 21 additions & 1 deletion tests/ext/aiohttp/test_client.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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]