Skip to content

Commit 1df9835

Browse files
feat(falcon): Set name and source on request span when streaming (#6562)
Call `Scope.set_transaction_name()` on the current scope in a `process_resource` middleware function (since the route is resolved when `process_resource` runs). This ensures that the span name and the `sentry.span.source` attribute are set on the segment span when the streaming trace lifecycle is enabled.
1 parent 77874bd commit 1df9835

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

sentry_sdk/integrations/falcon.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from sentry_sdk.integrations._wsgi_common import RequestExtractor
66
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
77
from sentry_sdk.tracing import SOURCE_FOR_STYLE
8+
from sentry_sdk.tracing_utils import has_span_streaming_enabled
89
from sentry_sdk.utils import (
910
capture_internal_exceptions,
1011
ensure_integration_enabled,
@@ -104,6 +105,25 @@ def process_request(
104105
scope._name = "falcon"
105106
scope.add_event_processor(_make_request_event_processor(req, integration))
106107

108+
def process_resource(
109+
self, req: "Any", resp: "Any", resource: "Any", params: "Any"
110+
) -> None:
111+
"""
112+
Sets the segment name and source as the route is resolved when this runs.
113+
"""
114+
client = sentry_sdk.get_client()
115+
integration = client.get_integration(FalconIntegration)
116+
if integration is None or not has_span_streaming_enabled(client.options):
117+
return
118+
119+
name_for_style = {
120+
"uri_template": req.uri_template,
121+
"path": req.path,
122+
}
123+
name = name_for_style[integration.transaction_style]
124+
source = sentry_sdk.traces.SOURCE_FOR_STYLE[integration.transaction_style]
125+
sentry_sdk.set_transaction_name(name, source)
126+
107127

108128
TRANSACTION_STYLE_VALUES = ("uri_template", "path")
109129

tests/integrations/falcon/test_falcon.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,36 @@ def test_transaction_style(
121121
integration = FalconIntegration(transaction_style=transaction_style)
122122
sentry_init(
123123
integrations=[integration],
124+
traces_sample_rate=1.0,
124125
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
125126
)
126127

127128
client = make_client()
128129
if span_streaming:
129130
items = capture_items("event")
131+
items = capture_items("event", "span")
130132

131133
response = client.simulate_get(url)
132134
assert response.status == falcon.HTTP_200
133135

134-
(event,) = (item.payload for item in items)
136+
(event,) = (item.payload for item in items if item.type == "event")
137+
138+
sentry_sdk.flush()
139+
spans = [item.payload for item in items if item.type == "span"]
140+
spans = [span for span in spans if span["name"] == expected_transaction]
141+
assert len(spans) == 1
142+
assert spans[0]["attributes"]["sentry.span.source"] == expected_source
135143
else:
136144
events = capture_events()
137145

138146
response = client.simulate_get(url)
139147
assert response.status == falcon.HTTP_200
140148

141-
(event,) = events
149+
(event, transaction) = events
150+
151+
assert transaction["transaction"] == expected_transaction
152+
assert transaction["transaction_info"] == {"source": expected_source}
153+
142154
assert event["transaction"] == expected_transaction
143155
assert event["transaction_info"] == {"source": expected_source}
144156

0 commit comments

Comments
 (0)