From 6de271464c76952934cd34c6bbbfc5df3cd5754f Mon Sep 17 00:00:00 2001 From: Jeffery Saeteurn Date: Tue, 15 Jan 2019 16:24:30 -0800 Subject: [PATCH 1/5] Add ability to enable/disable the SDK (#26) * Disabling the SDK does the following: + Ext module patch methods no longer patch. + Any call to BeginSegment() automatically generates a DummySegment instead and will not be sent to the Daemon, and consequently, since DummySegments are generated, any generated subsegments automatically become DummySubsegments. + For Lambda, when Subsegments are created, they automatically are converted to DummySubsegments * Disabling/Enabling is done through the configure() method of the recorder. It may also be set through an environment variable "AWS_XRAY_ENABLED". --- aws_xray_sdk/core/lambda_launcher.py | 8 ++- aws_xray_sdk/core/patcher.py | 4 ++ aws_xray_sdk/core/recorder.py | 54 ++++++++++++++++++-- aws_xray_sdk/sdk_config.py | 35 +++++++++++++ tests/ext/aiohttp/test_middleware.py | 18 +++++++ tests/ext/django/test_middleware.py | 7 +++ tests/ext/flask/test_flask.py | 8 +++ tests/test_lambda_context.py | 10 ++++ tests/test_patcher.py | 9 ++++ tests/test_recorder.py | 49 ++++++++++++++++++ tests/test_sdk_config.py | 76 ++++++++++++++++++++++++++++ 11 files changed, 272 insertions(+), 6 deletions(-) create mode 100644 aws_xray_sdk/sdk_config.py create mode 100644 tests/test_sdk_config.py diff --git a/aws_xray_sdk/core/lambda_launcher.py b/aws_xray_sdk/core/lambda_launcher.py index 0b0558be..c2b59387 100644 --- a/aws_xray_sdk/core/lambda_launcher.py +++ b/aws_xray_sdk/core/lambda_launcher.py @@ -2,11 +2,12 @@ import logging import threading +from aws_xray_sdk.sdk_config import SDKConfig from .models.facade_segment import FacadeSegment from .models.trace_header import TraceHeader +from .models.dummy_entities import DummySubsegment from .context import Context - log = logging.getLogger(__name__) @@ -74,6 +75,11 @@ def put_subsegment(self, subsegment): log.warning("Subsegment %s discarded due to Lambda worker still initializing" % subsegment.name) return + enabled = SDKConfig.sdk_enabled() + if not enabled: + # For lambda, if the SDK is not enabled, we force the subsegment to be a dummy segment. + subsegment = DummySubsegment(current_entity) + current_entity.add_subsegment(subsegment) self._local.entities.append(subsegment) diff --git a/aws_xray_sdk/core/patcher.py b/aws_xray_sdk/core/patcher.py index b990cf6b..a9fae76a 100644 --- a/aws_xray_sdk/core/patcher.py +++ b/aws_xray_sdk/core/patcher.py @@ -7,6 +7,7 @@ import sys import wrapt +from aws_xray_sdk.sdk_config import SDKConfig from .utils.compat import PY2, is_classmethod, is_instance_method log = logging.getLogger(__name__) @@ -60,6 +61,9 @@ def _is_valid_import(module): def patch(modules_to_patch, raise_errors=True, ignore_module_patterns=None): + enabled = SDKConfig.sdk_enabled() + if not enabled: + return # Disable module patching if the SDK is disabled. modules = set() for module_to_patch in modules_to_patch: # boto3 depends on botocore and patching botocore is sufficient diff --git a/aws_xray_sdk/core/recorder.py b/aws_xray_sdk/core/recorder.py index 4662d206..e2c7efd4 100644 --- a/aws_xray_sdk/core/recorder.py +++ b/aws_xray_sdk/core/recorder.py @@ -6,6 +6,7 @@ import time from aws_xray_sdk.version import VERSION +from aws_xray_sdk.sdk_config import SDKConfig from .models.segment import Segment, SegmentContextManager from .models.subsegment import Subsegment, SubsegmentContextManager from .models.default_dynamic_naming import DefaultDynamicNaming @@ -18,12 +19,13 @@ from .daemon_config import DaemonConfig from .plugins.utils import get_plugin_modules from .lambda_launcher import check_in_lambda -from .exceptions.exceptions import SegmentNameMissingException +from .exceptions.exceptions import SegmentNameMissingException, SegmentNotFoundException from .utils.compat import string_types from .utils import stacktrace log = logging.getLogger(__name__) +XRAY_ENABLED_KEY = 'AWS_XRAY_ENABLED' TRACING_NAME_KEY = 'AWS_XRAY_TRACING_NAME' DAEMON_ADDR_KEY = 'AWS_XRAY_DAEMON_ADDRESS' CONTEXT_MISSING_KEY = 'AWS_XRAY_CONTEXT_MISSING' @@ -64,6 +66,7 @@ def __init__(self): self._context = Context() self._sampler = DefaultSampler() + self._enabled = True self._emitter = UDPEmitter() self._sampling = True self._max_trace_back = 10 @@ -77,7 +80,7 @@ def __init__(self): if type(self.sampler).__name__ == 'DefaultSampler': self.sampler.load_settings(DaemonConfig(), self.context) - def configure(self, sampling=None, plugins=None, + def configure(self, enabled=None, sampling=None, plugins=None, context_missing=None, sampling_rules=None, daemon_address=None, service=None, context=None, emitter=None, streaming=None, @@ -88,7 +91,16 @@ def configure(self, sampling=None, plugins=None, Configure needs to run before patching thrid party libraries to avoid creating dangling subsegment. - + :param bool enabled: If not enabled, the recorder automatically + generates DummySegments for every segment creation, whether + through patched extensions nor middlewares, and thus + not send any Segments out to the Daemon. May be set through an + environmental variable, where the environmental variable will + always take precedence over hardcoded configurations. The environment + variable is set as a case-insensitive string boolean. If the environment + variable exists but is an invalid string boolean, this enabled flag + will automatically be set to true. If no enabled flag is given and the + env variable is not set, then it will also default to being enabled. :param bool sampling: If sampling is enabled, every time the recorder creates a segment it decides whether to send this segment to the X-Ray daemon. This setting is not used if the recorder @@ -134,10 +146,11 @@ class to have your own implementation of the streaming process. by auto-capture. Lower this if a single document becomes too large. :param bool stream_sql: Whether SQL query texts should be streamed. - Environment variables AWS_XRAY_DAEMON_ADDRESS, AWS_XRAY_CONTEXT_MISSING + Environment variables AWS_XRAY_ENABLED, AWS_XRAY_DAEMON_ADDRESS, AWS_XRAY_CONTEXT_MISSING and AWS_XRAY_TRACING_NAME respectively overrides arguments - daemon_address, context_missing and service. + enabled, daemon_address, context_missing and service. """ + if sampling is not None: self.sampling = sampling if sampler: @@ -164,6 +177,12 @@ class to have your own implementation of the streaming process. self.max_trace_back = max_trace_back if stream_sql is not None: self.stream_sql = stream_sql + if enabled is not None: + SDKConfig.set_sdk_enabled(enabled) + else: + # By default we enable if no enable parameter is given. Prevents unit tests from breaking + # if setup doesn't explicitly set enabled while other tests set enabled to false. + SDKConfig.set_sdk_enabled(True) if plugins: plugin_modules = get_plugin_modules(plugins) @@ -219,6 +238,12 @@ def begin_segment(self, name=None, traceid=None, # depending on if centralized or local sampling rule takes effect. decision = True + # To disable the recorder, we set the sampling decision to always be false. + # This way, when segments are generated, they become dummy segments and are ultimately never sent. + # The call to self._sampler.should_trace() is never called either so the poller threads are never started. + if not SDKConfig.sdk_enabled(): + sampling = 0 + # we respect the input sampling decision # regardless of recorder configuration. if sampling == 0: @@ -273,6 +298,7 @@ def begin_subsegment(self, name, namespace='local'): :param str name: the name of the subsegment. :param str namespace: currently can only be 'local', 'remote', 'aws'. """ + segment = self.current_segment() if not segment: log.warning("No segment found, cannot begin subsegment %s." % name) @@ -396,6 +422,16 @@ def capture(self, name=None): def record_subsegment(self, wrapped, instance, args, kwargs, name, namespace, meta_processor): + # In the case when the SDK is disabled, we ensure that a parent segment exists, because this is usually + # handled by the middleware. We generate a dummy segment as the parent segment if one doesn't exist. + # This is to allow potential segment method calls to not throw exceptions in the captured method. + if not SDKConfig.sdk_enabled(): + try: + self.current_segment() + except SegmentNotFoundException: + segment = DummySegment(name) + self.context.put_segment(segment) + subsegment = self.begin_subsegment(name, namespace) exception = None @@ -473,6 +509,14 @@ def _is_subsegment(self, entity): return (hasattr(entity, 'type') and entity.type == 'subsegment') + @property + def enabled(self): + return self._enabled + + @enabled.setter + def enabled(self, value): + self._enabled = value + @property def sampling(self): return self._sampling diff --git a/aws_xray_sdk/sdk_config.py b/aws_xray_sdk/sdk_config.py new file mode 100644 index 00000000..0f6ceadf --- /dev/null +++ b/aws_xray_sdk/sdk_config.py @@ -0,0 +1,35 @@ +import os + + +class SDKConfig(object): + """ + Global Configuration Class that defines SDK-level configuration properties. + It is recommended to only use the recorder to set this configuration's enabled + flag to maintain thread safety. + """ + XRAY_ENABLED_KEY = 'AWS_XRAY_ENABLED' + __SDK_ENABLED = str(os.getenv(XRAY_ENABLED_KEY, 'true')).lower() != 'false' + + @staticmethod + def sdk_enabled(): + """ + Returns whether the SDK is enabled or not. + """ + return SDKConfig.__SDK_ENABLED + + @staticmethod + def set_sdk_enabled(value): + """ + Modifies the enabled flag if the "AWS_XRAY_ENABLED" environment variable is not set, + otherwise, set the enabled flag to be equal to the environment variable. If the + env variable is an invalid string boolean, it will default to true. + + :param bool value: Flag to set whether the SDK is enabled or disabled. + + Environment variables AWS_XRAY_ENABLED overrides argument value. + """ + # Environment Variables take precedence over hardcoded configurations. + if SDKConfig.XRAY_ENABLED_KEY in os.environ: + SDKConfig.__SDK_ENABLED = str(os.getenv(SDKConfig.XRAY_ENABLED_KEY, 'true')).lower() != 'false' + else: + SDKConfig.__SDK_ENABLED = value diff --git a/tests/ext/aiohttp/test_middleware.py b/tests/ext/aiohttp/test_middleware.py index e58848d8..81144481 100644 --- a/tests/ext/aiohttp/test_middleware.py +++ b/tests/ext/aiohttp/test_middleware.py @@ -283,3 +283,21 @@ async def get_delay(): # Ensure all ID's are different ids = [item.id for item in recorder.emitter.local] assert len(ids) == len(set(ids)) + + +async def test_disabled_sdk(test_client, loop, recorder): + """ + Test a normal response when the SDK is disabled. + + :param test_client: AioHttp test client fixture + :param loop: Eventloop fixture + :param recorder: X-Ray recorder fixture + """ + recorder.configure(enabled=False) + client = await test_client(ServerTest.app(loop=loop)) + + resp = await client.get('/') + assert resp.status == 200 + + segment = recorder.emitter.pop() + assert not segment diff --git a/tests/ext/django/test_middleware.py b/tests/ext/django/test_middleware.py index 66e96488..908424a4 100644 --- a/tests/ext/django/test_middleware.py +++ b/tests/ext/django/test_middleware.py @@ -102,3 +102,10 @@ def test_response_header(self): assert 'Sampled=1' in trace_header assert segment.trace_id in trace_header + + def test_disabled_sdk(self): + xray_recorder.configure(enabled=False) + url = reverse('200ok') + self.client.get(url) + segment = xray_recorder.emitter.pop() + assert not segment diff --git a/tests/ext/flask/test_flask.py b/tests/ext/flask/test_flask.py index 3b435028..145fc990 100644 --- a/tests/ext/flask/test_flask.py +++ b/tests/ext/flask/test_flask.py @@ -143,3 +143,11 @@ def test_sampled_response_header(): resp_header = resp.headers[http.XRAY_HEADER] assert segment.trace_id in resp_header assert 'Sampled=1' in resp_header + + +def test_disabled_sdk(): + recorder.configure(enabled=False) + path = '/ok' + app.get(path) + segment = recorder.emitter.pop() + assert not segment diff --git a/tests/test_lambda_context.py b/tests/test_lambda_context.py index 0bfec7b4..52baa222 100644 --- a/tests/test_lambda_context.py +++ b/tests/test_lambda_context.py @@ -1,7 +1,9 @@ import os +from aws_xray_sdk.sdk_config import SDKConfig from aws_xray_sdk.core import lambda_launcher from aws_xray_sdk.core.models.subsegment import Subsegment +from aws_xray_sdk.core.models.dummy_entities import DummySubsegment TRACE_ID = '1-5759e988-bd862e3fe1be46a994272793' @@ -41,3 +43,11 @@ def test_put_subsegment(): context.end_subsegment() assert context.get_trace_entity().id == segment.id + + +def test_disable(): + SDKConfig.set_sdk_enabled(False) + segment = context.get_trace_entity() + subsegment = Subsegment('name', 'local', segment) + context.put_subsegment(subsegment) + assert type(context.get_trace_entity()) is DummySubsegment diff --git a/tests/test_patcher.py b/tests/test_patcher.py index 944d1aad..951cf55e 100644 --- a/tests/test_patcher.py +++ b/tests/test_patcher.py @@ -13,6 +13,7 @@ # Python versions < 3 have reload built-in pass +from aws_xray_sdk.sdk_config import SDKConfig from aws_xray_sdk.core import patcher, xray_recorder from aws_xray_sdk.core.context import Context @@ -172,3 +173,11 @@ def test_external_submodules_ignores_module(): assert xray_recorder.current_segment().subsegments[0].name == 'mock_init' assert xray_recorder.current_segment().subsegments[1].name == 'mock_func' assert xray_recorder.current_segment().subsegments[2].name == 'mock_no_doublepatch' # It is patched with decorator + + +def test_disable_sdk_disables_patching(): + SDKConfig.set_sdk_enabled(False) + patcher.patch(['tests.mock_module']) + imported_modules = [module for module in TEST_MODULES if module in sys.modules] + assert not imported_modules + assert len(xray_recorder.current_segment().subsegments) == 0 diff --git a/tests/test_recorder.py b/tests/test_recorder.py index 32a906ca..142eda42 100644 --- a/tests/test_recorder.py +++ b/tests/test_recorder.py @@ -5,7 +5,14 @@ from aws_xray_sdk.version import VERSION from .util import get_new_stubbed_recorder +import os +from aws_xray_sdk.sdk_config import SDKConfig +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 + xray_recorder = get_new_stubbed_recorder() +XRAY_ENABLED_KEY = SDKConfig.XRAY_ENABLED_KEY @pytest.fixture(autouse=True) @@ -166,3 +173,45 @@ def test_in_segment_exception(): raise Exception('test exception') assert len(subsegment.cause['exceptions']) == 1 + + +def test_default_enabled(): + segment = xray_recorder.begin_segment('name') + subsegment = xray_recorder.begin_subsegment('name') + assert type(xray_recorder.current_segment()) is Segment + assert type(xray_recorder.current_subsegment()) is Subsegment + + +def test_disable_is_dummy(): + xray_recorder.configure(enabled=False) + segment = xray_recorder.begin_segment('name') + subsegment = xray_recorder.begin_subsegment('name') + assert type(xray_recorder.current_segment()) is DummySegment + assert type(xray_recorder.current_subsegment()) is DummySubsegment + + +def test_disable_env_precedence(): + os.environ[XRAY_ENABLED_KEY] = "False" + xray_recorder.configure(enabled=True) + segment = xray_recorder.begin_segment('name') + subsegment = xray_recorder.begin_subsegment('name') + assert type(xray_recorder.current_segment()) is DummySegment + assert type(xray_recorder.current_subsegment()) is DummySubsegment + + +def test_disable_env(): + os.environ[XRAY_ENABLED_KEY] = "False" + xray_recorder.configure(enabled=False) + segment = xray_recorder.begin_segment('name') + subsegment = xray_recorder.begin_subsegment('name') + assert type(xray_recorder.current_segment()) is DummySegment + assert type(xray_recorder.current_subsegment()) is DummySubsegment + + +def test_enable_env(): + os.environ[XRAY_ENABLED_KEY] = "True" + xray_recorder.configure(enabled=True) + segment = xray_recorder.begin_segment('name') + subsegment = xray_recorder.begin_subsegment('name') + assert type(xray_recorder.current_segment()) is Segment + assert type(xray_recorder.current_subsegment()) is Subsegment diff --git a/tests/test_sdk_config.py b/tests/test_sdk_config.py new file mode 100644 index 00000000..1a432f9a --- /dev/null +++ b/tests/test_sdk_config.py @@ -0,0 +1,76 @@ +from aws_xray_sdk.sdk_config import SDKConfig +import os +import pytest + + +XRAY_ENABLED_KEY = "AWS_XRAY_ENABLED" + + +@pytest.fixture(autouse=True) +def cleanup(): + """ + Clean up Environmental Variable for enable before and after tests + """ + if XRAY_ENABLED_KEY in os.environ: + del os.environ[XRAY_ENABLED_KEY] + yield + if XRAY_ENABLED_KEY in os.environ: + del os.environ[XRAY_ENABLED_KEY] + + +def test_enable_key(): + assert SDKConfig.XRAY_ENABLED_KEY == XRAY_ENABLED_KEY + + +def test_default_enabled(): + assert SDKConfig.sdk_enabled() is True + + +def test_env_var_precedence(): + os.environ[XRAY_ENABLED_KEY] = "true" + SDKConfig.set_sdk_enabled(False) + assert SDKConfig.sdk_enabled() is True + os.environ[XRAY_ENABLED_KEY] = "false" + SDKConfig.set_sdk_enabled(False) + assert SDKConfig.sdk_enabled() is False + os.environ[XRAY_ENABLED_KEY] = "false" + SDKConfig.set_sdk_enabled(True) + assert SDKConfig.sdk_enabled() is False + os.environ[XRAY_ENABLED_KEY] = "true" + SDKConfig.set_sdk_enabled(True) + assert SDKConfig.sdk_enabled() is True + os.environ[XRAY_ENABLED_KEY] = "true" + SDKConfig.set_sdk_enabled(None) + assert SDKConfig.sdk_enabled() is True + + +def test_env_enable_case(): + os.environ[XRAY_ENABLED_KEY] = "TrUE" + SDKConfig.set_sdk_enabled(True) # Env Variable takes precedence. This is called to activate the internal check + assert SDKConfig.sdk_enabled() is True + + os.environ[XRAY_ENABLED_KEY] = "true" + SDKConfig.set_sdk_enabled(True) + assert SDKConfig.sdk_enabled() is True + + os.environ[XRAY_ENABLED_KEY] = "False" + SDKConfig.set_sdk_enabled(True) + assert SDKConfig.sdk_enabled() is False + + os.environ[XRAY_ENABLED_KEY] = "falSE" + SDKConfig.set_sdk_enabled(True) + assert SDKConfig.sdk_enabled() is False + + +def test_invalid_env_string(): + os.environ[XRAY_ENABLED_KEY] = "INVALID" + SDKConfig.set_sdk_enabled(True) # Env Variable takes precedence. This is called to activate the internal check + assert SDKConfig.sdk_enabled() is True + + os.environ[XRAY_ENABLED_KEY] = "1.0" + SDKConfig.set_sdk_enabled(True) + assert SDKConfig.sdk_enabled() is True + + os.environ[XRAY_ENABLED_KEY] = "1-.0" + SDKConfig.set_sdk_enabled(False) + assert SDKConfig.sdk_enabled() is True From e96016408aa21b7bc22ef63167d7f26f4ee8dd2f Mon Sep 17 00:00:00 2001 From: Jeffery Saeteurn Date: Mon, 21 Jan 2019 13:54:56 -0800 Subject: [PATCH 2/5] Add ability to enable/disable the SDK (#26) Revision 2 Changes: - SDKConfig now built on top of recorder. SDK-level configuration should all be done through SDKConfig. - Renamed the SDK Enable environment variable to AWS_XRAY_SDK_ENABLED - Disabling the SDK now causes LambdaContext to set sampling decision of facade segment to False. Previously, it would force all subsegments to be generated as dummy subsegments. - SDKConfig.set_sdk_enabled(value) method defaults to True and throws an exception if value is not of type boolean. --- aws_xray_sdk/__init__.py | 3 ++ aws_xray_sdk/core/lambda_launcher.py | 19 ++++----- aws_xray_sdk/core/patcher.py | 4 +- aws_xray_sdk/core/recorder.py | 11 ++---- aws_xray_sdk/core/sampling/sampler.py | 7 ++++ aws_xray_sdk/sdk_config.py | 35 ++++++++++++----- tests/ext/aiohttp/test_middleware.py | 5 ++- tests/ext/django/test_middleware.py | 4 +- tests/test_lambda_context.py | 14 ++++--- tests/test_patcher.py | 6 ++- tests/test_recorder.py | 32 +-------------- tests/test_sdk_config.py | 56 +++++++++++++-------------- 12 files changed, 101 insertions(+), 95 deletions(-) diff --git a/aws_xray_sdk/__init__.py b/aws_xray_sdk/__init__.py index e69de29b..79ee3c82 100644 --- a/aws_xray_sdk/__init__.py +++ b/aws_xray_sdk/__init__.py @@ -0,0 +1,3 @@ +from .sdk_config import SDKConfig + +global_sdk_config = SDKConfig() diff --git a/aws_xray_sdk/core/lambda_launcher.py b/aws_xray_sdk/core/lambda_launcher.py index c2b59387..866ffa02 100644 --- a/aws_xray_sdk/core/lambda_launcher.py +++ b/aws_xray_sdk/core/lambda_launcher.py @@ -2,10 +2,9 @@ import logging import threading -from aws_xray_sdk.sdk_config import SDKConfig +import aws_xray_sdk from .models.facade_segment import FacadeSegment from .models.trace_header import TraceHeader -from .models.dummy_entities import DummySubsegment from .context import Context log = logging.getLogger(__name__) @@ -72,14 +71,10 @@ def put_subsegment(self, subsegment): current_entity = self.get_trace_entity() if not self._is_subsegment(current_entity) and current_entity.initializing: - log.warning("Subsegment %s discarded due to Lambda worker still initializing" % subsegment.name) + if sdk_config_module.sdk_enabled(): + log.warning("Subsegment %s discarded due to Lambda worker still initializing" % subsegment.name) return - enabled = SDKConfig.sdk_enabled() - if not enabled: - # For lambda, if the SDK is not enabled, we force the subsegment to be a dummy segment. - subsegment = DummySubsegment(current_entity) - current_entity.add_subsegment(subsegment) self._local.entities.append(subsegment) @@ -99,6 +94,9 @@ def _refresh_context(self): """ header_str = os.getenv(LAMBDA_TRACE_HEADER_KEY) trace_header = TraceHeader.from_header_str(header_str) + if not aws_xray_sdk.global_sdk_config.sdk_enabled(): + trace_header._sampled = False + segment = getattr(self._local, 'segment', None) if segment: @@ -130,7 +128,10 @@ def _initialize_context(self, trace_header): set by AWS Lambda and initialize storage for subsegments. """ sampled = None - if trace_header.sampled == 0: + if not aws_xray_sdk.global_sdk_config.sdk_enabled(): + # Force subsequent subsegments to be disabled and turned into DummySegments. + sampled = False + elif trace_header.sampled == 0: sampled = False elif trace_header.sampled == 1: sampled = True diff --git a/aws_xray_sdk/core/patcher.py b/aws_xray_sdk/core/patcher.py index a9fae76a..8bf8058b 100644 --- a/aws_xray_sdk/core/patcher.py +++ b/aws_xray_sdk/core/patcher.py @@ -7,7 +7,7 @@ import sys import wrapt -from aws_xray_sdk.sdk_config import SDKConfig +import aws_xray_sdk from .utils.compat import PY2, is_classmethod, is_instance_method log = logging.getLogger(__name__) @@ -61,7 +61,7 @@ def _is_valid_import(module): def patch(modules_to_patch, raise_errors=True, ignore_module_patterns=None): - enabled = SDKConfig.sdk_enabled() + enabled = aws_xray_sdk.global_sdk_config.sdk_enabled() if not enabled: return # Disable module patching if the SDK is disabled. modules = set() diff --git a/aws_xray_sdk/core/recorder.py b/aws_xray_sdk/core/recorder.py index e2c7efd4..4d09ade3 100644 --- a/aws_xray_sdk/core/recorder.py +++ b/aws_xray_sdk/core/recorder.py @@ -6,7 +6,6 @@ import time from aws_xray_sdk.version import VERSION -from aws_xray_sdk.sdk_config import SDKConfig from .models.segment import Segment, SegmentContextManager from .models.subsegment import Subsegment, SubsegmentContextManager from .models.default_dynamic_naming import DefaultDynamicNaming @@ -178,11 +177,7 @@ class to have your own implementation of the streaming process. if stream_sql is not None: self.stream_sql = stream_sql if enabled is not None: - SDKConfig.set_sdk_enabled(enabled) - else: - # By default we enable if no enable parameter is given. Prevents unit tests from breaking - # if setup doesn't explicitly set enabled while other tests set enabled to false. - SDKConfig.set_sdk_enabled(True) + self.enabled = enabled if plugins: plugin_modules = get_plugin_modules(plugins) @@ -241,7 +236,7 @@ def begin_segment(self, name=None, traceid=None, # To disable the recorder, we set the sampling decision to always be false. # This way, when segments are generated, they become dummy segments and are ultimately never sent. # The call to self._sampler.should_trace() is never called either so the poller threads are never started. - if not SDKConfig.sdk_enabled(): + if not self.enabled: sampling = 0 # we respect the input sampling decision @@ -425,7 +420,7 @@ def record_subsegment(self, wrapped, instance, args, kwargs, name, # In the case when the SDK is disabled, we ensure that a parent segment exists, because this is usually # handled by the middleware. We generate a dummy segment as the parent segment if one doesn't exist. # This is to allow potential segment method calls to not throw exceptions in the captured method. - if not SDKConfig.sdk_enabled(): + if not self.enabled: try: self.current_segment() except SegmentNotFoundException: diff --git a/aws_xray_sdk/core/sampling/sampler.py b/aws_xray_sdk/core/sampling/sampler.py index d5d03818..db5c4614 100644 --- a/aws_xray_sdk/core/sampling/sampler.py +++ b/aws_xray_sdk/core/sampling/sampler.py @@ -9,6 +9,7 @@ from .target_poller import TargetPoller from .connector import ServiceConnector from .reservoir import ReservoirDecision +import aws_xray_sdk log = logging.getLogger(__name__) @@ -37,6 +38,9 @@ def start(self): Start rule poller and target poller once X-Ray daemon address and context manager is in place. """ + if not aws_xray_sdk.global_sdk_config.sdk_enabled(): + return + with self._lock: if not self._started: self._rule_poller.start() @@ -51,6 +55,9 @@ def should_trace(self, sampling_req=None): All optional arguments are extracted from incoming requests by X-Ray middleware to perform path based sampling. """ + if not aws_xray_sdk.global_sdk_config.sdk_enabled(): + return False + if not self._started: self.start() # only front-end that actually uses the sampler spawns poller threads diff --git a/aws_xray_sdk/sdk_config.py b/aws_xray_sdk/sdk_config.py index 0f6ceadf..7bb86ee4 100644 --- a/aws_xray_sdk/sdk_config.py +++ b/aws_xray_sdk/sdk_config.py @@ -1,4 +1,12 @@ import os +import aws_xray_sdk.core + + +class InvalidParameterTypeException(Exception): + """ + Exception thrown when an invalid parameter is passed into SDKConfig.set_sdk_enabled. + """ + pass class SDKConfig(object): @@ -7,18 +15,18 @@ class SDKConfig(object): It is recommended to only use the recorder to set this configuration's enabled flag to maintain thread safety. """ - XRAY_ENABLED_KEY = 'AWS_XRAY_ENABLED' + XRAY_ENABLED_KEY = 'AWS_XRAY_SDK_ENABLED' __SDK_ENABLED = str(os.getenv(XRAY_ENABLED_KEY, 'true')).lower() != 'false' - @staticmethod - def sdk_enabled(): + @classmethod + def sdk_enabled(cls): """ Returns whether the SDK is enabled or not. """ - return SDKConfig.__SDK_ENABLED + return cls.__SDK_ENABLED - @staticmethod - def set_sdk_enabled(value): + @classmethod + def set_sdk_enabled(cls, value): """ Modifies the enabled flag if the "AWS_XRAY_ENABLED" environment variable is not set, otherwise, set the enabled flag to be equal to the environment variable. If the @@ -29,7 +37,16 @@ def set_sdk_enabled(value): Environment variables AWS_XRAY_ENABLED overrides argument value. """ # Environment Variables take precedence over hardcoded configurations. - if SDKConfig.XRAY_ENABLED_KEY in os.environ: - SDKConfig.__SDK_ENABLED = str(os.getenv(SDKConfig.XRAY_ENABLED_KEY, 'true')).lower() != 'false' + if cls.XRAY_ENABLED_KEY in os.environ: + cls.__SDK_ENABLED = str(os.getenv(cls.XRAY_ENABLED_KEY, 'true')).lower() != 'false' else: - SDKConfig.__SDK_ENABLED = value + if type(value) == bool: + cls.__SDK_ENABLED = value + else: + cls.__SDK_ENABLED = True + raise InvalidParameterTypeException( + "Invalid parameter type passed into set_sdk_enabled(). Defaulting to True..." + ) + + # Modify all key paths. + aws_xray_sdk.core.xray_recorder.configure(enabled=cls.__SDK_ENABLED) diff --git a/tests/ext/aiohttp/test_middleware.py b/tests/ext/aiohttp/test_middleware.py index 81144481..28aae53b 100644 --- a/tests/ext/aiohttp/test_middleware.py +++ b/tests/ext/aiohttp/test_middleware.py @@ -4,6 +4,7 @@ Expects pytest-aiohttp """ import asyncio +import aws_xray_sdk.sdk_config_module from unittest.mock import patch from aiohttp import web @@ -108,7 +109,9 @@ def recorder(loop): patcher.start() xray_recorder.clear_trace_entities() + sdk_enabled_state = aws_xray_sdk.global_sdk_config.sdk_enabled() yield xray_recorder + aws_xray_sdk.global_sdk_config.set_sdk_enabled(sdk_enabled_state) xray_recorder.clear_trace_entities() patcher.stop() @@ -293,7 +296,7 @@ async def test_disabled_sdk(test_client, loop, recorder): :param loop: Eventloop fixture :param recorder: X-Ray recorder fixture """ - recorder.configure(enabled=False) + aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) client = await test_client(ServerTest.app(loop=loop)) resp = await client.get('/') diff --git a/tests/ext/django/test_middleware.py b/tests/ext/django/test_middleware.py index 908424a4..f68bdac6 100644 --- a/tests/ext/django/test_middleware.py +++ b/tests/ext/django/test_middleware.py @@ -1,4 +1,5 @@ import django +import aws_xray_sdk from django.core.urlresolvers import reverse from django.test import TestCase @@ -14,6 +15,7 @@ def setUp(self): xray_recorder.configure(context=Context(), context_missing='LOG_ERROR') xray_recorder.clear_trace_entities() + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) def tearDown(self): xray_recorder.clear_trace_entities() @@ -104,7 +106,7 @@ def test_response_header(self): assert segment.trace_id in trace_header def test_disabled_sdk(self): - xray_recorder.configure(enabled=False) + aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) url = reverse('200ok') self.client.get(url) segment = xray_recorder.emitter.pop() diff --git a/tests/test_lambda_context.py b/tests/test_lambda_context.py index 52baa222..f7a004b1 100644 --- a/tests/test_lambda_context.py +++ b/tests/test_lambda_context.py @@ -1,6 +1,6 @@ import os -from aws_xray_sdk.sdk_config import SDKConfig +import aws_xray_sdk from aws_xray_sdk.core import lambda_launcher from aws_xray_sdk.core.models.subsegment import Subsegment from aws_xray_sdk.core.models.dummy_entities import DummySubsegment @@ -12,6 +12,7 @@ os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] = HEADER_VAR context = lambda_launcher.LambdaContext() +aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) def test_facade_segment_generation(): @@ -46,8 +47,11 @@ def test_put_subsegment(): def test_disable(): - SDKConfig.set_sdk_enabled(False) + context.clear_trace_entities() segment = context.get_trace_entity() - subsegment = Subsegment('name', 'local', segment) - context.put_subsegment(subsegment) - assert type(context.get_trace_entity()) is DummySubsegment + assert segment.sampled + + context.clear_trace_entities() + aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) + segment = context.get_trace_entity() + assert not segment.sampled diff --git a/tests/test_patcher.py b/tests/test_patcher.py index 951cf55e..4b026462 100644 --- a/tests/test_patcher.py +++ b/tests/test_patcher.py @@ -13,7 +13,7 @@ # Python versions < 3 have reload built-in pass -from aws_xray_sdk.sdk_config import SDKConfig +import aws_xray_sdk from aws_xray_sdk.core import patcher, xray_recorder from aws_xray_sdk.core.context import Context @@ -35,12 +35,14 @@ def construct_ctx(): """ pre_run_modules = set(module for module in sys.modules.keys()) + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) xray_recorder.configure(service='test', sampling=False, context=Context()) xray_recorder.clear_trace_entities() xray_recorder.begin_segment('name') yield xray_recorder.end_segment() xray_recorder.clear_trace_entities() + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) # Reload wrapt.importer references to modules to start off clean reload(wrapt) @@ -176,7 +178,7 @@ def test_external_submodules_ignores_module(): def test_disable_sdk_disables_patching(): - SDKConfig.set_sdk_enabled(False) + aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) patcher.patch(['tests.mock_module']) imported_modules = [module for module in TEST_MODULES if module in sys.modules] assert not imported_modules diff --git a/tests/test_recorder.py b/tests/test_recorder.py index 142eda42..20dcd968 100644 --- a/tests/test_recorder.py +++ b/tests/test_recorder.py @@ -5,14 +5,13 @@ from aws_xray_sdk.version import VERSION from .util import get_new_stubbed_recorder -import os -from aws_xray_sdk.sdk_config import SDKConfig +import aws_xray_sdk 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 xray_recorder = get_new_stubbed_recorder() -XRAY_ENABLED_KEY = SDKConfig.XRAY_ENABLED_KEY +XRAY_ENABLED_KEY = aws_xray_sdk.global_sdk_config.XRAY_ENABLED_KEY @pytest.fixture(autouse=True) @@ -188,30 +187,3 @@ def test_disable_is_dummy(): subsegment = xray_recorder.begin_subsegment('name') assert type(xray_recorder.current_segment()) is DummySegment assert type(xray_recorder.current_subsegment()) is DummySubsegment - - -def test_disable_env_precedence(): - os.environ[XRAY_ENABLED_KEY] = "False" - xray_recorder.configure(enabled=True) - segment = xray_recorder.begin_segment('name') - subsegment = xray_recorder.begin_subsegment('name') - assert type(xray_recorder.current_segment()) is DummySegment - assert type(xray_recorder.current_subsegment()) is DummySubsegment - - -def test_disable_env(): - os.environ[XRAY_ENABLED_KEY] = "False" - xray_recorder.configure(enabled=False) - segment = xray_recorder.begin_segment('name') - subsegment = xray_recorder.begin_subsegment('name') - assert type(xray_recorder.current_segment()) is DummySegment - assert type(xray_recorder.current_subsegment()) is DummySubsegment - - -def test_enable_env(): - os.environ[XRAY_ENABLED_KEY] = "True" - xray_recorder.configure(enabled=True) - segment = xray_recorder.begin_segment('name') - subsegment = xray_recorder.begin_subsegment('name') - assert type(xray_recorder.current_segment()) is Segment - assert type(xray_recorder.current_subsegment()) is Subsegment diff --git a/tests/test_sdk_config.py b/tests/test_sdk_config.py index 1a432f9a..247e460a 100644 --- a/tests/test_sdk_config.py +++ b/tests/test_sdk_config.py @@ -1,9 +1,9 @@ -from aws_xray_sdk.sdk_config import SDKConfig +import aws_xray_sdk import os import pytest -XRAY_ENABLED_KEY = "AWS_XRAY_ENABLED" +XRAY_ENABLED_KEY = "AWS_XRAY_SDK_ENABLED" @pytest.fixture(autouse=True) @@ -19,58 +19,58 @@ def cleanup(): def test_enable_key(): - assert SDKConfig.XRAY_ENABLED_KEY == XRAY_ENABLED_KEY + assert aws_xray_sdk.global_sdk_config.XRAY_ENABLED_KEY == XRAY_ENABLED_KEY def test_default_enabled(): - assert SDKConfig.sdk_enabled() is True + assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True def test_env_var_precedence(): os.environ[XRAY_ENABLED_KEY] = "true" - SDKConfig.set_sdk_enabled(False) - assert SDKConfig.sdk_enabled() is True + aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) + assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "false" - SDKConfig.set_sdk_enabled(False) - assert SDKConfig.sdk_enabled() is False + aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) + assert aws_xray_sdk.global_sdk_config.sdk_enabled() is False os.environ[XRAY_ENABLED_KEY] = "false" - SDKConfig.set_sdk_enabled(True) - assert SDKConfig.sdk_enabled() is False + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + assert aws_xray_sdk.global_sdk_config.sdk_enabled() is False os.environ[XRAY_ENABLED_KEY] = "true" - SDKConfig.set_sdk_enabled(True) - assert SDKConfig.sdk_enabled() is True + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "true" - SDKConfig.set_sdk_enabled(None) - assert SDKConfig.sdk_enabled() is True + aws_xray_sdk.global_sdk_config.set_sdk_enabled(None) + assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True def test_env_enable_case(): os.environ[XRAY_ENABLED_KEY] = "TrUE" - SDKConfig.set_sdk_enabled(True) # Env Variable takes precedence. This is called to activate the internal check - assert SDKConfig.sdk_enabled() is True + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) # Env Variable takes precedence. This is called to activate the internal check + assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "true" - SDKConfig.set_sdk_enabled(True) - assert SDKConfig.sdk_enabled() is True + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "False" - SDKConfig.set_sdk_enabled(True) - assert SDKConfig.sdk_enabled() is False + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + assert aws_xray_sdk.global_sdk_config.sdk_enabled() is False os.environ[XRAY_ENABLED_KEY] = "falSE" - SDKConfig.set_sdk_enabled(True) - assert SDKConfig.sdk_enabled() is False + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + assert aws_xray_sdk.global_sdk_config.sdk_enabled() is False def test_invalid_env_string(): os.environ[XRAY_ENABLED_KEY] = "INVALID" - SDKConfig.set_sdk_enabled(True) # Env Variable takes precedence. This is called to activate the internal check - assert SDKConfig.sdk_enabled() is True + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) # Env Variable takes precedence. This is called to activate the internal check + assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "1.0" - SDKConfig.set_sdk_enabled(True) - assert SDKConfig.sdk_enabled() is True + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "1-.0" - SDKConfig.set_sdk_enabled(False) - assert SDKConfig.sdk_enabled() is True + aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) + assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True From 682703e4ab577eaeec8371b0ae489c04effab60e Mon Sep 17 00:00:00 2001 From: Jeffery Saeteurn Date: Wed, 23 Jan 2019 12:41:22 -0800 Subject: [PATCH 3/5] Add ability to enable/disable the SDK (#26) Revision 3 Changes: - Removed enable flag from recorder. Now it checks the global SDK configuration to determine if the SDK is disabled. Global SDK configuration no longer modifies the recorder. - Tests modified to reflect change. --- aws_xray_sdk/core/recorder.py | 24 ++++++------------------ aws_xray_sdk/sdk_config.py | 28 ++++++++++++++++++++-------- tests/ext/aiohttp/test_middleware.py | 5 ++--- tests/ext/flask/test_flask.py | 4 +++- tests/test_lambda_context.py | 9 +++++++-- tests/test_patcher.py | 1 - tests/test_recorder.py | 4 +++- tests/test_sdk_config.py | 8 ++++++-- 8 files changed, 47 insertions(+), 36 deletions(-) diff --git a/aws_xray_sdk/core/recorder.py b/aws_xray_sdk/core/recorder.py index 4d09ade3..70a8d36e 100644 --- a/aws_xray_sdk/core/recorder.py +++ b/aws_xray_sdk/core/recorder.py @@ -5,6 +5,7 @@ import platform import time +import aws_xray_sdk from aws_xray_sdk.version import VERSION from .models.segment import Segment, SegmentContextManager from .models.subsegment import Subsegment, SubsegmentContextManager @@ -65,7 +66,6 @@ def __init__(self): self._context = Context() self._sampler = DefaultSampler() - self._enabled = True self._emitter = UDPEmitter() self._sampling = True self._max_trace_back = 10 @@ -79,7 +79,7 @@ def __init__(self): if type(self.sampler).__name__ == 'DefaultSampler': self.sampler.load_settings(DaemonConfig(), self.context) - def configure(self, enabled=None, sampling=None, plugins=None, + def configure(self, sampling=None, plugins=None, context_missing=None, sampling_rules=None, daemon_address=None, service=None, context=None, emitter=None, streaming=None, @@ -90,16 +90,6 @@ def configure(self, enabled=None, sampling=None, plugins=None, Configure needs to run before patching thrid party libraries to avoid creating dangling subsegment. - :param bool enabled: If not enabled, the recorder automatically - generates DummySegments for every segment creation, whether - through patched extensions nor middlewares, and thus - not send any Segments out to the Daemon. May be set through an - environmental variable, where the environmental variable will - always take precedence over hardcoded configurations. The environment - variable is set as a case-insensitive string boolean. If the environment - variable exists but is an invalid string boolean, this enabled flag - will automatically be set to true. If no enabled flag is given and the - env variable is not set, then it will also default to being enabled. :param bool sampling: If sampling is enabled, every time the recorder creates a segment it decides whether to send this segment to the X-Ray daemon. This setting is not used if the recorder @@ -145,9 +135,9 @@ class to have your own implementation of the streaming process. by auto-capture. Lower this if a single document becomes too large. :param bool stream_sql: Whether SQL query texts should be streamed. - Environment variables AWS_XRAY_ENABLED, AWS_XRAY_DAEMON_ADDRESS, AWS_XRAY_CONTEXT_MISSING + Environment variables AWS_XRAY_DAEMON_ADDRESS, AWS_XRAY_CONTEXT_MISSING and AWS_XRAY_TRACING_NAME respectively overrides arguments - enabled, daemon_address, context_missing and service. + daemon_address, context_missing and service. """ if sampling is not None: @@ -176,8 +166,6 @@ class to have your own implementation of the streaming process. self.max_trace_back = max_trace_back if stream_sql is not None: self.stream_sql = stream_sql - if enabled is not None: - self.enabled = enabled if plugins: plugin_modules = get_plugin_modules(plugins) @@ -236,7 +224,7 @@ def begin_segment(self, name=None, traceid=None, # To disable the recorder, we set the sampling decision to always be false. # This way, when segments are generated, they become dummy segments and are ultimately never sent. # The call to self._sampler.should_trace() is never called either so the poller threads are never started. - if not self.enabled: + if not aws_xray_sdk.global_sdk_config.sdk_enabled(): sampling = 0 # we respect the input sampling decision @@ -420,7 +408,7 @@ def record_subsegment(self, wrapped, instance, args, kwargs, name, # In the case when the SDK is disabled, we ensure that a parent segment exists, because this is usually # handled by the middleware. We generate a dummy segment as the parent segment if one doesn't exist. # This is to allow potential segment method calls to not throw exceptions in the captured method. - if not self.enabled: + if not aws_xray_sdk.global_sdk_config.sdk_enabled(): try: self.current_segment() except SegmentNotFoundException: diff --git a/aws_xray_sdk/sdk_config.py b/aws_xray_sdk/sdk_config.py index 7bb86ee4..1228093b 100644 --- a/aws_xray_sdk/sdk_config.py +++ b/aws_xray_sdk/sdk_config.py @@ -1,5 +1,4 @@ import os -import aws_xray_sdk.core class InvalidParameterTypeException(Exception): @@ -12,8 +11,24 @@ class InvalidParameterTypeException(Exception): class SDKConfig(object): """ Global Configuration Class that defines SDK-level configuration properties. - It is recommended to only use the recorder to set this configuration's enabled - flag to maintain thread safety. + + Enabling/Disabling the SDK: + By default, the SDK is enabled unless if an environment variable AWS_XRAY_SDK_ENABLED + is set. If it is set, it needs to be a valid string boolean, otherwise, it will default + to true. If the environment variable is set, all calls to set_sdk_enabled() will + prioritize the value of the environment variable. + Disabling the SDK affects the recorder, patcher, and middlewares in the following ways: + For the recorder, disabling automatically generates DummySegments for subsequent segments + and DummySubsegments for subsegments created and thus not send any traces to the daemon. + For the patcher, module patching will automatically be disabled. The SDK must be disabled + before calling patcher.patch() method in order for this to function properly. + For the middleware, no modification is made on them, but since the recorder automatically + generates DummySegments for all subsequent calls, they will not generate segments/subsegments + to be sent. + + Environment variables: + "AWS_XRAY_SDK_ENABLED" - If set to 'false' disables the SDK and causes the explained above + to occur. """ XRAY_ENABLED_KEY = 'AWS_XRAY_SDK_ENABLED' __SDK_ENABLED = str(os.getenv(XRAY_ENABLED_KEY, 'true')).lower() != 'false' @@ -28,13 +43,13 @@ def sdk_enabled(cls): @classmethod def set_sdk_enabled(cls, value): """ - Modifies the enabled flag if the "AWS_XRAY_ENABLED" environment variable is not set, + Modifies the enabled flag if the "AWS_XRAY_SDK_ENABLED" environment variable is not set, otherwise, set the enabled flag to be equal to the environment variable. If the env variable is an invalid string boolean, it will default to true. :param bool value: Flag to set whether the SDK is enabled or disabled. - Environment variables AWS_XRAY_ENABLED overrides argument value. + Environment variables AWS_XRAY_SDK_ENABLED overrides argument value. """ # Environment Variables take precedence over hardcoded configurations. if cls.XRAY_ENABLED_KEY in os.environ: @@ -47,6 +62,3 @@ def set_sdk_enabled(cls, value): raise InvalidParameterTypeException( "Invalid parameter type passed into set_sdk_enabled(). Defaulting to True..." ) - - # Modify all key paths. - aws_xray_sdk.core.xray_recorder.configure(enabled=cls.__SDK_ENABLED) diff --git a/tests/ext/aiohttp/test_middleware.py b/tests/ext/aiohttp/test_middleware.py index 28aae53b..339e53e6 100644 --- a/tests/ext/aiohttp/test_middleware.py +++ b/tests/ext/aiohttp/test_middleware.py @@ -4,7 +4,7 @@ Expects pytest-aiohttp """ import asyncio -import aws_xray_sdk.sdk_config_module +import aws_xray_sdk from unittest.mock import patch from aiohttp import web @@ -109,9 +109,8 @@ def recorder(loop): patcher.start() xray_recorder.clear_trace_entities() - sdk_enabled_state = aws_xray_sdk.global_sdk_config.sdk_enabled() yield xray_recorder - aws_xray_sdk.global_sdk_config.set_sdk_enabled(sdk_enabled_state) + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) xray_recorder.clear_trace_entities() patcher.stop() diff --git a/tests/ext/flask/test_flask.py b/tests/ext/flask/test_flask.py index 145fc990..f32bf870 100644 --- a/tests/ext/flask/test_flask.py +++ b/tests/ext/flask/test_flask.py @@ -1,6 +1,7 @@ import pytest from flask import Flask, render_template_string +import aws_xray_sdk from aws_xray_sdk.ext.flask.middleware import XRayMiddleware from aws_xray_sdk.core.context import Context from aws_xray_sdk.core.models import http @@ -51,6 +52,7 @@ def cleanup(): recorder.clear_trace_entities() yield recorder.clear_trace_entities() + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) def test_ok(): @@ -146,7 +148,7 @@ def test_sampled_response_header(): def test_disabled_sdk(): - recorder.configure(enabled=False) + aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) path = '/ok' app.get(path) segment = recorder.emitter.pop() diff --git a/tests/test_lambda_context.py b/tests/test_lambda_context.py index f7a004b1..f7cf7fa5 100644 --- a/tests/test_lambda_context.py +++ b/tests/test_lambda_context.py @@ -1,9 +1,9 @@ import os import aws_xray_sdk +import pytest from aws_xray_sdk.core import lambda_launcher from aws_xray_sdk.core.models.subsegment import Subsegment -from aws_xray_sdk.core.models.dummy_entities import DummySubsegment TRACE_ID = '1-5759e988-bd862e3fe1be46a994272793' @@ -12,7 +12,12 @@ os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] = HEADER_VAR context = lambda_launcher.LambdaContext() -aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + + +@pytest.fixture(autouse=True) +def setup(): + yield + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) def test_facade_segment_generation(): diff --git a/tests/test_patcher.py b/tests/test_patcher.py index 4b026462..89cbfbe2 100644 --- a/tests/test_patcher.py +++ b/tests/test_patcher.py @@ -35,7 +35,6 @@ def construct_ctx(): """ pre_run_modules = set(module for module in sys.modules.keys()) - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) xray_recorder.configure(service='test', sampling=False, context=Context()) xray_recorder.clear_trace_entities() xray_recorder.begin_segment('name') diff --git a/tests/test_recorder.py b/tests/test_recorder.py index 20dcd968..99246263 100644 --- a/tests/test_recorder.py +++ b/tests/test_recorder.py @@ -23,6 +23,7 @@ def construct_ctx(): xray_recorder.clear_trace_entities() yield xray_recorder.clear_trace_entities() + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) def test_default_runtime_context(): @@ -175,6 +176,7 @@ def test_in_segment_exception(): def test_default_enabled(): + assert aws_xray_sdk.global_sdk_config.sdk_enabled() segment = xray_recorder.begin_segment('name') subsegment = xray_recorder.begin_subsegment('name') assert type(xray_recorder.current_segment()) is Segment @@ -182,7 +184,7 @@ def test_default_enabled(): def test_disable_is_dummy(): - xray_recorder.configure(enabled=False) + aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) segment = xray_recorder.begin_segment('name') subsegment = xray_recorder.begin_subsegment('name') assert type(xray_recorder.current_segment()) is DummySegment diff --git a/tests/test_sdk_config.py b/tests/test_sdk_config.py index 247e460a..ac48b061 100644 --- a/tests/test_sdk_config.py +++ b/tests/test_sdk_config.py @@ -16,6 +16,7 @@ def cleanup(): yield if XRAY_ENABLED_KEY in os.environ: del os.environ[XRAY_ENABLED_KEY] + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) def test_enable_key(): @@ -28,6 +29,7 @@ def test_default_enabled(): def test_env_var_precedence(): os.environ[XRAY_ENABLED_KEY] = "true" + # Env Variable takes precedence. This is called to activate the internal check aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "false" @@ -46,7 +48,8 @@ def test_env_var_precedence(): def test_env_enable_case(): os.environ[XRAY_ENABLED_KEY] = "TrUE" - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) # Env Variable takes precedence. This is called to activate the internal check + # Env Variable takes precedence. This is called to activate the internal check + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "true" @@ -64,7 +67,8 @@ def test_env_enable_case(): def test_invalid_env_string(): os.environ[XRAY_ENABLED_KEY] = "INVALID" - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) # Env Variable takes precedence. This is called to activate the internal check + # Env Variable takes precedence. This is called to activate the internal check + aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "1.0" From 322792a1db68895cb0c678e3f2d0b98a8782d806 Mon Sep 17 00:00:00 2001 From: Jeffery Saeteurn Date: Tue, 29 Jan 2019 13:16:35 -0800 Subject: [PATCH 4/5] Add ability to enable/disable the SDK (#26) Revision 4 Changes: - Refactored multiple modules; instead of import aws_xray_sdk to get the global_sdk_config, direct import using from... import. - Removed unused XRAY_ENABLED_KEY variable from recorder. It wasn't being used. - Added debug log entry to the patcher to inform the customer that patching is disabled when the SDK is disabled. --- aws_xray_sdk/core/lambda_launcher.py | 6 +-- aws_xray_sdk/core/patcher.py | 5 ++- aws_xray_sdk/core/recorder.py | 7 ++-- tests/ext/aiohttp/test_middleware.py | 6 +-- tests/ext/django/test_middleware.py | 6 +-- tests/ext/flask/test_flask.py | 6 +-- tests/test_lambda_context.py | 6 +-- tests/test_patcher.py | 6 +-- tests/test_recorder.py | 10 ++--- tests/test_sdk_config.py | 56 ++++++++++++++-------------- 10 files changed, 57 insertions(+), 57 deletions(-) diff --git a/aws_xray_sdk/core/lambda_launcher.py b/aws_xray_sdk/core/lambda_launcher.py index 866ffa02..79ab8338 100644 --- a/aws_xray_sdk/core/lambda_launcher.py +++ b/aws_xray_sdk/core/lambda_launcher.py @@ -2,7 +2,7 @@ import logging import threading -import aws_xray_sdk +from aws_xray_sdk import global_sdk_config from .models.facade_segment import FacadeSegment from .models.trace_header import TraceHeader from .context import Context @@ -94,7 +94,7 @@ def _refresh_context(self): """ header_str = os.getenv(LAMBDA_TRACE_HEADER_KEY) trace_header = TraceHeader.from_header_str(header_str) - if not aws_xray_sdk.global_sdk_config.sdk_enabled(): + if not global_sdk_config.sdk_enabled(): trace_header._sampled = False segment = getattr(self._local, 'segment', None) @@ -128,7 +128,7 @@ def _initialize_context(self, trace_header): set by AWS Lambda and initialize storage for subsegments. """ sampled = None - if not aws_xray_sdk.global_sdk_config.sdk_enabled(): + if not global_sdk_config.sdk_enabled(): # Force subsequent subsegments to be disabled and turned into DummySegments. sampled = False elif trace_header.sampled == 0: diff --git a/aws_xray_sdk/core/patcher.py b/aws_xray_sdk/core/patcher.py index 8bf8058b..2107ac65 100644 --- a/aws_xray_sdk/core/patcher.py +++ b/aws_xray_sdk/core/patcher.py @@ -7,7 +7,7 @@ import sys import wrapt -import aws_xray_sdk +from aws_xray_sdk import global_sdk_config from .utils.compat import PY2, is_classmethod, is_instance_method log = logging.getLogger(__name__) @@ -61,8 +61,9 @@ def _is_valid_import(module): def patch(modules_to_patch, raise_errors=True, ignore_module_patterns=None): - enabled = aws_xray_sdk.global_sdk_config.sdk_enabled() + enabled = global_sdk_config.sdk_enabled() if not enabled: + log.debug("Skipped patching modules %s because the SDK is currently disabled." % ', '.join(modules_to_patch)) return # Disable module patching if the SDK is disabled. modules = set() for module_to_patch in modules_to_patch: diff --git a/aws_xray_sdk/core/recorder.py b/aws_xray_sdk/core/recorder.py index e0e0aedc..b953cece 100644 --- a/aws_xray_sdk/core/recorder.py +++ b/aws_xray_sdk/core/recorder.py @@ -5,7 +5,7 @@ import platform import time -import aws_xray_sdk +from aws_xray_sdk import global_sdk_config from aws_xray_sdk.version import VERSION from .models.segment import Segment, SegmentContextManager from .models.subsegment import Subsegment, SubsegmentContextManager @@ -25,7 +25,6 @@ log = logging.getLogger(__name__) -XRAY_ENABLED_KEY = 'AWS_XRAY_ENABLED' TRACING_NAME_KEY = 'AWS_XRAY_TRACING_NAME' DAEMON_ADDR_KEY = 'AWS_XRAY_DAEMON_ADDRESS' CONTEXT_MISSING_KEY = 'AWS_XRAY_CONTEXT_MISSING' @@ -224,7 +223,7 @@ def begin_segment(self, name=None, traceid=None, # To disable the recorder, we set the sampling decision to always be false. # This way, when segments are generated, they become dummy segments and are ultimately never sent. # The call to self._sampler.should_trace() is never called either so the poller threads are never started. - if not aws_xray_sdk.global_sdk_config.sdk_enabled(): + if not global_sdk_config.sdk_enabled(): sampling = 0 # we respect the input sampling decision @@ -408,7 +407,7 @@ def record_subsegment(self, wrapped, instance, args, kwargs, name, # In the case when the SDK is disabled, we ensure that a parent segment exists, because this is usually # handled by the middleware. We generate a dummy segment as the parent segment if one doesn't exist. # This is to allow potential segment method calls to not throw exceptions in the captured method. - if not aws_xray_sdk.global_sdk_config.sdk_enabled(): + if not global_sdk_config.sdk_enabled(): try: self.current_segment() except SegmentNotFoundException: diff --git a/tests/ext/aiohttp/test_middleware.py b/tests/ext/aiohttp/test_middleware.py index 339e53e6..c8b23335 100644 --- a/tests/ext/aiohttp/test_middleware.py +++ b/tests/ext/aiohttp/test_middleware.py @@ -4,7 +4,7 @@ Expects pytest-aiohttp """ import asyncio -import aws_xray_sdk +from aws_xray_sdk import global_sdk_config from unittest.mock import patch from aiohttp import web @@ -110,7 +110,7 @@ def recorder(loop): xray_recorder.clear_trace_entities() yield xray_recorder - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + global_sdk_config.set_sdk_enabled(True) xray_recorder.clear_trace_entities() patcher.stop() @@ -295,7 +295,7 @@ async def test_disabled_sdk(test_client, loop, recorder): :param loop: Eventloop fixture :param recorder: X-Ray recorder fixture """ - aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) + global_sdk_config.set_sdk_enabled(False) client = await test_client(ServerTest.app(loop=loop)) resp = await client.get('/') diff --git a/tests/ext/django/test_middleware.py b/tests/ext/django/test_middleware.py index f68bdac6..a0128b7c 100644 --- a/tests/ext/django/test_middleware.py +++ b/tests/ext/django/test_middleware.py @@ -1,5 +1,5 @@ import django -import aws_xray_sdk +from aws_xray_sdk import global_sdk_config from django.core.urlresolvers import reverse from django.test import TestCase @@ -15,7 +15,7 @@ def setUp(self): xray_recorder.configure(context=Context(), context_missing='LOG_ERROR') xray_recorder.clear_trace_entities() - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + global_sdk_config.set_sdk_enabled(True) def tearDown(self): xray_recorder.clear_trace_entities() @@ -106,7 +106,7 @@ def test_response_header(self): assert segment.trace_id in trace_header def test_disabled_sdk(self): - aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) + global_sdk_config.set_sdk_enabled(False) url = reverse('200ok') self.client.get(url) segment = xray_recorder.emitter.pop() diff --git a/tests/ext/flask/test_flask.py b/tests/ext/flask/test_flask.py index f32bf870..07c8d42c 100644 --- a/tests/ext/flask/test_flask.py +++ b/tests/ext/flask/test_flask.py @@ -1,7 +1,7 @@ import pytest from flask import Flask, render_template_string -import aws_xray_sdk +from aws_xray_sdk import global_sdk_config from aws_xray_sdk.ext.flask.middleware import XRayMiddleware from aws_xray_sdk.core.context import Context from aws_xray_sdk.core.models import http @@ -52,7 +52,7 @@ def cleanup(): recorder.clear_trace_entities() yield recorder.clear_trace_entities() - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + global_sdk_config.set_sdk_enabled(True) def test_ok(): @@ -148,7 +148,7 @@ def test_sampled_response_header(): def test_disabled_sdk(): - aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) + global_sdk_config.set_sdk_enabled(False) path = '/ok' app.get(path) segment = recorder.emitter.pop() diff --git a/tests/test_lambda_context.py b/tests/test_lambda_context.py index f7cf7fa5..90f405e8 100644 --- a/tests/test_lambda_context.py +++ b/tests/test_lambda_context.py @@ -1,6 +1,6 @@ import os -import aws_xray_sdk +from aws_xray_sdk import global_sdk_config import pytest from aws_xray_sdk.core import lambda_launcher from aws_xray_sdk.core.models.subsegment import Subsegment @@ -17,7 +17,7 @@ @pytest.fixture(autouse=True) def setup(): yield - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + global_sdk_config.set_sdk_enabled(True) def test_facade_segment_generation(): @@ -57,6 +57,6 @@ def test_disable(): assert segment.sampled context.clear_trace_entities() - aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) + global_sdk_config.set_sdk_enabled(False) segment = context.get_trace_entity() assert not segment.sampled diff --git a/tests/test_patcher.py b/tests/test_patcher.py index 89cbfbe2..f9651401 100644 --- a/tests/test_patcher.py +++ b/tests/test_patcher.py @@ -13,7 +13,7 @@ # Python versions < 3 have reload built-in pass -import aws_xray_sdk +from aws_xray_sdk import global_sdk_config from aws_xray_sdk.core import patcher, xray_recorder from aws_xray_sdk.core.context import Context @@ -41,7 +41,7 @@ def construct_ctx(): yield xray_recorder.end_segment() xray_recorder.clear_trace_entities() - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + global_sdk_config.set_sdk_enabled(True) # Reload wrapt.importer references to modules to start off clean reload(wrapt) @@ -177,7 +177,7 @@ def test_external_submodules_ignores_module(): def test_disable_sdk_disables_patching(): - aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) + global_sdk_config.set_sdk_enabled(False) patcher.patch(['tests.mock_module']) imported_modules = [module for module in TEST_MODULES if module in sys.modules] assert not imported_modules diff --git a/tests/test_recorder.py b/tests/test_recorder.py index f69aecf5..0fbd4f13 100644 --- a/tests/test_recorder.py +++ b/tests/test_recorder.py @@ -5,13 +5,13 @@ from aws_xray_sdk.version import VERSION from .util import get_new_stubbed_recorder -import aws_xray_sdk +from aws_xray_sdk import global_sdk_config 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 xray_recorder = get_new_stubbed_recorder() -XRAY_ENABLED_KEY = aws_xray_sdk.global_sdk_config.XRAY_ENABLED_KEY +XRAY_ENABLED_KEY = global_sdk_config.XRAY_ENABLED_KEY @pytest.fixture(autouse=True) @@ -23,7 +23,7 @@ def construct_ctx(): xray_recorder.clear_trace_entities() yield xray_recorder.clear_trace_entities() - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + global_sdk_config.set_sdk_enabled(True) def test_default_runtime_context(): @@ -176,7 +176,7 @@ def test_in_segment_exception(): def test_default_enabled(): - assert aws_xray_sdk.global_sdk_config.sdk_enabled() + assert global_sdk_config.sdk_enabled() segment = xray_recorder.begin_segment('name') subsegment = xray_recorder.begin_subsegment('name') assert type(xray_recorder.current_segment()) is Segment @@ -184,7 +184,7 @@ def test_default_enabled(): def test_disable_is_dummy(): - aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) + global_sdk_config.set_sdk_enabled(False) segment = xray_recorder.begin_segment('name') subsegment = xray_recorder.begin_subsegment('name') assert type(xray_recorder.current_segment()) is DummySegment diff --git a/tests/test_sdk_config.py b/tests/test_sdk_config.py index ac48b061..81569456 100644 --- a/tests/test_sdk_config.py +++ b/tests/test_sdk_config.py @@ -1,4 +1,4 @@ -import aws_xray_sdk +from aws_xray_sdk import global_sdk_config import os import pytest @@ -16,65 +16,65 @@ def cleanup(): yield if XRAY_ENABLED_KEY in os.environ: del os.environ[XRAY_ENABLED_KEY] - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) + global_sdk_config.set_sdk_enabled(True) def test_enable_key(): - assert aws_xray_sdk.global_sdk_config.XRAY_ENABLED_KEY == XRAY_ENABLED_KEY + assert global_sdk_config.XRAY_ENABLED_KEY == XRAY_ENABLED_KEY def test_default_enabled(): - assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True + assert global_sdk_config.sdk_enabled() is True def test_env_var_precedence(): os.environ[XRAY_ENABLED_KEY] = "true" # Env Variable takes precedence. This is called to activate the internal check - aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) - assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True + global_sdk_config.set_sdk_enabled(False) + assert global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "false" - aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) - assert aws_xray_sdk.global_sdk_config.sdk_enabled() is False + global_sdk_config.set_sdk_enabled(False) + assert global_sdk_config.sdk_enabled() is False os.environ[XRAY_ENABLED_KEY] = "false" - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) - assert aws_xray_sdk.global_sdk_config.sdk_enabled() is False + global_sdk_config.set_sdk_enabled(True) + assert global_sdk_config.sdk_enabled() is False os.environ[XRAY_ENABLED_KEY] = "true" - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) - assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True + global_sdk_config.set_sdk_enabled(True) + assert global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "true" - aws_xray_sdk.global_sdk_config.set_sdk_enabled(None) - assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True + global_sdk_config.set_sdk_enabled(None) + assert global_sdk_config.sdk_enabled() is True def test_env_enable_case(): os.environ[XRAY_ENABLED_KEY] = "TrUE" # Env Variable takes precedence. This is called to activate the internal check - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) - assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True + global_sdk_config.set_sdk_enabled(True) + assert global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "true" - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) - assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True + global_sdk_config.set_sdk_enabled(True) + assert global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "False" - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) - assert aws_xray_sdk.global_sdk_config.sdk_enabled() is False + global_sdk_config.set_sdk_enabled(True) + assert global_sdk_config.sdk_enabled() is False os.environ[XRAY_ENABLED_KEY] = "falSE" - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) - assert aws_xray_sdk.global_sdk_config.sdk_enabled() is False + global_sdk_config.set_sdk_enabled(True) + assert global_sdk_config.sdk_enabled() is False def test_invalid_env_string(): os.environ[XRAY_ENABLED_KEY] = "INVALID" # Env Variable takes precedence. This is called to activate the internal check - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) - assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True + global_sdk_config.set_sdk_enabled(True) + assert global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "1.0" - aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) - assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True + global_sdk_config.set_sdk_enabled(True) + assert global_sdk_config.sdk_enabled() is True os.environ[XRAY_ENABLED_KEY] = "1-.0" - aws_xray_sdk.global_sdk_config.set_sdk_enabled(False) - assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True + global_sdk_config.set_sdk_enabled(False) + assert global_sdk_config.sdk_enabled() is True From e443268574d7f28c7907ef8e4f8e5940ab7c243b Mon Sep 17 00:00:00 2001 From: Jeffery Saeteurn Date: Wed, 6 Feb 2019 11:00:46 -0800 Subject: [PATCH 5/5] Add ability to enable/disable the SDK (#26) Revision 5 Changes: - Refactored sampler to use "from ... import ..." for the global sdk config module. - global sdk config logs and defaults to true instead of throwing an exception when an invalid parameter is passed into the method. --- aws_xray_sdk/core/sampling/sampler.py | 6 +++--- aws_xray_sdk/sdk_config.py | 12 +++--------- tests/test_recorder.py | 1 - 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/aws_xray_sdk/core/sampling/sampler.py b/aws_xray_sdk/core/sampling/sampler.py index db5c4614..8c09d59b 100644 --- a/aws_xray_sdk/core/sampling/sampler.py +++ b/aws_xray_sdk/core/sampling/sampler.py @@ -9,7 +9,7 @@ from .target_poller import TargetPoller from .connector import ServiceConnector from .reservoir import ReservoirDecision -import aws_xray_sdk +from aws_xray_sdk import global_sdk_config log = logging.getLogger(__name__) @@ -38,7 +38,7 @@ def start(self): Start rule poller and target poller once X-Ray daemon address and context manager is in place. """ - if not aws_xray_sdk.global_sdk_config.sdk_enabled(): + if not global_sdk_config.sdk_enabled(): return with self._lock: @@ -55,7 +55,7 @@ def should_trace(self, sampling_req=None): All optional arguments are extracted from incoming requests by X-Ray middleware to perform path based sampling. """ - if not aws_xray_sdk.global_sdk_config.sdk_enabled(): + if not global_sdk_config.sdk_enabled(): return False if not self._started: diff --git a/aws_xray_sdk/sdk_config.py b/aws_xray_sdk/sdk_config.py index 1228093b..350ad5fa 100644 --- a/aws_xray_sdk/sdk_config.py +++ b/aws_xray_sdk/sdk_config.py @@ -1,11 +1,7 @@ import os +import logging - -class InvalidParameterTypeException(Exception): - """ - Exception thrown when an invalid parameter is passed into SDKConfig.set_sdk_enabled. - """ - pass +log = logging.getLogger(__name__) class SDKConfig(object): @@ -59,6 +55,4 @@ def set_sdk_enabled(cls, value): cls.__SDK_ENABLED = value else: cls.__SDK_ENABLED = True - raise InvalidParameterTypeException( - "Invalid parameter type passed into set_sdk_enabled(). Defaulting to True..." - ) + log.warning("Invalid parameter type passed into set_sdk_enabled(). Defaulting to True...") diff --git a/tests/test_recorder.py b/tests/test_recorder.py index 0fbd4f13..76c9415c 100644 --- a/tests/test_recorder.py +++ b/tests/test_recorder.py @@ -11,7 +11,6 @@ from aws_xray_sdk.core.models.dummy_entities import DummySegment, DummySubsegment xray_recorder = get_new_stubbed_recorder() -XRAY_ENABLED_KEY = global_sdk_config.XRAY_ENABLED_KEY @pytest.fixture(autouse=True)