Skip to content

Commit ce9b064

Browse files
committed
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.
1 parent e960164 commit ce9b064

File tree

8 files changed

+46
-35
lines changed

8 files changed

+46
-35
lines changed

aws_xray_sdk/core/recorder.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import platform
66
import time
77

8+
import aws_xray_sdk
89
from aws_xray_sdk.version import VERSION
910
from .models.segment import Segment, SegmentContextManager
1011
from .models.subsegment import Subsegment, SubsegmentContextManager
@@ -65,7 +66,6 @@ def __init__(self):
6566
self._context = Context()
6667
self._sampler = DefaultSampler()
6768

68-
self._enabled = True
6969
self._emitter = UDPEmitter()
7070
self._sampling = True
7171
self._max_trace_back = 10
@@ -79,7 +79,7 @@ def __init__(self):
7979
if type(self.sampler).__name__ == 'DefaultSampler':
8080
self.sampler.load_settings(DaemonConfig(), self.context)
8181

82-
def configure(self, enabled=None, sampling=None, plugins=None,
82+
def configure(self, sampling=None, plugins=None,
8383
context_missing=None, sampling_rules=None,
8484
daemon_address=None, service=None,
8585
context=None, emitter=None, streaming=None,
@@ -90,16 +90,6 @@ def configure(self, enabled=None, sampling=None, plugins=None,
9090
9191
Configure needs to run before patching thrid party libraries
9292
to avoid creating dangling subsegment.
93-
:param bool enabled: If not enabled, the recorder automatically
94-
generates DummySegments for every segment creation, whether
95-
through patched extensions nor middlewares, and thus
96-
not send any Segments out to the Daemon. May be set through an
97-
environmental variable, where the environmental variable will
98-
always take precedence over hardcoded configurations. The environment
99-
variable is set as a case-insensitive string boolean. If the environment
100-
variable exists but is an invalid string boolean, this enabled flag
101-
will automatically be set to true. If no enabled flag is given and the
102-
env variable is not set, then it will also default to being enabled.
10393
:param bool sampling: If sampling is enabled, every time the recorder
10494
creates a segment it decides whether to send this segment to
10595
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.
145135
by auto-capture. Lower this if a single document becomes too large.
146136
:param bool stream_sql: Whether SQL query texts should be streamed.
147137
148-
Environment variables AWS_XRAY_ENABLED, AWS_XRAY_DAEMON_ADDRESS, AWS_XRAY_CONTEXT_MISSING
138+
Environment variables AWS_XRAY_DAEMON_ADDRESS, AWS_XRAY_CONTEXT_MISSING
149139
and AWS_XRAY_TRACING_NAME respectively overrides arguments
150-
enabled, daemon_address, context_missing and service.
140+
daemon_address, context_missing and service.
151141
"""
152142

153143
if sampling is not None:
@@ -176,8 +166,6 @@ class to have your own implementation of the streaming process.
176166
self.max_trace_back = max_trace_back
177167
if stream_sql is not None:
178168
self.stream_sql = stream_sql
179-
if enabled is not None:
180-
self.enabled = enabled
181169

182170
if plugins:
183171
plugin_modules = get_plugin_modules(plugins)
@@ -236,7 +224,7 @@ def begin_segment(self, name=None, traceid=None,
236224
# To disable the recorder, we set the sampling decision to always be false.
237225
# This way, when segments are generated, they become dummy segments and are ultimately never sent.
238226
# The call to self._sampler.should_trace() is never called either so the poller threads are never started.
239-
if not self.enabled:
227+
if not aws_xray_sdk.global_sdk_config.sdk_enabled():
240228
sampling = 0
241229

242230
# we respect the input sampling decision
@@ -420,7 +408,7 @@ def record_subsegment(self, wrapped, instance, args, kwargs, name,
420408
# In the case when the SDK is disabled, we ensure that a parent segment exists, because this is usually
421409
# handled by the middleware. We generate a dummy segment as the parent segment if one doesn't exist.
422410
# This is to allow potential segment method calls to not throw exceptions in the captured method.
423-
if not self.enabled:
411+
if not aws_xray_sdk.global_sdk_config.sdk_enabled():
424412
try:
425413
self.current_segment()
426414
except SegmentNotFoundException:

aws_xray_sdk/sdk_config.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import aws_xray_sdk.core
32

43

54
class InvalidParameterTypeException(Exception):
@@ -12,8 +11,24 @@ class InvalidParameterTypeException(Exception):
1211
class SDKConfig(object):
1312
"""
1413
Global Configuration Class that defines SDK-level configuration properties.
15-
It is recommended to only use the recorder to set this configuration's enabled
16-
flag to maintain thread safety.
14+
15+
Enabling/Disabling the SDK:
16+
By default, the SDK is enabled unless if an environment variable AWS_XRAY_SDK_ENABLED
17+
is set. If it is set, it needs to be a valid string boolean, otherwise, it will default
18+
to true. If the environment variable is set, all calls to set_sdk_enabled() will
19+
prioritize the value of the environment variable.
20+
Disabling the SDK affects the recorder, patcher, and middlewares in the following ways:
21+
For the recorder, disabling automatically generates DummySegments for subsequent segments
22+
and DummySubsegments for subsegments created and thus not send any traces to the daemon.
23+
For the patcher, module patching will automatically be disabled. The SDK must be disabled
24+
before calling patcher.patch() method in order for this to function properly.
25+
For the middleware, no modification is made on them, but since the recorder automatically
26+
generates DummySegments for all subsequent calls, they will not generate segments/subsegments
27+
to be sent.
28+
29+
Environment variables:
30+
"AWS_XRAY_SDK_ENABLED" - If set to 'false' disables the SDK and causes the explained above
31+
to occur.
1732
"""
1833
XRAY_ENABLED_KEY = 'AWS_XRAY_SDK_ENABLED'
1934
__SDK_ENABLED = str(os.getenv(XRAY_ENABLED_KEY, 'true')).lower() != 'false'
@@ -28,13 +43,13 @@ def sdk_enabled(cls):
2843
@classmethod
2944
def set_sdk_enabled(cls, value):
3045
"""
31-
Modifies the enabled flag if the "AWS_XRAY_ENABLED" environment variable is not set,
46+
Modifies the enabled flag if the "AWS_XRAY_SDK_ENABLED" environment variable is not set,
3247
otherwise, set the enabled flag to be equal to the environment variable. If the
3348
env variable is an invalid string boolean, it will default to true.
3449
3550
:param bool value: Flag to set whether the SDK is enabled or disabled.
3651
37-
Environment variables AWS_XRAY_ENABLED overrides argument value.
52+
Environment variables AWS_XRAY_SDK_ENABLED overrides argument value.
3853
"""
3954
# Environment Variables take precedence over hardcoded configurations.
4055
if cls.XRAY_ENABLED_KEY in os.environ:
@@ -47,6 +62,3 @@ def set_sdk_enabled(cls, value):
4762
raise InvalidParameterTypeException(
4863
"Invalid parameter type passed into set_sdk_enabled(). Defaulting to True..."
4964
)
50-
51-
# Modify all key paths.
52-
aws_xray_sdk.core.xray_recorder.configure(enabled=cls.__SDK_ENABLED)

tests/ext/aiohttp/test_middleware.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ def recorder(loop):
109109
patcher.start()
110110

111111
xray_recorder.clear_trace_entities()
112-
sdk_enabled_state = aws_xray_sdk.global_sdk_config.sdk_enabled()
113112
yield xray_recorder
114-
aws_xray_sdk.global_sdk_config.set_sdk_enabled(sdk_enabled_state)
113+
aws_xray_sdk.global_sdk_config.set_sdk_enabled(True)
115114
xray_recorder.clear_trace_entities()
116115
patcher.stop()
117116

tests/ext/flask/test_flask.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from flask import Flask, render_template_string
33

4+
import aws_xray_sdk
45
from aws_xray_sdk.ext.flask.middleware import XRayMiddleware
56
from aws_xray_sdk.core.context import Context
67
from aws_xray_sdk.core.models import http
@@ -51,6 +52,7 @@ def cleanup():
5152
recorder.clear_trace_entities()
5253
yield
5354
recorder.clear_trace_entities()
55+
aws_xray_sdk.global_sdk_config.set_sdk_enabled(True)
5456

5557

5658
def test_ok():
@@ -146,7 +148,7 @@ def test_sampled_response_header():
146148

147149

148150
def test_disabled_sdk():
149-
recorder.configure(enabled=False)
151+
aws_xray_sdk.global_sdk_config.set_sdk_enabled(False)
150152
path = '/ok'
151153
app.get(path)
152154
segment = recorder.emitter.pop()

tests/test_lambda_context.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import os
22

33
import aws_xray_sdk
4+
import pytest
45
from aws_xray_sdk.core import lambda_launcher
56
from aws_xray_sdk.core.models.subsegment import Subsegment
6-
from aws_xray_sdk.core.models.dummy_entities import DummySubsegment
77

88

99
TRACE_ID = '1-5759e988-bd862e3fe1be46a994272793'
@@ -12,7 +12,12 @@
1212

1313
os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] = HEADER_VAR
1414
context = lambda_launcher.LambdaContext()
15-
aws_xray_sdk.global_sdk_config.set_sdk_enabled(True)
15+
16+
17+
@pytest.fixture(autouse=True)
18+
def setup():
19+
yield
20+
aws_xray_sdk.global_sdk_config.set_sdk_enabled(True)
1621

1722

1823
def test_facade_segment_generation():

tests/test_patcher.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ def construct_ctx():
3535
"""
3636
pre_run_modules = set(module for module in sys.modules.keys())
3737

38-
aws_xray_sdk.global_sdk_config.set_sdk_enabled(True)
3938
xray_recorder.configure(service='test', sampling=False, context=Context())
4039
xray_recorder.clear_trace_entities()
4140
xray_recorder.begin_segment('name')

tests/test_recorder.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def construct_ctx():
2323
xray_recorder.clear_trace_entities()
2424
yield
2525
xray_recorder.clear_trace_entities()
26+
aws_xray_sdk.global_sdk_config.set_sdk_enabled(True)
2627

2728

2829
def test_default_runtime_context():
@@ -175,14 +176,15 @@ def test_in_segment_exception():
175176

176177

177178
def test_default_enabled():
179+
assert aws_xray_sdk.global_sdk_config.sdk_enabled()
178180
segment = xray_recorder.begin_segment('name')
179181
subsegment = xray_recorder.begin_subsegment('name')
180182
assert type(xray_recorder.current_segment()) is Segment
181183
assert type(xray_recorder.current_subsegment()) is Subsegment
182184

183185

184186
def test_disable_is_dummy():
185-
xray_recorder.configure(enabled=False)
187+
aws_xray_sdk.global_sdk_config.set_sdk_enabled(False)
186188
segment = xray_recorder.begin_segment('name')
187189
subsegment = xray_recorder.begin_subsegment('name')
188190
assert type(xray_recorder.current_segment()) is DummySegment

tests/test_sdk_config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def cleanup():
1616
yield
1717
if XRAY_ENABLED_KEY in os.environ:
1818
del os.environ[XRAY_ENABLED_KEY]
19+
aws_xray_sdk.global_sdk_config.set_sdk_enabled(True)
1920

2021

2122
def test_enable_key():
@@ -28,6 +29,7 @@ def test_default_enabled():
2829

2930
def test_env_var_precedence():
3031
os.environ[XRAY_ENABLED_KEY] = "true"
32+
# Env Variable takes precedence. This is called to activate the internal check
3133
aws_xray_sdk.global_sdk_config.set_sdk_enabled(False)
3234
assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True
3335
os.environ[XRAY_ENABLED_KEY] = "false"
@@ -46,7 +48,8 @@ def test_env_var_precedence():
4648

4749
def test_env_enable_case():
4850
os.environ[XRAY_ENABLED_KEY] = "TrUE"
49-
aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) # Env Variable takes precedence. This is called to activate the internal check
51+
# Env Variable takes precedence. This is called to activate the internal check
52+
aws_xray_sdk.global_sdk_config.set_sdk_enabled(True)
5053
assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True
5154

5255
os.environ[XRAY_ENABLED_KEY] = "true"
@@ -64,7 +67,8 @@ def test_env_enable_case():
6467

6568
def test_invalid_env_string():
6669
os.environ[XRAY_ENABLED_KEY] = "INVALID"
67-
aws_xray_sdk.global_sdk_config.set_sdk_enabled(True) # Env Variable takes precedence. This is called to activate the internal check
70+
# Env Variable takes precedence. This is called to activate the internal check
71+
aws_xray_sdk.global_sdk_config.set_sdk_enabled(True)
6872
assert aws_xray_sdk.global_sdk_config.sdk_enabled() is True
6973

7074
os.environ[XRAY_ENABLED_KEY] = "1.0"

0 commit comments

Comments
 (0)