@@ -92,6 +92,17 @@ def sentry_handle_http_request(request_handler, *args, **kwargs):
9292 return old_handle_http_request(request_handler, *args, **kwargs)
9393
9494 lambda_bootstrap.handle_http_request = sentry_handle_http_request
95+
96+ # Patch to_json to drain the queue. This should work even when the
97+ # SDK is initialized inside of the handler
98+
99+ old_to_json = lambda_bootstrap.to_json
100+
101+ def sentry_to_json(*args, **kwargs):
102+ _drain_queue()
103+ return old_to_json(*args, **kwargs)
104+
105+ lambda_bootstrap.to_json = sentry_to_json
95106 else:
96107 old_handle_event_request = lambda_bootstrap.handle_event_request
97108
@@ -105,16 +116,22 @@ def sentry_handle_event_request(
105116
106117 lambda_bootstrap.handle_event_request = sentry_handle_event_request
107118
108- # This is the only function that is called in all Python environments
109- # at the end of the request/response lifecycle. It is the only way to
110- # do it in the Python 3.7 env.
111- old_to_json = lambda_bootstrap.to_json
119+ # Patch the runtime client to drain the queue. This should work
120+ # even when the SDK is initialized inside of the handler
112121
113- def sentry_to_json(*args, **kwargs):
114- _drain_queue()
115- return old_to_json(*args, **kwargs)
122+ def _wrap_post_function(f):
123+ def inner(*args, **kwargs):
124+ _drain_queue()
125+ return f(*args, **kwargs)
116126
117- lambda_bootstrap.to_json = sentry_to_json
127+ return inner
128+
129+ lambda_bootstrap.LambdaRuntimeClient.post_invocation_result = _wrap_post_function(
130+ lambda_bootstrap.LambdaRuntimeClient.post_invocation_result
131+ )
132+ lambda_bootstrap.LambdaRuntimeClient.post_invocation_error = _wrap_post_function(
133+ lambda_bootstrap.LambdaRuntimeClient.post_invocation_error
134+ )
118135
119136
120137def _make_request_event_processor(aws_event, aws_context):
0 commit comments