Skip to content

Commit 89023f4

Browse files
starlette and fastapi tests
1 parent 255fcee commit 89023f4

5 files changed

Lines changed: 106 additions & 6 deletions

File tree

sentry_sdk/integrations/_asgi_common.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ def _get_ip(asgi_scope: "Any") -> str:
8686
return asgi_scope.get("client")[0]
8787

8888

89-
def _get_request_data(asgi_scope: "Any") -> "Dict[str, Any]":
89+
def _get_request_data(
90+
asgi_scope: "Any", path_includes_root_path: "bool" = True
91+
) -> "Dict[str, Any]":
9092
"""
9193
Returns data related to the HTTP request from the ASGI scope.
9294
"""
@@ -101,7 +103,10 @@ def _get_request_data(asgi_scope: "Any") -> "Dict[str, Any]":
101103
request_data["query_string"] = _get_query(asgi_scope)
102104

103105
request_data["url"] = _get_url(
104-
asgi_scope, "http" if ty == "http" else "ws", headers.get("host")
106+
asgi_scope,
107+
"http" if ty == "http" else "ws",
108+
headers.get("host"),
109+
path_includes_root_path=path_includes_root_path,
105110
)
106111

107112
client = asgi_scope.get("client")
@@ -111,7 +116,9 @@ def _get_request_data(asgi_scope: "Any") -> "Dict[str, Any]":
111116
return request_data
112117

113118

114-
def _get_request_attributes(asgi_scope: "Any") -> "dict[str, Any]":
119+
def _get_request_attributes(
120+
asgi_scope: "Any", path_includes_root_path: "bool" = True
121+
) -> "dict[str, Any]":
115122
"""
116123
Return attributes related to the HTTP request from the ASGI scope.
117124
"""
@@ -132,7 +139,10 @@ def _get_request_attributes(asgi_scope: "Any") -> "dict[str, Any]":
132139
attributes["http.query"] = query
133140

134141
url_without_query_string = _get_url(
135-
asgi_scope, "http" if ty == "http" else "ws", headers.get("host")
142+
asgi_scope,
143+
"http" if ty == "http" else "ws",
144+
headers.get("host"),
145+
path_includes_root_path=path_includes_root_path,
136146
)
137147
query_string = _get_query(asgi_scope)
138148
attributes["url.full"] = (

sentry_sdk/integrations/asgi.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ async def _run_app(
322322
with span_ctx as span:
323323
if isinstance(span, StreamedSpan):
324324
for attribute, value in _get_request_attributes(
325-
scope
325+
scope,
326+
path_includes_root_path=self.path_includes_root_path,
326327
).items():
327328
span.set_attribute(attribute, value)
328329

@@ -404,7 +405,11 @@ def event_processor(
404405
self, event: "Event", hint: "Hint", asgi_scope: "Any"
405406
) -> "Optional[Event]":
406407
request_data = event.get("request", {})
407-
request_data.update(_get_request_data(asgi_scope))
408+
request_data.update(
409+
_get_request_data(
410+
asgi_scope, path_includes_root_path=self.path_includes_root_path
411+
)
412+
)
408413
event["request"] = deepcopy(request_data)
409414

410415
# Only set transaction name if not already set by Starlette or FastAPI (or other frameworks)

sentry_sdk/integrations/starlette.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def setup_once() -> None:
143143
)
144144

145145
patch_middlewares()
146+
# See https://github.com/Kludex/starlette/commit/e8f0dcd54e4ceec47e02c45f5275374e292339ad
146147
path_includes_root_path = version >= (0, 33)
147148
patch_asgi_app(path_includes_root_path=path_includes_root_path)
148149
patch_request_response()

tests/integrations/fastapi/test_fastapi.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,48 @@ def test_transaction_http_method_custom(sentry_init, capture_events):
10431043
assert event2["request"]["method"] == "HEAD"
10441044

10451045

1046+
@pytest.mark.parametrize("span_streaming", [True, False])
1047+
def test_request_url(sentry_init, capture_events, capture_items, span_streaming):
1048+
sentry_init(
1049+
traces_sample_rate=1.0,
1050+
send_default_pii=True,
1051+
integrations=[
1052+
StarletteIntegration(),
1053+
],
1054+
_experiments={
1055+
"trace_lifecycle": "stream" if span_streaming else "static",
1056+
},
1057+
)
1058+
1059+
starlette_app = fastapi_app_factory()
1060+
1061+
client = TestClient(starlette_app, root_path="/root")
1062+
1063+
if span_streaming:
1064+
items = capture_items("span")
1065+
1066+
client.get("/root/nomessage")
1067+
sentry_sdk.flush()
1068+
spans = [item.payload for item in items]
1069+
1070+
(server_span,) = (
1071+
span
1072+
for span in spans
1073+
if span["attributes"].get("sentry.op") == "http.server"
1074+
)
1075+
assert server_span["attributes"]["url.full"] == (
1076+
"http://testserver/root/nomessage"
1077+
)
1078+
else:
1079+
events = capture_events()
1080+
1081+
client.get("/root/nomessage")
1082+
1083+
assert len(events) == 1
1084+
(event,) = events
1085+
assert event["request"]["url"] == "http://testserver/root/nomessage"
1086+
1087+
10461088
@parametrize_test_configurable_status_codes
10471089
def test_configurable_status_codes(
10481090
sentry_init,

tests/integrations/starlette/test_starlette.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,48 @@ def test_transaction_http_method_default(sentry_init, capture_events):
14771477
assert event["request"]["method"] == "GET"
14781478

14791479

1480+
@pytest.mark.parametrize("span_streaming", [True, False])
1481+
def test_request_url(sentry_init, capture_events, capture_items, span_streaming):
1482+
sentry_init(
1483+
traces_sample_rate=1.0,
1484+
send_default_pii=True,
1485+
integrations=[
1486+
StarletteIntegration(),
1487+
],
1488+
_experiments={
1489+
"trace_lifecycle": "stream" if span_streaming else "static",
1490+
},
1491+
)
1492+
1493+
starlette_app = starlette_app_factory()
1494+
1495+
client = TestClient(starlette_app, root_path="/root")
1496+
1497+
if span_streaming:
1498+
items = capture_items("span")
1499+
1500+
client.get("/root/nomessage")
1501+
sentry_sdk.flush()
1502+
spans = [item.payload for item in items]
1503+
1504+
(server_span,) = (
1505+
span
1506+
for span in spans
1507+
if span["attributes"].get("sentry.op") == "http.server"
1508+
)
1509+
assert server_span["attributes"]["url.full"] == (
1510+
"http://testserver/root/nomessage"
1511+
)
1512+
else:
1513+
events = capture_events()
1514+
1515+
client.get("/root/nomessage")
1516+
1517+
assert len(events) == 1
1518+
(event,) = events
1519+
assert event["request"]["url"] == "http://testserver/root/nomessage"
1520+
1521+
14801522
@pytest.mark.skipif(
14811523
STARLETTE_VERSION < (0, 21),
14821524
reason="Requires Starlette >= 0.21, because earlier versions do not support HTTP 'HEAD' requests",

0 commit comments

Comments
 (0)