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
40 changes: 7 additions & 33 deletions sentry_sdk/integrations/pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
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,
Expand Down Expand Up @@ -37,23 +37,10 @@
from sentry_sdk.utils import ExcInfo


TRANSACTION_STYLE_VALUES = ("route_name", "route_pattern")


class PyramidIntegration(Integration):
identifier = "pyramid"
origin = f"auto.http.{identifier}"

transaction_style = ""

def __init__(self, transaction_style: str = "route_name") -> 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

@staticmethod
def setup_once() -> None:
version = package_version("pyramid")
Expand Down Expand Up @@ -84,9 +71,12 @@ def sentry_patched_call_view(
SPANDATA.HTTP_ROUTE, request.matched_route.pattern
)

_set_transaction_name_and_source(
sentry_sdk.get_current_scope(), integration.transaction_style, request
)
try:
sentry_sdk.get_current_scope().set_transaction_name(
request.matched_route.pattern, source=SegmentNameSource.ROUTE
)
except Exception:
pass

scope = sentry_sdk.get_isolation_scope()

Expand Down Expand Up @@ -163,22 +153,6 @@ def _capture_exception(exc_info: "ExcInfo") -> None:
sentry_sdk.capture_event(event, hint=hint)


def _set_transaction_name_and_source(
scope: "sentry_sdk.Scope", transaction_style: str, request: "Request"
) -> None:
try:
name_for_style = {
"route_name": request.matched_route.name,
"route_pattern": request.matched_route.pattern,
}
scope.set_transaction_name(
name_for_style[transaction_style],
source=SEGMENT_SOURCE_FOR_STYLE[transaction_style],
)
except Exception:
pass


class PyramidRequestExtractor(RequestExtractor):
def url(self) -> str:
return self.request.path_url
Expand Down
17 changes: 7 additions & 10 deletions tests/integrations/pyramid/test_pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,26 @@ def hi2(request):
"method": "GET",
"url": "http://localhost/context_message/yoo",
}
assert event["transaction"] == "hi2"
assert event["transaction"] == "/context_message/{msg}"


@pytest.mark.parametrize(
"url,transaction_style,expected_transaction,expected_source",
"url,expected_transaction,expected_source",
[
("/message", "route_name", "hi", "component"),
("/message", "route_pattern", "/message", "route"),
("/message/123456", "route_name", "hi_with_id", "component"),
("/message/123456", "route_pattern", "/message/{message_id}", "route"),
("/message", "/message", "route"),
("/message/123456", "/message/{message_id}", "route"),
],
)
def test_transaction_style(
def test_segment_name_and_source(
sentry_init,
get_client,
capture_items,
url,
transaction_style,
expected_transaction,
expected_source,
):
sentry_init(
integrations=[PyramidIntegration(transaction_style=transaction_style)],
integrations=[PyramidIntegration()],
traces_sample_rate=1.0,
)

Expand Down Expand Up @@ -504,7 +501,7 @@ def tracing_error(request):
(segment,) = spans
(error_event,) = error_events

assert segment["name"] == "tracing_error"
assert segment["name"] == "/tracing-error"
assert segment["status"] == SpanStatus.ERROR
assert segment["attributes"]["sentry.origin"] == "auto.http.pyramid"

Expand Down
Loading