Skip to content

Commit 816ab26

Browse files
authored
Added the option to ignore missing segments (#338)
* Add `IGNORE_ERROR` option for `AWS_XRAY_CONTEXT_MISSING`
1 parent 8a82e4b commit 816ab26

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

CHANGELOG.rst

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
CHANGELOG
33
=========
44

5+
Unreleased
6+
==========
7+
* improvement: Added support for IGNORE_ERROR option when context is missing. `PR338 <https://github.com/aws/aws-xray-sdk-python/pull/338>`_.
8+
59
2.9.0
610
==========
711
* bugfix: Change logging behavior to avoid overflow. `PR302 <https://github.com/aws/aws-xray-sdk-python/pull/302>`_.

aws_xray_sdk/core/context.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
log = logging.getLogger(__name__)
1111

1212
MISSING_SEGMENT_MSG = 'cannot find the current segment/subsegment, please make sure you have a segment open'
13-
SUPPORTED_CONTEXT_MISSING = ('RUNTIME_ERROR', 'LOG_ERROR')
13+
SUPPORTED_CONTEXT_MISSING = ('RUNTIME_ERROR', 'LOG_ERROR', 'IGNORE_ERROR')
1414
CXT_MISSING_STRATEGY_KEY = 'AWS_XRAY_CONTEXT_MISSING'
1515

1616

@@ -121,7 +121,7 @@ def handle_context_missing(self):
121121
"""
122122
if self.context_missing == 'RUNTIME_ERROR':
123123
raise SegmentNotFoundException(MISSING_SEGMENT_MSG)
124-
else:
124+
elif self.context_missing == 'LOG_ERROR':
125125
log.error(MISSING_SEGMENT_MSG)
126126

127127
def _is_subsegment(self, entity):

aws_xray_sdk/core/recorder.py

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def configure(self, sampling=None, plugins=None,
112112
RUNTIME_ERROR means the recorder will raise an exception.
113113
LOG_ERROR means the recorder will only log the error and
114114
do nothing.
115+
IGNORE_ERROR means the recorder will do nothing
115116
:param str daemon_address: The X-Ray daemon address where the recorder
116117
sends data to.
117118
:param str service: default segment name if creating a segment without

docs/configurations.rst

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Supported strategies are:
9292

9393
* RUNTIME_ERROR: throw an SegmentNotFoundException
9494
* LOG_ERROR: log an error and continue
95+
* IGNORE_ERROR: do nothing
9596

9697
Segment Dynamic Naming
9798
----------------------

tests/ext/aiohttp/test_client.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import logging
2+
13
import pytest
24
from aiohttp import ClientSession
35

46
from aws_xray_sdk.core import xray_recorder
57
from aws_xray_sdk.core.async_context import AsyncContext
8+
from aws_xray_sdk.core.context import MISSING_SEGMENT_MSG
69
from aws_xray_sdk.core.exceptions.exceptions import SegmentNotFoundException
710
from aws_xray_sdk.ext.util import strip_url, get_hostname
811
from aws_xray_sdk.ext.aiohttp.client import aws_xray_trace_config
@@ -144,7 +147,8 @@ async def test_no_segment_raise(loop, recorder):
144147
pass
145148

146149

147-
async def test_no_segment_not_raise(loop, recorder):
150+
async def test_no_segment_log_error(loop, recorder, caplog):
151+
caplog.set_level(logging.ERROR)
148152
xray_recorder.configure(context_missing='LOG_ERROR')
149153
trace_config = aws_xray_trace_config()
150154
status_code = 200
@@ -155,3 +159,19 @@ async def test_no_segment_not_raise(loop, recorder):
155159

156160
# Just check that the request was done correctly
157161
assert status_received == status_code
162+
assert MISSING_SEGMENT_MSG in [rec.message for rec in caplog.records]
163+
164+
165+
async def test_no_segment_ignore_error(loop, recorder, caplog):
166+
caplog.set_level(logging.ERROR)
167+
xray_recorder.configure(context_missing='IGNORE_ERROR')
168+
trace_config = aws_xray_trace_config()
169+
status_code = 200
170+
url = 'http://{}/status/{}?foo=bar'.format(BASE_URL, status_code)
171+
async with ClientSession(loop=loop, trace_configs=[trace_config]) as session:
172+
async with session.get(url) as resp:
173+
status_received = resp.status
174+
175+
# Just check that the request was done correctly
176+
assert status_received == status_code
177+
assert MISSING_SEGMENT_MSG not in [rec.message for rec in caplog.records]

0 commit comments

Comments
 (0)