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
44 changes: 8 additions & 36 deletions sentry_sdk/integrations/quart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from sentry_sdk.integrations import DidNotEnable, Integration
from sentry_sdk.integrations._wsgi_common import _filter_headers
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from sentry_sdk.traces import SOURCE_FOR_STYLE as SEGMENT_SOURCE_FOR_STYLE
from sentry_sdk.traces import Span, get_current_span
from sentry_sdk.traces import SegmentNameSource, Span, get_current_span
from sentry_sdk.utils import (
capture_internal_exceptions,
ensure_integration_enabled,
Expand Down Expand Up @@ -49,23 +48,11 @@
except ImportError:
raise DidNotEnable("Quart is not installed or incompatible")

TRANSACTION_STYLE_VALUES = ("endpoint", "url")


class QuartIntegration(Integration):
identifier = "quart"
origin = f"auto.http.{identifier}"

transaction_style = ""

def __init__(self, transaction_style: str = "endpoint") -> 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:
request_started.connect(_request_websocket_started)
Expand Down Expand Up @@ -131,25 +118,6 @@ def _sentry_func(*args: "Any", **kwargs: "Any") -> "Any":
Scaffold.route = _sentry_route


def _set_transaction_name_and_source(
scope: "sentry_sdk.Scope", transaction_style: str, request: "Request"
) -> None:
try:
name_for_style = {
"url": request.url_rule.rule,
"endpoint": request.url_rule.endpoint,
}

source = SEGMENT_SOURCE_FOR_STYLE[transaction_style]

scope.set_transaction_name(
name=name_for_style[transaction_style],
source=source,
)
except Exception:
pass


async def _request_websocket_started(app: "Quart", **kwargs: "Any") -> None:
integration = sentry_sdk.get_client().get_integration(QuartIntegration)
if integration is None:
Expand All @@ -172,9 +140,13 @@ async def _request_websocket_started(app: "Quart", **kwargs: "Any") -> None:

# Set the transaction name here, but rely on ASGI middleware
# to actually start the transaction
_set_transaction_name_and_source(
sentry_sdk.get_current_scope(), integration.transaction_style, request_websocket
)
try:
sentry_sdk.get_current_scope().set_transaction_name(
name=request_websocket.url_rule.rule,
source=SegmentNameSource.ROUTE,
)
except Exception:
pass
Comment thread
alexander-alderman-webb marked this conversation as resolved.
Resolved

scope = sentry_sdk.get_isolation_scope()

Expand Down
31 changes: 13 additions & 18 deletions tests/integrations/quart/test_quart.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ async def test_has_context(sentry_init, capture_events):
assert response.status_code == 200

(event,) = events
assert event["transaction"] == "hi"
assert event["transaction"] == "/message"
assert "data" not in event["request"]
assert event["request"]["url"] == "http://localhost/message"

Expand Down Expand Up @@ -545,10 +545,10 @@ async def hi_tx():

message, span = [item.payload for item in items]

assert span["name"] == "hi_tx"
assert span["name"] == "/message_tx"

assert message["message"] == "hi"
assert message["transaction"] == "hi_tx"
assert message["transaction"] == "/message_tx"
assert message["tags"]["view"] == "yes"
assert message["tags"]["before_request"] == "yes"

Expand Down Expand Up @@ -576,9 +576,9 @@ async def error():

error_event, span = [item.payload for item in items]

assert span["name"] == "error"
assert span["name"] == "/error"

assert error_event["transaction"] == "error"
assert error_event["transaction"] == "/error"
(exception,) = error_event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"

Expand Down Expand Up @@ -608,7 +608,7 @@ async def dispatch_request(self):
(event,) = events

assert event["message"] == "hi"
assert event["transaction"] == "hello_class"
assert event["transaction"] == "/hello-class/"


@pytest.mark.asyncio
Expand Down Expand Up @@ -657,31 +657,26 @@ async def test_basic(sentry_init, capture_items):
assert segment["attributes"]["sentry.op"] == "http.server"
assert segment["attributes"]["sentry.origin"] == "auto.http.quart"
assert segment["attributes"]["http.request.method"] == "GET"
assert segment["name"] == "hi"
assert segment["name"] == "/message"


@pytest.mark.asyncio
@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", "endpoint", "hi_with_id", "component"),
("/message/123456", "url", "/message/<message_id>", "route"),
("/message", "/message", "route"),
("/message/123456", "/message/<message_id>", "route"),
],
)
async def test_transaction_style(
async def test_segment_name_and_source(
sentry_init,
capture_items,
url,
transaction_style,
expected_name,
expected_source,
):
sentry_init(
integrations=[
quart_sentry.QuartIntegration(transaction_style=transaction_style)
],
integrations=[quart_sentry.QuartIntegration()],
traces_sample_rate=1.0,
data_collection={},
)
Expand Down Expand Up @@ -934,7 +929,7 @@ async def login():
spans = [item.payload for item in items]
assert len(spans) == 2

segment = next(s for s in spans if s["name"] == "hi")
segment = next(s for s in spans if s["name"] == "/message")
if expect_user_info and user_id is not None:
assert segment["attributes"]["user.id"] == user_id
else:
Expand Down
Loading