Skip to content

Commit 65232d2

Browse files
committed
Address code review comments.
Removes the creation of new spans but instead updates the segment of the current span should one be present (this happens if the AWS Lambda integration is also enabled and would be what creates the initial span).
1 parent e658841 commit 65232d2

2 files changed

Lines changed: 50 additions & 353 deletions

File tree

sentry_sdk/integrations/chalice.py

Lines changed: 50 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -55,62 +55,18 @@ def __call__(self, event: "Any", context: "Any") -> "Any":
5555
_make_request_event_processor(event, context, configured_time)
5656
)
5757

58-
if has_span_streaming_enabled(client.options):
59-
current_span = get_current_span()
60-
if type(current_span) is StreamedSpan:
61-
# A segment already exists (created by the AWS Lambda
62-
# integration), so decorate it with Chalice attributes
63-
# instead of creating a duplicate root segment. The AWS
64-
# Lambda integration owns the span lifecycle (end + flush).
65-
segment = current_span._segment
66-
segment.set_attributes(_get_lambda_span_attributes(context))
67-
try:
68-
return ChaliceEventSourceHandler.__call__(self, event, context)
69-
except Exception:
70-
exc_info = sys.exc_info()
71-
segment.status = SpanStatus.ERROR.value
72-
sentry_event, hint = event_from_exception(
73-
exc_info,
74-
client_options=client.options,
75-
mechanism={"type": "chalice", "handled": False},
76-
)
77-
sentry_sdk.capture_event(sentry_event, hint=hint)
78-
reraise(*exc_info)
79-
else:
80-
span = sentry_sdk.traces.start_span(
81-
name=context.function_name,
82-
parent_span=None,
83-
attributes=_get_lambda_span_attributes(context),
84-
)
85-
try:
86-
return ChaliceEventSourceHandler.__call__(self, event, context)
87-
except Exception:
88-
exc_info = sys.exc_info()
89-
span.status = SpanStatus.ERROR.value
90-
sentry_event, hint = event_from_exception(
91-
exc_info,
92-
client_options=client.options,
93-
mechanism={"type": "chalice", "handled": False},
94-
)
95-
sentry_sdk.capture_event(sentry_event, hint=hint)
96-
reraise(*exc_info)
97-
finally:
98-
span.end()
99-
client.flush()
100-
101-
else:
102-
try:
103-
return ChaliceEventSourceHandler.__call__(self, event, context)
104-
except Exception:
105-
exc_info = sys.exc_info()
106-
sentry_event, hint = event_from_exception(
107-
exc_info,
108-
client_options=client.options,
109-
mechanism={"type": "chalice", "handled": False},
110-
)
111-
sentry_sdk.capture_event(sentry_event, hint=hint)
112-
client.flush()
113-
reraise(*exc_info)
58+
try:
59+
return ChaliceEventSourceHandler.__call__(self, event, context)
60+
except Exception:
61+
exc_info = sys.exc_info()
62+
sentry_event, hint = event_from_exception(
63+
exc_info,
64+
client_options=client.options,
65+
mechanism={"type": "chalice", "handled": False},
66+
)
67+
sentry_sdk.capture_event(sentry_event, hint=hint)
68+
client.flush()
69+
reraise(*exc_info)
11470

11571

11672
def _get_view_function_response(
@@ -131,74 +87,52 @@ def wrapped_view_function(**function_args: "Any") -> "Any":
13187
)
13288

13389
if has_span_streaming_enabled(client.options):
134-
aws_context = app.lambda_context
135-
request_dict = app.current_request.to_dict()
136-
headers = request_dict.get("headers", {})
137-
138-
header_attrs: "Dict[str, Any]" = {}
139-
for header, value in _filter_headers(
140-
headers, use_annotated_value=False
141-
).items():
142-
header_attrs[f"http.request.header.{header.lower()}"] = value
143-
144-
additional_attrs: "Dict[str, Any]" = {}
145-
if "method" in request_dict:
146-
additional_attrs["http.request.method"] = request_dict["method"]
147-
148-
attributes = {
149-
**_get_lambda_span_attributes(aws_context),
150-
**header_attrs,
151-
**additional_attrs,
152-
}
153-
15490
current_span = get_current_span()
91+
segment = None
15592
if type(current_span) is StreamedSpan:
15693
# A segment already exists (created by the AWS Lambda
15794
# integration), so decorate it with Chalice attributes
158-
# instead of creating a duplicate root segment. The AWS
159-
# Lambda integration owns the span lifecycle (end + flush),
160-
# but Chalice converts unhandled view exceptions into 500
161-
# responses, so the error must be captured here.
95+
# The AWS Lambda integration owns the span lifecycle
96+
# (end + flush), but Chalice converts unhandled view exceptions
97+
# into 500 responses, so the error must be captured here.
98+
aws_context = app.lambda_context
99+
request_dict = app.current_request.to_dict()
100+
headers = request_dict.get("headers", {})
101+
102+
header_attrs: "Dict[str, Any]" = {}
103+
for header, value in _filter_headers(
104+
headers, use_annotated_value=False
105+
).items():
106+
header_attrs[f"http.request.header.{header.lower()}"] = value
107+
108+
additional_attrs: "Dict[str, Any]" = {}
109+
if "method" in request_dict:
110+
additional_attrs["http.request.method"] = request_dict["method"]
111+
112+
attributes = {
113+
**_get_lambda_span_attributes(aws_context),
114+
**header_attrs,
115+
**additional_attrs,
116+
}
117+
162118
segment = current_span._segment
163119
segment.set_attributes(attributes)
164-
try:
165-
return view_function(**function_args)
166-
except Exception as exc:
167-
if isinstance(exc, ChaliceViewError):
168-
raise
169-
exc_info = sys.exc_info()
170-
segment.status = SpanStatus.ERROR.value
171-
sentry_event, hint = event_from_exception(
172-
exc_info,
173-
client_options=client.options,
174-
mechanism={"type": "chalice", "handled": False},
175-
)
176-
sentry_sdk.capture_event(sentry_event, hint=hint)
120+
121+
try:
122+
return view_function(**function_args)
123+
except Exception as exc:
124+
if isinstance(exc, ChaliceViewError):
177125
raise
178-
else:
179-
sentry_sdk.traces.continue_trace(headers)
180-
span = sentry_sdk.traces.start_span(
181-
name=aws_context.function_name,
182-
parent_span=None,
183-
attributes=attributes,
126+
exc_info = sys.exc_info()
127+
if segment:
128+
segment.status = SpanStatus.ERROR.value
129+
sentry_event, hint = event_from_exception(
130+
exc_info,
131+
client_options=client.options,
132+
mechanism={"type": "chalice", "handled": False},
184133
)
185-
try:
186-
return view_function(**function_args)
187-
except Exception as exc:
188-
if isinstance(exc, ChaliceViewError):
189-
raise
190-
exc_info = sys.exc_info()
191-
span.status = SpanStatus.ERROR.value
192-
sentry_event, hint = event_from_exception(
193-
exc_info,
194-
client_options=client.options,
195-
mechanism={"type": "chalice", "handled": False},
196-
)
197-
sentry_sdk.capture_event(sentry_event, hint=hint)
198-
raise
199-
finally:
200-
span.end()
201-
client.flush()
134+
sentry_sdk.capture_event(sentry_event, hint=hint)
135+
raise
202136
else:
203137
scope.set_transaction_name(
204138
app.lambda_context.function_name,

0 commit comments

Comments
 (0)