|
12 | 12 | from sentry_sdk.integrations._wsgi_common import _filter_headers
|
13 | 13 |
|
14 | 14 |
|
| 15 | +def _wrap_handler(handler): |
| 16 | + def sentry_handler(event, context, *args, **kwargs): |
| 17 | + hub = Hub.current |
| 18 | + integration = hub.get_integration(AwsLambdaIntegration) |
| 19 | + if integration is None: |
| 20 | + return handler(event, context, *args, **kwargs) |
| 21 | + |
| 22 | + with hub.push_scope() as scope: |
| 23 | + with capture_internal_exceptions(): |
| 24 | + scope.transaction = context.function_name |
| 25 | + scope.add_event_processor(_make_request_event_processor(event, context)) |
| 26 | + |
| 27 | + try: |
| 28 | + return handler(event, context, *args, **kwargs) |
| 29 | + except Exception: |
| 30 | + exc_info = sys.exc_info() |
| 31 | + event, hint = event_from_exception( |
| 32 | + exc_info, |
| 33 | + client_options=hub.client.options, |
| 34 | + mechanism={"type": "aws_lambda", "handled": False}, |
| 35 | + ) |
| 36 | + hub.capture_event(event, hint=hint) |
| 37 | + reraise(*exc_info) |
| 38 | + |
| 39 | + return sentry_handler |
| 40 | + |
| 41 | + |
| 42 | +def _drain_queue(): |
| 43 | + with capture_internal_exceptions(): |
| 44 | + hub = Hub.current |
| 45 | + integration = hub.get_integration(AwsLambdaIntegration) |
| 46 | + if integration is not None: |
| 47 | + # Flush out the event queue before AWS kills the |
| 48 | + # process. This is not threadsafe. |
| 49 | + # make new transport with empty queue |
| 50 | + new_transport = hub.client.transport.copy() |
| 51 | + hub.client.close() |
| 52 | + hub.client.transport = new_transport |
| 53 | + |
| 54 | + |
15 | 55 | class AwsLambdaIntegration(Integration):
|
16 | 56 | identifier = "aws_lambda"
|
17 | 57 |
|
18 | 58 | @staticmethod
|
19 | 59 | def setup_once():
|
20 | 60 | import __main__ as lambda_bootstrap
|
21 | 61 |
|
22 |
| - if not hasattr(lambda_bootstrap, "make_final_handler"): |
| 62 | + pre_37 = True # Python 3.6 or 2.7 |
| 63 | + |
| 64 | + if not hasattr(lambda_bootstrap, "handle_http_request"): |
| 65 | + try: |
| 66 | + import bootstrap as lambda_bootstrap |
| 67 | + |
| 68 | + pre_37 = False # Python 3.7 |
| 69 | + except ImportError: |
| 70 | + pass |
| 71 | + |
| 72 | + if not hasattr(lambda_bootstrap, "handle_event_request"): |
23 | 73 | logger.warning(
|
24 | 74 | "Not running in AWS Lambda environment, "
|
25 | 75 | "AwsLambdaIntegration disabled"
|
26 | 76 | )
|
27 | 77 | return
|
28 | 78 |
|
29 |
| - import runtime as lambda_runtime |
| 79 | + if pre_37: |
| 80 | + old_handle_event_request = lambda_bootstrap.handle_event_request |
30 | 81 |
|
31 |
| - old_make_final_handler = lambda_bootstrap.make_final_handler |
| 82 | + def sentry_handle_event_request(request_handler, *args, **kwargs): |
| 83 | + request_handler = _wrap_handler(request_handler) |
| 84 | + return old_handle_event_request(request_handler, *args, **kwargs) |
32 | 85 |
|
33 |
| - def sentry_make_final_handler(*args, **kwargs): |
34 |
| - handler = old_make_final_handler(*args, **kwargs) |
| 86 | + lambda_bootstrap.handle_event_request = sentry_handle_event_request |
35 | 87 |
|
36 |
| - def sentry_handler(event, context, *args, **kwargs): |
37 |
| - hub = Hub.current |
38 |
| - integration = hub.get_integration(AwsLambdaIntegration) |
39 |
| - if integration is None: |
40 |
| - return handler(event, context, *args, **kwargs) |
| 88 | + old_handle_http_request = lambda_bootstrap.handle_http_request |
41 | 89 |
|
42 |
| - with hub.push_scope() as scope: |
43 |
| - with capture_internal_exceptions(): |
44 |
| - scope.transaction = context.function_name |
45 |
| - scope.add_event_processor( |
46 |
| - _make_request_event_processor(event, context) |
47 |
| - ) |
| 90 | + def sentry_handle_http_request(request_handler, *args, **kwargs): |
| 91 | + request_handler = _wrap_handler(request_handler) |
| 92 | + return old_handle_http_request(request_handler, *args, **kwargs) |
48 | 93 |
|
49 |
| - try: |
50 |
| - return handler(event, context, *args, **kwargs) |
51 |
| - except Exception: |
52 |
| - exc_info = sys.exc_info() |
53 |
| - event, hint = event_from_exception( |
54 |
| - exc_info, |
55 |
| - client_options=hub.client.options, |
56 |
| - mechanism={"type": "aws_lambda", "handled": False}, |
57 |
| - ) |
58 |
| - hub.capture_event(event, hint=hint) |
59 |
| - reraise(*exc_info) |
| 94 | + lambda_bootstrap.handle_http_request = sentry_handle_http_request |
| 95 | + else: |
| 96 | + old_handle_event_request = lambda_bootstrap.handle_event_request |
60 | 97 |
|
61 |
| - return sentry_handler |
| 98 | + def sentry_handle_event_request( |
| 99 | + lambda_runtime_client, request_handler, *args, **kwargs |
| 100 | + ): |
| 101 | + request_handler = _wrap_handler(request_handler) |
| 102 | + return old_handle_event_request( |
| 103 | + lambda_runtime_client, request_handler, *args, **kwargs |
| 104 | + ) |
62 | 105 |
|
63 |
| - lambda_bootstrap.make_final_handler = sentry_make_final_handler |
| 106 | + lambda_bootstrap.handle_event_request = sentry_handle_event_request |
64 | 107 |
|
65 |
| - old_report_done = lambda_runtime.report_done |
| 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 |
66 | 112 |
|
67 |
| - def sentry_report_done(*args, **kwargs): |
68 |
| - with capture_internal_exceptions(): |
69 |
| - hub = Hub.current |
70 |
| - integration = hub.get_integration(AwsLambdaIntegration) |
71 |
| - if integration is not None: |
72 |
| - # Flush out the event queue before AWS kills the |
73 |
| - # process. This is not threadsafe. |
74 |
| - # make new transport with empty queue |
75 |
| - new_transport = hub.client.transport.copy() |
76 |
| - hub.client.close() |
77 |
| - hub.client.transport = new_transport |
78 |
| - |
79 |
| - return old_report_done(*args, **kwargs) |
80 |
| - |
81 |
| - lambda_runtime.report_done = sentry_report_done |
| 113 | + def sentry_to_json(*args, **kwargs): |
| 114 | + _drain_queue() |
| 115 | + return old_to_json(*args, **kwargs) |
| 116 | + |
| 117 | + lambda_bootstrap.to_json = sentry_to_json |
82 | 118 |
|
83 | 119 |
|
84 | 120 | def _make_request_event_processor(aws_event, aws_context):
|
|
0 commit comments