|
11 | 11 | CLOUD_PLATFORM, |
12 | 12 | CLOUD_PROVIDER, |
13 | 13 | ) |
14 | | -from sentry_sdk.traces import SegmentSource |
| 14 | +from sentry_sdk.traces import SegmentSource, SpanStatus |
15 | 15 | from sentry_sdk.tracing import TransactionSource |
16 | 16 | from sentry_sdk.tracing_utils import has_span_streaming_enabled |
17 | 17 | from sentry_sdk.utils import ( |
@@ -51,23 +51,27 @@ def __call__(self, event: "Any", context: "Any") -> "Any": |
51 | 51 | ) |
52 | 52 |
|
53 | 53 | if has_span_streaming_enabled(client.options): |
54 | | - with sentry_sdk.traces.start_span( |
| 54 | + span = sentry_sdk.traces.start_span( |
55 | 55 | name=context.function_name, |
56 | 56 | parent_span=None, |
57 | 57 | attributes=_get_lambda_span_attributes(context), |
58 | | - ): |
59 | | - try: |
60 | | - return ChaliceEventSourceHandler.__call__(self, event, context) |
61 | | - except Exception: |
62 | | - exc_info = sys.exc_info() |
63 | | - sentry_event, hint = event_from_exception( |
64 | | - exc_info, |
65 | | - client_options=client.options, |
66 | | - mechanism={"type": "chalice", "handled": False}, |
67 | | - ) |
68 | | - sentry_sdk.capture_event(sentry_event, hint=hint) |
69 | | - client.flush() |
70 | | - reraise(*exc_info) |
| 58 | + ) |
| 59 | + try: |
| 60 | + return ChaliceEventSourceHandler.__call__(self, event, context) |
| 61 | + except Exception: |
| 62 | + exc_info = sys.exc_info() |
| 63 | + span.status = SpanStatus.ERROR.value |
| 64 | + sentry_event, hint = event_from_exception( |
| 65 | + exc_info, |
| 66 | + client_options=client.options, |
| 67 | + mechanism={"type": "chalice", "handled": False}, |
| 68 | + ) |
| 69 | + sentry_sdk.capture_event(sentry_event, hint=hint) |
| 70 | + reraise(*exc_info) |
| 71 | + finally: |
| 72 | + span.end() |
| 73 | + client.flush() |
| 74 | + |
71 | 75 | else: |
72 | 76 | try: |
73 | 77 | return ChaliceEventSourceHandler.__call__(self, event, context) |
@@ -115,29 +119,32 @@ def wrapped_view_function(**function_args: "Any") -> "Any": |
115 | 119 | if "method" in request_dict: |
116 | 120 | additional_attrs["http.request.method"] = request_dict["method"] |
117 | 121 |
|
118 | | - with sentry_sdk.traces.start_span( |
| 122 | + span = sentry_sdk.traces.start_span( |
119 | 123 | name=aws_context.function_name, |
120 | 124 | parent_span=None, |
121 | 125 | attributes={ |
122 | 126 | **_get_lambda_span_attributes(aws_context), |
123 | 127 | **header_attrs, |
124 | 128 | **additional_attrs, |
125 | 129 | }, |
126 | | - ): |
127 | | - try: |
128 | | - return view_function(**function_args) |
129 | | - except Exception as exc: |
130 | | - if isinstance(exc, ChaliceViewError): |
131 | | - raise |
132 | | - exc_info = sys.exc_info() |
133 | | - sentry_event, hint = event_from_exception( |
134 | | - exc_info, |
135 | | - client_options=client.options, |
136 | | - mechanism={"type": "chalice", "handled": False}, |
137 | | - ) |
138 | | - sentry_sdk.capture_event(sentry_event, hint=hint) |
139 | | - client.flush() |
| 130 | + ) |
| 131 | + try: |
| 132 | + return view_function(**function_args) |
| 133 | + except Exception as exc: |
| 134 | + if isinstance(exc, ChaliceViewError): |
140 | 135 | raise |
| 136 | + exc_info = sys.exc_info() |
| 137 | + span.status = SpanStatus.ERROR.value |
| 138 | + sentry_event, hint = event_from_exception( |
| 139 | + exc_info, |
| 140 | + client_options=client.options, |
| 141 | + mechanism={"type": "chalice", "handled": False}, |
| 142 | + ) |
| 143 | + sentry_sdk.capture_event(sentry_event, hint=hint) |
| 144 | + raise |
| 145 | + finally: |
| 146 | + span.end() |
| 147 | + client.flush() |
141 | 148 | else: |
142 | 149 | scope.set_transaction_name( |
143 | 150 | app.lambda_context.function_name, |
|
0 commit comments