|
| 1 | +import sys |
| 2 | + |
| 3 | +from sentry_sdk._compat import reraise |
| 4 | +from sentry_sdk.hub import Hub |
| 5 | +from sentry_sdk.integrations import Integration |
| 6 | +from sentry_sdk.integrations.aws_lambda import _make_request_event_processor |
| 7 | +from sentry_sdk.utils import ( |
| 8 | + capture_internal_exceptions, |
| 9 | + event_from_exception, |
| 10 | +) |
| 11 | +from sentry_sdk._types import MYPY |
| 12 | +from sentry_sdk._functools import wraps |
| 13 | + |
| 14 | +import chalice # type: ignore |
| 15 | +from chalice import Chalice, ChaliceViewError |
| 16 | +from chalice.app import EventSourceHandler as ChaliceEventSourceHandler # type: ignore |
| 17 | + |
| 18 | +if MYPY: |
| 19 | + from typing import Any |
| 20 | + from typing import TypeVar |
| 21 | + from typing import Callable |
| 22 | + |
| 23 | + F = TypeVar("F", bound=Callable[..., Any]) |
| 24 | + |
| 25 | + |
| 26 | +class EventSourceHandler(ChaliceEventSourceHandler): # type: ignore |
| 27 | + def __call__(self, event, context): |
| 28 | + # type: (Any, Any) -> Any |
| 29 | + hub = Hub.current |
| 30 | + client = hub.client # type: Any |
| 31 | + |
| 32 | + with hub.push_scope() as scope: |
| 33 | + with capture_internal_exceptions(): |
| 34 | + configured_time = context.get_remaining_time_in_millis() |
| 35 | + scope.add_event_processor( |
| 36 | + _make_request_event_processor(event, context, configured_time) |
| 37 | + ) |
| 38 | + try: |
| 39 | + event_obj = self.event_class(event, context) |
| 40 | + return self.func(event_obj) |
| 41 | + except Exception: |
| 42 | + exc_info = sys.exc_info() |
| 43 | + event, hint = event_from_exception( |
| 44 | + exc_info, |
| 45 | + client_options=client.options, |
| 46 | + mechanism={"type": "chalice", "handled": False}, |
| 47 | + ) |
| 48 | + hub.capture_event(event, hint=hint) |
| 49 | + hub.flush() |
| 50 | + reraise(*exc_info) |
| 51 | + |
| 52 | + |
| 53 | +def _get_view_function_response(app, view_function, function_args): |
| 54 | + # type: (Any, F, Any) -> F |
| 55 | + @wraps(view_function) |
| 56 | + def wrapped_view_function(**function_args): |
| 57 | + # type: (**Any) -> Any |
| 58 | + hub = Hub.current |
| 59 | + client = hub.client # type: Any |
| 60 | + with hub.push_scope() as scope: |
| 61 | + with capture_internal_exceptions(): |
| 62 | + configured_time = app.lambda_context.get_remaining_time_in_millis() |
| 63 | + scope.transaction = app.lambda_context.function_name |
| 64 | + scope.add_event_processor( |
| 65 | + _make_request_event_processor( |
| 66 | + app.current_request.to_dict(), |
| 67 | + app.lambda_context, |
| 68 | + configured_time, |
| 69 | + ) |
| 70 | + ) |
| 71 | + try: |
| 72 | + return view_function(**function_args) |
| 73 | + except Exception as exc: |
| 74 | + if isinstance(exc, ChaliceViewError): |
| 75 | + raise |
| 76 | + exc_info = sys.exc_info() |
| 77 | + event, hint = event_from_exception( |
| 78 | + exc_info, |
| 79 | + client_options=client.options, |
| 80 | + mechanism={"type": "chalice", "handled": False}, |
| 81 | + ) |
| 82 | + hub.capture_event(event, hint=hint) |
| 83 | + hub.flush() |
| 84 | + raise |
| 85 | + |
| 86 | + return wrapped_view_function # type: ignore |
| 87 | + |
| 88 | + |
| 89 | +class ChaliceIntegration(Integration): |
| 90 | + identifier = "chalice" |
| 91 | + |
| 92 | + @staticmethod |
| 93 | + def setup_once(): |
| 94 | + # type: () -> None |
| 95 | + old_get_view_function_response = Chalice._get_view_function_response |
| 96 | + |
| 97 | + def sentry_event_response(app, view_function, function_args): |
| 98 | + # type: (Any, F, **Any) -> Any |
| 99 | + wrapped_view_function = _get_view_function_response( |
| 100 | + app, view_function, function_args |
| 101 | + ) |
| 102 | + |
| 103 | + return old_get_view_function_response( |
| 104 | + app, wrapped_view_function, function_args |
| 105 | + ) |
| 106 | + |
| 107 | + Chalice._get_view_function_response = sentry_event_response |
| 108 | + # for everything else (like events) |
| 109 | + chalice.app.EventSourceHandler = EventSourceHandler |
0 commit comments