Skip to content

Commit 7fe1845

Browse files
feat(falcon): Set name and source on request span when streaming
1 parent cac53d1 commit 7fe1845

2 files changed

Lines changed: 30 additions & 16 deletions

File tree

sentry_sdk/integrations/falcon.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ def process_request(
104104
scope._name = "falcon"
105105
scope.add_event_processor(_make_request_event_processor(req, integration))
106106

107+
def process_resource(
108+
self, req: "Any", resp: "Any", resource: "Any", params: "Any"
109+
) -> None:
110+
integration = sentry_sdk.get_client().get_integration(FalconIntegration)
111+
if integration is None:
112+
return
113+
114+
name_for_style = {
115+
"uri_template": req.uri_template,
116+
"path": req.path,
117+
}
118+
name = name_for_style[integration.transaction_style]
119+
source = SOURCE_FOR_STYLE[integration.transaction_style]
120+
sentry_sdk.set_transaction_name(name, source)
121+
sentry_sdk.get_isolation_scope().set_transaction_name(name, source)
122+
107123

108124
TRANSACTION_STYLE_VALUES = ("uri_template", "path")
109125

@@ -233,23 +249,10 @@ def _has_http_5xx_status(response: "falcon.Response") -> bool:
233249
return response.status.startswith("5")
234250

235251

236-
def _set_transaction_name_and_source(
237-
event: "Event", transaction_style: str, request: "falcon.Request"
238-
) -> None:
239-
name_for_style = {
240-
"uri_template": request.uri_template,
241-
"path": request.path,
242-
}
243-
event["transaction"] = name_for_style[transaction_style]
244-
event["transaction_info"] = {"source": SOURCE_FOR_STYLE[transaction_style]}
245-
246-
247252
def _make_request_event_processor(
248253
req: "falcon.Request", integration: "FalconIntegration"
249254
) -> "EventProcessor":
250255
def event_processor(event: "Event", hint: "dict[str, Any]") -> "Event":
251-
_set_transaction_name_and_source(event, integration.transaction_style, req)
252-
253256
with capture_internal_exceptions():
254257
FalconRequestExtractor(req).extract_into_event(event)
255258

tests/integrations/falcon/test_falcon.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,35 @@ 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:
129-
items = capture_items("event")
130+
items = capture_items("event", "span")
130131

131132
response = client.simulate_get(url)
132133
assert response.status == falcon.HTTP_200
133134

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

138145
response = client.simulate_get(url)
139146
assert response.status == falcon.HTTP_200
140147

141-
(event,) = events
148+
(event, transaction) = events
149+
150+
assert transaction["transaction"] == expected_transaction
151+
assert transaction["transaction_info"] == {"source": expected_source}
152+
142153
assert event["transaction"] == expected_transaction
143154
assert event["transaction_info"] == {"source": expected_source}
144155

0 commit comments

Comments
 (0)