Skip to content

Commit 0d92dd7

Browse files
committed
Address CR comments
1 parent 4d7da81 commit 0d92dd7

2 files changed

Lines changed: 47 additions & 73 deletions

File tree

sentry_sdk/integrations/bottle.py

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,9 @@ def _patched_handle(self: "Bottle", environ: "Dict[str, Any]") -> "Any":
106106
if has_span_streaming_enabled(sentry_sdk.get_client().options):
107107
res = old_handle(self, environ)
108108

109-
try:
110-
if integration.transaction_style == "url":
111-
name = bottle_request.route.rule or "bottle"
112-
else:
113-
name = (
114-
bottle_request.route.name
115-
or transaction_from_function(bottle_request.route.callback)
116-
or "bottle"
117-
)
118-
119-
sentry_sdk.get_current_scope().set_transaction_name(
120-
name,
121-
source=SEGMENT_SOURCE_FOR_STYLE[integration.transaction_style],
122-
)
123-
except RuntimeError:
124-
pass
109+
_set_segment_name_and_source(
110+
transaction_style=integration.transaction_style
111+
)
125112

126113
else:
127114
res = old_handle(self, environ)
@@ -143,33 +130,17 @@ def patched_make_callback(
143130
return prepared_callback
144131

145132
def wrapped_callback(*args: object, **kwargs: object) -> "Any":
146-
if has_span_streaming_enabled(sentry_sdk.get_client().options):
147-
with sentry_sdk.traces.start_span(name="bottle"):
148-
try:
149-
res = prepared_callback(*args, **kwargs)
150-
except Exception as exception:
151-
_capture_exception(exception, handled=False)
152-
raise exception
153-
154-
if (
155-
isinstance(res, HTTPResponse)
156-
and res.status_code
157-
in integration.failed_request_status_codes
158-
):
159-
_capture_exception(res, handled=True)
160-
161-
else:
162-
try:
163-
res = prepared_callback(*args, **kwargs)
164-
except Exception as exception:
165-
_capture_exception(exception, handled=False)
166-
raise exception
167-
168-
if (
169-
isinstance(res, HTTPResponse)
170-
and res.status_code in integration.failed_request_status_codes
171-
):
172-
_capture_exception(res, handled=True)
133+
try:
134+
res = prepared_callback(*args, **kwargs)
135+
except Exception as exception:
136+
_capture_exception(exception, handled=False)
137+
raise exception
138+
139+
if (
140+
isinstance(res, HTTPResponse)
141+
and res.status_code in integration.failed_request_status_codes
142+
):
143+
_capture_exception(res, handled=True)
173144

174145
return res
175146

@@ -203,6 +174,25 @@ def size_of_file(self, file: "FileUpload") -> int:
203174
return file.content_length
204175

205176

177+
def _set_segment_name_and_source(transaction_style: str) -> None:
178+
try:
179+
if transaction_style == "url":
180+
name = bottle_request.route.rule or "bottle request"
181+
else:
182+
name = (
183+
bottle_request.route.name
184+
or transaction_from_function(bottle_request.route.callback)
185+
or "bottle request"
186+
)
187+
188+
sentry_sdk.get_current_scope().set_transaction_name(
189+
name,
190+
source=SEGMENT_SOURCE_FOR_STYLE[transaction_style],
191+
)
192+
except RuntimeError:
193+
pass
194+
195+
206196
def _set_transaction_name_and_source(
207197
event: "Event", transaction_style: str, request: "Any"
208198
) -> None:

tests/integrations/bottle/test_bottle.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,9 @@ def hi():
593593
sentry_sdk.flush()
594594

595595
spans = [item.payload for item in items]
596-
assert len(spans) == 2
596+
assert len(spans) == 1
597597

598-
callback_span = spans[0]
599-
segment = spans[1]
598+
segment = spans[0]
600599

601600
# Segment span (root, created by WSGI middleware)
602601
assert segment["is_segment"] is True
@@ -608,11 +607,6 @@ def hi():
608607
assert segment["attributes"]["http.response.status_code"] == 200
609608
assert segment["name"].endswith("hi")
610609

611-
# Callback span (created by wrapped_callback, child of segment)
612-
assert callback_span["is_segment"] is False
613-
assert callback_span["parent_span_id"] == segment["span_id"]
614-
assert callback_span["name"] == "bottle"
615-
616610

617611
@pytest.mark.parametrize(
618612
"url,transaction_style,expected_name,expected_source",
@@ -658,13 +652,11 @@ def named_hi():
658652
sentry_sdk.flush()
659653

660654
spans = [item.payload for item in items]
661-
assert len(spans) == 2
655+
assert len(spans) == 1
662656

663-
callback_span = spans[0]
664-
segment = spans[1]
657+
segment = spans[0]
665658

666659
assert segment["is_segment"] is True
667-
assert callback_span["parent_span_id"] == segment["span_id"]
668660

669661
assert segment["name"].endswith(expected_name)
670662
assert segment["attributes"]["sentry.span.source"] == expected_source
@@ -695,27 +687,23 @@ def error():
695687
events = [item.payload for item in items if item.type == "event"]
696688
spans = [item.payload for item in items if item.type == "span"]
697689
assert len(events) == 1
698-
assert len(spans) == 2
690+
assert len(spans) == 1
699691

700692
error_event = events[0]
701-
callback_span = spans[0]
702-
segment = spans[1]
693+
segment = spans[0]
703694

704-
# All share the same trace
705-
assert callback_span["trace_id"] == error_event["contexts"]["trace"]["trace_id"]
695+
# Confirm the same trace is shared
706696
assert segment["trace_id"] == error_event["contexts"]["trace"]["trace_id"]
707697

708698
# Span hierarchy
709699
assert segment["is_segment"] is True
710700
assert "parent_span_id" not in segment
711-
assert callback_span["parent_span_id"] == segment["span_id"]
712701

713-
# Error event span_id points to the callback span (where the exception was raised)
714-
assert error_event["contexts"]["trace"]["span_id"] == callback_span["span_id"]
702+
# Error event span_id points to the segment span (where the exception was raised)
703+
assert error_event["contexts"]["trace"]["span_id"] == segment["span_id"]
715704

716-
# Span statuses
705+
# Span status
717706
assert segment["status"] == "error"
718-
assert callback_span["status"] == "error"
719707

720708
# Bottle mechanism on the error event
721709
assert error_event["exception"]["values"][0]["mechanism"]["type"] == "bottle"
@@ -755,13 +743,11 @@ def handle():
755743
sentry_sdk.flush()
756744

757745
spans = [item.payload for item in items]
758-
assert len(spans) == 2
746+
assert len(spans) == 1
759747

760-
callback_span = spans[0]
761-
segment = spans[1]
748+
segment = spans[0]
762749

763750
assert segment["is_segment"] is True
764-
assert callback_span["parent_span_id"] == segment["span_id"]
765751

766752
assert segment["status"] == expected_span_status
767753
assert segment["attributes"]["http.response.status_code"] == status_code
@@ -809,13 +795,11 @@ def handle():
809795

810796
events = [item.payload for item in items if item.type == "event"]
811797
spans = [item.payload for item in items if item.type == "span"]
812-
assert len(spans) == 2
798+
assert len(spans) == 1
813799

814-
callback_span = spans[0]
815-
segment = spans[1]
800+
segment = spans[0]
816801

817802
assert segment["is_segment"] is True
818-
assert callback_span["parent_span_id"] == segment["span_id"]
819803

820804
if should_capture:
821805
assert len(events) == 1

0 commit comments

Comments
 (0)