17
17
logger = logging .getLogger (__name__ )
18
18
19
19
aws_xray_sdk = LazyLoader (constants .XRAY_SDK_MODULE , globals (), constants .XRAY_SDK_MODULE )
20
- aws_xray_sdk .core = LazyLoader (constants .XRAY_SDK_CORE_MODULE , globals (), constants .XRAY_SDK_CORE_MODULE ) # type: ignore # noqa: E501
21
20
22
21
23
22
class Tracer :
@@ -137,7 +136,7 @@ def handler(event: dict, context: Any) -> Dict:
137
136
"""
138
137
139
138
_default_config : Dict [str , Any ] = {
140
- "service" : "service_undefined " ,
139
+ "service" : "" ,
141
140
"disabled" : False ,
142
141
"auto_patch" : True ,
143
142
"patch_modules" : None ,
@@ -156,7 +155,7 @@ def __init__(
156
155
self .__build_config (
157
156
service = service , disabled = disabled , auto_patch = auto_patch , patch_modules = patch_modules , provider = provider
158
157
)
159
- self .provider : BaseProvider = self ._config ["provider" ]
158
+ self .provider = self ._config ["provider" ]
160
159
self .disabled = self ._config ["disabled" ]
161
160
self .service = self ._config ["service" ]
162
161
self .auto_patch = self ._config ["auto_patch" ]
@@ -167,10 +166,8 @@ def __init__(
167
166
if self .auto_patch :
168
167
self .patch (modules = patch_modules )
169
168
170
- # Set the streaming threshold to 0 on the default recorder to force sending
171
- # subsegments individually, rather than batching them.
172
- # See https://github.com/awslabs/aws-lambda-powertools-python/issues/283
173
- aws_xray_sdk .core .xray_recorder .configure (streaming_threshold = 0 ) # noqa: E800
169
+ if self ._is_xray_provider ():
170
+ self ._disable_xray_trace_batching ()
174
171
175
172
def put_annotation (self , key : str , value : Union [str , numbers .Number , bool ]):
176
173
"""Adds annotation to existing segment or subsegment
@@ -239,9 +236,9 @@ def patch(self, modules: Optional[Sequence[str]] = None):
239
236
return
240
237
241
238
if modules is None :
242
- aws_xray_sdk . core .patch_all ()
239
+ self . provider .patch_all ()
243
240
else :
244
- aws_xray_sdk . core .patch (modules )
241
+ self . provider .patch (modules )
245
242
246
243
def capture_lambda_handler (
247
244
self ,
@@ -310,6 +307,9 @@ def decorate(event, context, **kwargs):
310
307
if is_cold_start :
311
308
is_cold_start = False
312
309
310
+ if self .service :
311
+ subsegment .put_annotation (key = "Service" , value = self .service )
312
+
313
313
try :
314
314
logger .debug ("Calling lambda handler" )
315
315
response = lambda_handler (event , context , ** kwargs )
@@ -743,7 +743,8 @@ def __build_config(
743
743
is_disabled = disabled if disabled is not None else self ._is_tracer_disabled ()
744
744
is_service = resolve_env_var_choice (choice = service , env = os .getenv (constants .SERVICE_NAME_ENV ))
745
745
746
- self ._config ["provider" ] = provider or self ._config ["provider" ] or aws_xray_sdk .core .xray_recorder
746
+ # Logic: Choose overridden option first, previously cached config, or default if available
747
+ self ._config ["provider" ] = provider or self ._config ["provider" ] or self ._patch_xray_provider ()
747
748
self ._config ["auto_patch" ] = auto_patch if auto_patch is not None else self ._config ["auto_patch" ]
748
749
self ._config ["service" ] = is_service or self ._config ["service" ]
749
750
self ._config ["disabled" ] = is_disabled or self ._config ["disabled" ]
@@ -752,3 +753,28 @@ def __build_config(
752
753
@classmethod
753
754
def _reset_config (cls ):
754
755
cls ._config = copy .copy (cls ._default_config )
756
+
757
+ def _patch_xray_provider (self ):
758
+ # Due to Lazy Import, we need to activate `core` attrib via import
759
+ # we also need to include `patch`, `patch_all` methods
760
+ # to ensure patch calls are done via the provider
761
+ from aws_xray_sdk .core import xray_recorder
762
+
763
+ provider = xray_recorder
764
+ provider .patch = aws_xray_sdk .core .patch
765
+ provider .patch_all = aws_xray_sdk .core .patch_all
766
+
767
+ return provider
768
+
769
+ def _disable_xray_trace_batching (self ):
770
+ """Configure X-Ray SDK to send subsegment individually over batching
771
+ Known issue: https://github.com/awslabs/aws-lambda-powertools-python/issues/283
772
+ """
773
+ if self .disabled :
774
+ logger .debug ("Tracing has been disabled, aborting streaming override" )
775
+ return
776
+
777
+ aws_xray_sdk .core .xray_recorder .configure (streaming_threshold = 0 )
778
+
779
+ def _is_xray_provider (self ):
780
+ return "aws_xray_sdk" in self .provider .__module__
0 commit comments