@@ -65,7 +65,7 @@ def sentry_init_error(*args, **kwargs):
6565
6666def _wrap_handler (handler ):
6767 # type: (F) -> F
68- def sentry_handler (aws_event , context , * args , ** kwargs ):
68+ def sentry_handler (aws_event , aws_context , * args , ** kwargs ):
6969 # type: (Any, Any, *Any, **Any) -> Any
7070
7171 # Per https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html,
@@ -94,21 +94,23 @@ def sentry_handler(aws_event, context, *args, **kwargs):
9494 hub = Hub .current
9595 integration = hub .get_integration (AwsLambdaIntegration )
9696 if integration is None :
97- return handler (aws_event , context , * args , ** kwargs )
97+ return handler (aws_event , aws_context , * args , ** kwargs )
9898
9999 # If an integration is there, a client has to be there.
100100 client = hub .client # type: Any
101- configured_time = context .get_remaining_time_in_millis ()
101+ configured_time = aws_context .get_remaining_time_in_millis ()
102102
103103 with hub .push_scope () as scope :
104104 with capture_internal_exceptions ():
105105 scope .clear_breadcrumbs ()
106106 scope .add_event_processor (
107107 _make_request_event_processor (
108- request_data , context , configured_time
108+ request_data , aws_context , configured_time
109109 )
110110 )
111- scope .set_tag ("aws_region" , context .invoked_function_arn .split (":" )[3 ])
111+ scope .set_tag (
112+ "aws_region" , aws_context .invoked_function_arn .split (":" )[3 ]
113+ )
112114 if batch_size > 1 :
113115 scope .set_tag ("batch_request" , True )
114116 scope .set_tag ("batch_size" , batch_size )
@@ -134,11 +136,17 @@ def sentry_handler(aws_event, context, *args, **kwargs):
134136
135137 headers = request_data .get ("headers" , {})
136138 transaction = Transaction .continue_from_headers (
137- headers , op = "serverless.function" , name = context .function_name
139+ headers , op = "serverless.function" , name = aws_context .function_name
138140 )
139- with hub .start_transaction (transaction ):
141+ with hub .start_transaction (
142+ transaction ,
143+ custom_sampling_context = {
144+ "aws_event" : aws_event ,
145+ "aws_context" : aws_context ,
146+ },
147+ ):
140148 try :
141- return handler (aws_event , context , * args , ** kwargs )
149+ return handler (aws_event , aws_context , * args , ** kwargs )
142150 except Exception :
143151 exc_info = sys .exc_info ()
144152 sentry_event , hint = event_from_exception (
@@ -177,23 +185,8 @@ def __init__(self, timeout_warning=False):
177185 def setup_once ():
178186 # type: () -> None
179187
180- # Python 2.7: Everything is in `__main__`.
181- #
182- # Python 3.7: If the bootstrap module is *already imported*, it is the
183- # one we actually want to use (no idea what's in __main__)
184- #
185- # On Python 3.8 bootstrap is also importable, but will be the same file
186- # as __main__ imported under a different name:
187- #
188- # sys.modules['__main__'].__file__ == sys.modules['bootstrap'].__file__
189- # sys.modules['__main__'] is not sys.modules['bootstrap']
190- #
191- # Such a setup would then make all monkeypatches useless.
192- if "bootstrap" in sys .modules :
193- lambda_bootstrap = sys .modules ["bootstrap" ] # type: Any
194- elif "__main__" in sys .modules :
195- lambda_bootstrap = sys .modules ["__main__" ]
196- else :
188+ lambda_bootstrap = get_lambda_bootstrap ()
189+ if not lambda_bootstrap :
197190 logger .warning (
198191 "Not running in AWS Lambda environment, "
199192 "AwsLambdaIntegration disabled (could not find bootstrap module)"
@@ -280,6 +273,29 @@ def inner(*args, **kwargs):
280273 )
281274
282275
276+ def get_lambda_bootstrap ():
277+ # type: () -> Optional[Any]
278+
279+ # Python 2.7: Everything is in `__main__`.
280+ #
281+ # Python 3.7: If the bootstrap module is *already imported*, it is the
282+ # one we actually want to use (no idea what's in __main__)
283+ #
284+ # On Python 3.8 bootstrap is also importable, but will be the same file
285+ # as __main__ imported under a different name:
286+ #
287+ # sys.modules['__main__'].__file__ == sys.modules['bootstrap'].__file__
288+ # sys.modules['__main__'] is not sys.modules['bootstrap']
289+ #
290+ # Such a setup would then make all monkeypatches useless.
291+ if "bootstrap" in sys .modules :
292+ return sys .modules ["bootstrap" ]
293+ elif "__main__" in sys .modules :
294+ return sys .modules ["__main__" ]
295+ else :
296+ return None
297+
298+
283299def _make_request_event_processor (aws_event , aws_context , configured_timeout ):
284300 # type: (Any, Any, Any) -> EventProcessor
285301 start_time = datetime .utcnow ()
0 commit comments