Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 11 additions & 39 deletions sentry_sdk/integrations/bottle.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
)
from sentry_sdk.integrations._wsgi_common import RequestExtractor
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
from sentry_sdk.traces import SOURCE_FOR_STYLE as SEGMENT_SOURCE_FOR_STYLE
from sentry_sdk.traces import SegmentNameSource
from sentry_sdk.utils import (
capture_internal_exceptions,
ensure_integration_enabled,
event_from_exception,
parse_version,
transaction_from_function,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -45,27 +44,15 @@
raise DidNotEnable("Bottle not installed or incompatible")


TRANSACTION_STYLE_VALUES = ("endpoint", "url")


class BottleIntegration(Integration):
identifier = "bottle"
origin = f"auto.http.{identifier}"

transaction_style = ""

def __init__(
self,
transaction_style: str = "endpoint",
*,
failed_request_status_codes: "Set[int]" = _DEFAULT_FAILED_REQUEST_STATUS_CODES,
) -> None:
if transaction_style not in TRANSACTION_STYLE_VALUES:
raise ValueError(
"Invalid value for transaction_style: %s (must be in %s)"
% (transaction_style, TRANSACTION_STYLE_VALUES)
)
self.transaction_style = transaction_style
self.failed_request_status_codes = failed_request_status_codes

@staticmethod
Expand Down Expand Up @@ -127,9 +114,12 @@ def _patched_handle(self: "Bottle", environ: "Dict[str, Any]") -> "Any":
if route_path is not None:
server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path)

_set_segment_name_and_source(
transaction_style=integration.transaction_style
)
name = _get_transaction_name(bottle_request)
if name:
sentry_sdk.get_current_scope().set_transaction_name(
name,
source=SegmentNameSource.ROUTE,
)

return res

Expand Down Expand Up @@ -192,38 +182,20 @@ def size_of_file(self, file: "FileUpload") -> int:
return file.content_length


def _get_transaction_name(transaction_style: str, request: "Any") -> str:
def _get_transaction_name(request: "Any") -> str:
try:
if transaction_style == "url":
return request.route.rule or "bottle request"
else:
return (
request.route.name
or transaction_from_function(request.route.callback)
or "bottle request"
)
return request.route.rule or "bottle request"
except RuntimeError:
return "bottle request"


def _set_segment_name_and_source(transaction_style: str) -> None:
name = _get_transaction_name(transaction_style, bottle_request)
if name:
sentry_sdk.get_current_scope().set_transaction_name(
name,
source=SEGMENT_SOURCE_FOR_STYLE[transaction_style],
)


def _make_request_event_processor(
app: "Bottle", request: "LocalRequest", integration: "BottleIntegration"
) -> "EventProcessor":
def event_processor(event: "Event", hint: "dict[str, Any]") -> "Event":
name = _get_transaction_name(integration.transaction_style, request)
name = _get_transaction_name(request)
event["transaction"] = name
event["transaction_info"] = {
"source": SEGMENT_SOURCE_FOR_STYLE[integration.transaction_style]
}
event["transaction_info"] = {"source": SegmentNameSource.ROUTE}

with capture_internal_exceptions():
BottleRequestExtractor(request).extract_into_event(event)
Expand Down
17 changes: 7 additions & 10 deletions tests/integrations/bottle/test_bottle.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,28 +488,25 @@ def hi():
assert segment["attributes"]["sentry.origin"] == "auto.http.bottle"
assert segment["attributes"]["http.request.method"] == "GET"
assert segment["attributes"]["http.response.status_code"] == 200
assert segment["name"].endswith("hi")
assert segment["name"] == "/message"


@pytest.mark.parametrize(
"url,transaction_style,expected_name,expected_source",
"url,expected_name,expected_source",
[
("/message", "endpoint", "hi", "component"),
("/message", "url", "/message", "route"),
("/message/123456", "url", "/message/<message_id>", "route"),
("/message-named-route", "endpoint", "hi", "component"),
("/message", "/message", "route"),
("/message/123456", "/message/<message_id>", "route"),
],
)
def test_transaction_style(
def test_segment_name_and_source(
sentry_init,
capture_items,
url,
transaction_style,
expected_name,
expected_source,
):
sentry_init(
integrations=[BottleIntegration(transaction_style=transaction_style)],
integrations=[BottleIntegration()],
traces_sample_rate=1.0,
)
items = capture_items("span")
Expand Down Expand Up @@ -830,7 +827,7 @@ def test_error_event_has_transaction_name(sentry_init, capture_events):
transaction name set by the event processor, even though the inline
set_transaction_name call happens after the handler returns.
"""
sentry_init(integrations=[BottleIntegration(transaction_style="url")])
sentry_init(integrations=[BottleIntegration()])
events = capture_events()

app = Bottle()
Expand Down
Loading